Property Iteration in JSON serialization

Luke Smith lsmith at lucassmith.name
Tue Oct 13 22:54:51 PDT 2009


On Oct 13, 2009, at 9:24 PM, Brian Kardell wrote:

>
> There are potentially some additional practical upshots beyond human
> readability to this which I won't get into here until I find out:  Is
> it even plausible to accomplish this with new built in JSON supports?
>
> To be specific, if it's not clear... Given an instance of an order
> object, I would like to serialize it like this:
>
> {
>    "items": [
>        {
>            "description": "A bottle of rum",
>            "itemId":  13523,
>            "price": 12.94,
>            "quantity": 6
>        },
>        {
>            "description": "A bottle of whiskey",
>            "itemId":  23423,
>            "price": 6.95,
>            "quantity": 24
>        }
>    ],
>    "orderNumber": 1234123,
>    "shipTo": {
>        "city": "Seattle",
>        "name": "Jane Doe",
>        "state": "Washington",
>        "streetAddress": "123 Main Street",
>        "zip": 12345
>    }
> }

It is possible to sort object keys in a replacer function.  See http://gist.github.com/209826

Currently FF3.5.4 doesn't properly apply replacer functions, but  
Safari 4, WebKit, IE8, and Chrome 3 work fine for this task.

Luke


More information about the es-discuss mailing list