Comments to the JSON related proposals

Simon Bünzli zeniko at gmail.com
Sun Aug 19 10:43:29 PDT 2007


doekman at gmail.com schrieb am 19.08.07 16:49:
>> 2. The optional filter argument should allow both function and blacklist
> Why can't a function define a blacklist?

It can (given that my proposal #6 is implemented). For simple filtering 
an Array is enough, though, and should perform better than a function (a 
string array can be converted into a native [e.g. C++] hash for speedy 
querying - as opposed to having to call a JavaScript function for every 
key).

And as I said: I'd prefer a blacklist to a whitelist as IME the use 
cases for a whitelist will rather require context (e.g. type and number 
of children) opposed to a blacklist for just getting rid of extensions 
to Object.prototype or temporary keys.

> I think a filter can have (from a blacklist point of view):
> - string: case sensitive exclusion
> - regular expression: pattern exclusion, and case insensitive stuff
> - function: for even more complicated exclusions

Sure, as a third alternative a RegExp could be considered (instead of 
String Array or Function), which would lead to the following three 
calling options:

x.toJSONString(function(key, value) {
     return ["exclude", "these", "keys"].indexOf(key) == -1 ?
            value : undefined;
}); // this specific use case shouldn't require a function at all

x.toJSONString(["exclude", "these", "keys"]);

x.toJSONString(/^(exclude|these|keys)$/i);

> and we could combine them with an array:
> x.toJSONString( { exclude: ['name','email',/^.+code$/i,function(p) {
> return p.charAt(0)!=p.charAt(1); } ] } );

Looks slightly over-engineered to me.

> the object-syntax is to deal with options (include, exclude and
> prettyPrint).

If you only want to pretty-print, that'd be

x.toJSONString(null, true); or x.toJSONString([], true);

~sb



More information about the Es4-discuss mailing list