JSON decoding

Bob Ippolito bob at redivi.com
Fri Oct 20 15:36:54 PDT 2006

On 10/20/06, Brendan Eich <brendan at mozilla.org> wrote:
> On Oct 20, 2006, at 4:53 PM, Bob Ippolito wrote:
> > I'd imagine that for most values of N and M, that the latter is
> > better. When M is zero, which is probably usually will be, then there
> > is no copy made. It also provides symmetry with decoding and is
> > infinitely more flexible and useful.
> I agree it's many times better to have a symmetric object hook.  I
> was re-reading
> http://svn.red-bean.com/bob/simplejson/trunk/docs/module-
> simplejson.html#load
> and wondered why you don't have an object hook for dump/dumps.  Also,
> is there a way to have the object_hook cause the key/value for which
> the object passed into the hook is the value to be skipped entirely?

It does have a hook, but it's a method not a parameter...

To extend this to recognize other objects, subclass and implement a
.default() method with another method that returns a serializable
object for o if possible, otherwise it should call the superclass
implementation (to raise TypeError).

The object hook gets just object values, not key:value pairs. If you
don't want a key:value pair included, return an object that does not
have that key:value pair.

If you worked only with key:value pairs, then you wouldn't be able to
hook elements of an Array... and that would be silly. It's also not
reasonable to implement skip for Arrays, because if they're used as a
tuple then you've changed the meaning of the Array. Skipping an object
wholesale is best done by returning a placeholder like null, but in
general most people implement encoders that raise exceptions if stuff
they don't want ends up in the object graph...


More information about the Es4-discuss mailing list