Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3360304
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T02:58:35+00:00 2026-05-18T02:58:35+00:00

I have defined a Point object in a file Point.pm as following: package Point;

  • 0

I have defined a Point object in a file Point.pm as following:

package Point;
sub new {
    my ($class) = @_;
    my $self = {
        _x => 0,
        _y => 0,
    };
    return bless $self => $class;
}

sub X {
    my ($self, $x) = @_;
    $self->{_x} = $x if defined $x;
    return $self->{_x};
}

sub Y {
    my ($self, $y) = @_;
    $self->{_y} = $y if defined $y;
    return $self->{_y};
}

1;

Now when I use JSON to convert the object to JSON by the following code:

use JSON;
use Point;

Point $p = new Point;
$p->X(20);
$p->Y(30);

my $json = encode_json $p;

I get the following error:

encountered object 'Point=HASH(0x40017288)', but neither allow_blessed nor convert_blessed settings are enabled at test.pl line 28

How do I convert to and from JSON to an object using the JSON module?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T02:58:36+00:00Added an answer on May 18, 2026 at 2:58 am

    The warning tells you most of what is wrong. Unless you tell JSON how to handle blessed references(Perl objects), JSON handles only un-blessed data structures.

    You can convert_blessed and you can allow_blessed. For allow_blessed, it says:

    If $enable is false (the default), then encode will throw an exception when it encounters a blessed object.

    Point is an object class, thus an instance of Point is a blessed reference, and thus the default for JSON is to throw an exception.

    If you enable convert_blessed, it will call a TO_JSON method on your object. With simple objects like Point (ones that contain no blessed members), you can do that as easily as:

    sub TO_JSON { return { %{ shift() } }; }
    

    If you have to descend a structure, it will get a lot hairier.


    Somebody in the comments below said that I didn’t cover how to get objects out of JSON.

    The basics are simple. So here goes

    my $object = bless( JSON->new->decode( $json_string ), 'ClassIWant' );
    

    I mainly covered the part that prevents you from simply serializing a blessed object into JSON.

    The basics of deserialization are simple, just like the basics of serialization are simple–once you know the trick. There is no error in the way, there is just the task of finding what you need and blessing it into the right class.

    If you want to have code coupled to the objects, then you’ll know what has to be blessed and what it will have to be blessed into. If you want totally decoupled code, this is no harder or easier in Perl than it is in JavaScript itself.

    You’re going to have to serialize a marker in the JSON. If I need something like this, I will insert a '__CLASS__' field into the blessed objects. And when deserializing, I will descend through the structure and bless everything like this:

     bless( $ref, delete $ref->{__CLASS__} );
    

    But as I said, this is no easier or harder in Perl, because JSON presents the same challenge to all languages.

    As Schwern suggested in his comment up top, YAML is much better built for serializing and deserializing objects, because it has notation for it. JSON gives you associative arrays or arrays.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a very simple method: Class Team(models.Model): def sides(self): return SideNames.objects.filter(team=self) SideNames is
I have an implicit scalar field defined in 2D, for every point in 2D
I have defined an interface in C++, i.e. a class containing only pure virtual
I have defined a new dialog and its controls in an already existing resource
I have an interface that I have defined in C++ which now needs to
Let's say I have defined a route: routes.Add(new Route(Users/{id}, new MvcRouteHandler()) { Defaults =
Let's say we have defined a CSS class that is being applied to various
I have a Moose class that i would like to store using Apache::Session::File. However,
sI have an XML file that looks like the following... <a> <b> <version>1.0</version> <c>
I have a 4 side convex Polygon defined by 4 points in 2D, and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.