Reputation: 39182
I'm supposed to set up JSON formatted data parsing in a project for IE (exclusively) and I know about this library but the data transfer will be significant issue and I'd like to minimize the amount of bytes moved.
The JSON object I'll be consuming is very small and quite flat, in human terms maybe 30 lines in at most 3 levels of depth.
Is there a better way to approach this? RegEx tends to create more problems than it solves. Any other ideas on how to pick out a value (or a set of values) connected to a node called e.g. shabong?
I'm used to the XDocument
class in C# where one only needs to specify the name of a descendant to get a bunch of information contained in a tag. A similar solutions would be preferable. (Old dog, new tricks, hehe.)
Upvotes: 0
Views: 132
Reputation: 48771
If you can be guaranteed that the data is secure, then you can use eval
or the Function
constructor to parse the data.
This is because the JSON data structures and values very closely resemble those found in JavaScript's literal notations, so you can usually resort to evaluating the data as actual JavaScript expressions.
Using eval
:
var parsed = eval("(" + myjsondata + ")");
Using Function
:
var parsed = (new Function("return " + myjsondata))();
Again, this is only if you're absolutely certain of the integrity of the data being passed. Because you're treating it like part of the program, any malicious code that was substituted for valid data will be executed.
The Function
version offers a tiny bit of security in that it's invoked in the global scope, so any malicious code won't have access to local variables. This should offer little comfort overall.
Aside from the above solution, you could always create your own serialization that is specific to the needs of your data. If the data is simple enough, it could offer you the ability to write a very short and fast parser for your custom serialization.
Upvotes: 1