DEV Community

Cover image for Extending JSON for fun and profit

Extending JSON for fun and profit

Klemen Slavič on October 28, 2018

header image by Steel's Fudge In the early days of JavaScript when asynchronous requests first enabled web authors to make requests to HTTP server...
Collapse
 
gmartigny profile image
Guillaume Martigny

Very neat article with inspiring code. I never thought of your "private class member" with symbols, this is amazing (Why use Symbol.for tho ? It would allow access with instance[Symbol.for("name")] ?!.

Another question, why use Map for the classes dictionary ? Object literal would make a more readable code (IMO of course).

const createClassLookup = (scope = {}) => (name) =>
  scope[name] || (global || window)[name];
const classes = {
  Drone,
};
Collapse
 
krofdrakula profile image
Klemen Slavič

Actually, yeah, you're right... I should've used Symbol('name') instead. My bad! I'll fix the examples.

In the case of the classes lookup, you might have to have some mapping between the value of the $type property and the actual classname if they happen to not coincide. But really, there's no reason to pick Map over an object if you're just mapping strings to objects. I just like to use Map instead of POJOs as hashmaps when they become highly polymorphic, as it doesn't create hidden classes on mutation.

Collapse
 
jamesmalvi profile image
Jaimie Malvi

Awesome Article Klemen.

Would love to share JSON parser tool jsonformatter.org/json-parser

Collapse
 
ben profile image
Ben Halpern

Jaimie, you appear to be a sort of low-key spam account. But you've been on-platform for about a year and a half and you only have nine comments, all like this. So I'm not even mad, I'm impressed.

Collapse
 
johncarroll profile image
John Carroll

Your comment made me click on Jaimie's profile. I don't see how you reached the spam conclusion. Clearly, a JSON parser tool is on-topic here, and past comments about the tool also seem to be on-topic.

Thread Thread
 
krofdrakula profile image
Klemen Slavič

I would be wary of these kinds of online tools; you may be exposing sensitive data to a 3rd party. It's easy to mistakenly leak configuration, passwords or keys accidentally this way.

Collapse
 
the_doctor profile image
Vaibhav

TIL I qualify as a spam account.

Thread Thread
 
gmartigny profile image
Guillaume Martigny

TIL Kate Upton really like JSON :D
pinterest.fr/pin/139470919694956648/

Collapse
 
krofdrakula profile image
Klemen Slavič

True, I could have framed it as, say, a saving mechanism for a game using *Storage. I just hope the idea carries over rather than the implementation details in the reader's mind. :)

Collapse
 
mikaturk profile image
mikat

Very interesting article, thanks for the great read :D

Collapse
 
carlosmgspires profile image
Carlos Pires

Great article. Concise and precise. Keep it up!

Collapse
 
philnash profile image
Phil Nash

Thanks for writing this! I had no idea about the reviver and replacer functions and I reckon they will come in handy in the future.

Collapse
 
ben profile image
Ben Halpern

Fascinating