2

In my client/server web app, I am generating large JSON strings (using JSON.NET JToken classes in C# web services) which I pass via AJAX Get requests to the client.

I would like to be able to define certain properties in the ES5 style: prop: { value: '1', enumerable: false, writable: true }, but there's a hitch.

First, JSON.parse() simply interprets the 'enumerable' and 'writable' properties as generic properties on a generic JavaScript object. This is probably expected and desirable behavior, but is the a comparable method to 'parse' the JSON with respect to new ES5 property attributes?

Second, Object.create() works, but it has some limitations. For example,

//Native works
var prop = Object.create(Object.prototype, { prop: { value: '1', enumerable: false, writable: true } });
//prop === "1"

//Parse works
var prop = Object.create(Object.prototype, JSON.parse({ "prop": { "value": '1', "enumerable": false, "writable": true } });
//prop === "1"    

//Parse fails
var prop = Object.create(Object.prototype, JSON.parse({ "prop": { "value": '1', "enumerable": "false", "writable": "true" } });
// prop === JSON.parse({ "prop": { "value": '1', "enumerable": "false", "writable": "true" } })

The problem here, of course, is that "true" !== true, which instructs Object.create() to return the same object as JSON.parse(). Further, either because of the truthy evaluation of a particular prop attribute OR because I'm attempting to create particularly large objects, Object.create() seems to be quite fragile.

Is there a better way to "parse" these objects in a way that respects the ES5 prop attributes?

Community
  • 1
  • 1
Christopher
  • 1,723
  • 1
  • 18
  • 31
  • Can you guarantee you will never get `false` as a string? – alex Oct 13 '11 at 01:07
  • @alex, yes I could. I'm trying to build this out on the mobile portion of my app where I can safely assume that all users have an ES5 capable browser (we only guarantee support for iOS and Android devices). If I didn't have to maintain compatibility for shared methods servicing the full app (requires supporting IE), I could do this easily with a single refactoring. Since I have to fork the logic for the mobile context, it would be much easier if I didn't have to worry about across the board bool conversion. – Christopher Oct 14 '11 at 02:46
  • ES5 API is very specific in the way it allows to define properties. Besides `Object.create`, you can also use `defineProperty` or `defineProperties`, but the input conforms to the very same pattern (you provide value, and attribute flags — writable, enumerable, configurable — in boolean form). A "better" way I could think of is to create your own wrapper, which would take care of these boolean-as-string "incompatibilities". – kangax Oct 16 '11 at 17:27
  • @kangax I'm still building out my prototype, so I don't have empirical evidence to support or confront my fears, but I suspect that either create() or defineProperty() will fail against my ES5 assumptions when attributes have been incorrectly read as properties. When in an environment where we can't guarantee all assumptions will be either (fully truthily) true or false, this becomes problem. – Christopher Oct 17 '11 at 23:47

0 Answers0