Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #77513

Re: Storing instances using jsonpickle

Date 2014-09-04 00:39 +0100
From MRAB <python@mrabarnett.plus.com>
Subject Re: Storing instances using jsonpickle
References <c54f7a2f-cf2c-4ee9-ba8e-a309761c1c6a@googlegroups.com> <mailman.13748.1409777587.18130.python-list@python.org> <46e782a5-b087-4f95-aadb-26e233bf5419@googlegroups.com>
Newsgroups comp.lang.python
Message-ID <mailman.13750.1409787549.18130.python-list@python.org> (permalink)

Show all headers | View raw


On 2014-09-03 23:30, Josh English wrote:
> On Wednesday, September 3, 2014 1:53:23 PM UTC-7, Ned Batchelder
> wrote:
>
>> Pickle (and it looks like jsonpickle) does not invoke the class'
>> __init__ method when it reconstitutes objects.  Your new __init__
>> is not being run, so new attributes it defines are not being
>> created.
>>
>> This is one of the reasons that people avoid pickle: being
>> completely implicit is very handy, but also fragile.
>>
>
> I seem to remember having this exact same frustration when I used
> pickle and shelve 15 years ago. I had hoped to have another way
> around this. I spent over a decade trying to make an XML-based
> database work, in part because of this limitation.
>
> Some days I get so frustrated I think the only data structure I
> should ever use is a dictionary.
>
> I suppose to make this sort of thing work, I should look at creating
> custom json encoders and decoders.
>
I occasionally think about a superset of JSON, called, say, "pyson" ...
ah, name already taken! :-(

In summary, it would be something like this:

JSON supports int, float, string, None (as 'null'), list and dict (the 
key must be string).

It would add tuples, delimited by (...), which are not used otherwise 
(no expressions):

     () => ()
     (0, ) => (0)
     (0, 1) => (0, 1)

The key of a dict could also be int, float, or tuple.

It could support other classes, and could handle named arguments.

For example, sets:

To encode {0, 1, 2):

     Look in encoder dict for encoder function with class's name ('set') 
and call it, passing object.

     Encoder returns positional and keyword arguments: [0, 1, 2] and {}.

     Output name, followed by encoded arguments in parentheses.

     Encoder for set:

     def encode_set(obj):
         return list(obj), {}

To decode 'set(0, 1, 2)':

     Parse name: 'set'.

     Parse contents of parentheses: [0, 1, 2] and {}.

     Look in decoder dict for decoder function with given name ('set') 
and call it, passing arguments.

     Result would be {0, 1, 2}.

     Decoder for set:

     def decode_set(*args):
         return set(args)

pyson.dumps({0, 1, 2}, decoders={'set': decode_set}) would return 
'set(0, 1, 2)'.

pyson.loads('set(0, 1, 2)', encoders={'set': encode_set}) would return 
{0, 1, 2}.

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Storing instances using jsonpickle Josh English <Joshua.R.English@gmail.com> - 2014-09-03 13:32 -0700
  Re: Storing instances using jsonpickle Ned Batchelder <ned@nedbatchelder.com> - 2014-09-03 16:52 -0400
    Re: Storing instances using jsonpickle Josh English <Joshua.R.English@gmail.com> - 2014-09-03 15:30 -0700
      Re: Storing instances using jsonpickle MRAB <python@mrabarnett.plus.com> - 2014-09-04 00:39 +0100
        Re: Storing instances using jsonpickle Denis McMahon <denismfmcmahon@gmail.com> - 2014-09-04 01:11 +0000
      Re: Storing instances using jsonpickle Ned Batchelder <ned@nedbatchelder.com> - 2014-09-03 22:18 -0400
        Re: Storing instances using jsonpickle Sam Raker <sam.raker@gmail.com> - 2014-09-03 21:52 -0700
        Re: Storing instances using jsonpickle Josh English <Joshua.R.English@gmail.com> - 2014-09-15 12:30 -0700
      Re: Storing instances using jsonpickle Chris Angelico <rosuav@gmail.com> - 2014-09-04 15:17 +1000
        Re: Storing instances using jsonpickle Dan Sommers <dan@tombstonezero.net> - 2014-09-05 04:38 +0000
          Re: Storing instances using jsonpickle Chris Angelico <rosuav@gmail.com> - 2014-09-05 15:08 +1000
      Re: Storing instances using jsonpickle MRAB <python@mrabarnett.plus.com> - 2014-09-04 12:07 +0100
      Re: Storing instances using jsonpickle MRAB <python@mrabarnett.plus.com> - 2014-09-05 18:04 +0100
        Re: Storing instances using jsonpickle Marko Rauhamaa <marko@pacujo.net> - 2014-09-05 20:16 +0300
          Re: Storing instances using jsonpickle Ned Batchelder <ned@nedbatchelder.com> - 2014-09-05 13:30 -0400
            Re: Storing instances using jsonpickle Marko Rauhamaa <marko@pacujo.net> - 2014-09-05 21:04 +0300
              Re: Storing instances using jsonpickle Ned Batchelder <ned@nedbatchelder.com> - 2014-09-05 16:50 -0400
                Re: Storing instances using jsonpickle Marko Rauhamaa <marko@pacujo.net> - 2014-09-05 23:57 +0300
          Re: Storing instances using jsonpickle Irmen de Jong <irmen.NOSPAM@xs4all.nl> - 2014-09-06 00:18 +0200
      Re: Storing instances using jsonpickle Chris Angelico <rosuav@gmail.com> - 2014-09-06 10:20 +1000
      Re: Storing instances using jsonpickle MRAB <python@mrabarnett.plus.com> - 2014-09-06 17:32 +0100
      Re: Storing instances using jsonpickle Ned Batchelder <ned@nedbatchelder.com> - 2014-09-06 12:56 -0400
      Re: Storing instances using jsonpickle Terry Reedy <tjreedy@udel.edu> - 2014-09-06 14:27 -0400

csiph-web