JSON.parse, JS typecasting and revivers

Sorry if this is a stupid question, this is my first JS project ...

I am trying to deserialize a custom object in JS using JSON. The problem is that JSON.parse () does not return an object as its original type. Since direct casting of the returned object to the desired type is not possible, this leaves me with the "reviver" parameter ...

Am I missing something? Isn't JSON the whole thing to avoid having to write your own methods for serializing and deserializing objects? What is the point of using JSON if I need to write my own method to animate my object?

+6
source share
1 answer

JSON is a format for raw data. This is very primitive. It supports dictionaries (aka javascript objects, hashes, associative arrays), arrays, strings, numbers, booleans and zero. It. The reason why this does not do anymore is that these primitives are agnostic for the language, and almost all programming languages โ€‹โ€‹are built into the tools for processing these data types.

If you had other notations like "class", then suddenly it becomes very attached to specific languages โ€‹โ€‹or code areas and loses its wide and general applicability.

So think of JSON as simple raw data. JSON is not a sorting of instances or a complete serialization of objects. So yes, you need to write "revivers" if you are going to serialize JS objects created from constructors.

This approach takes things like backbone.js take :

new Book({ title: "One Thousand and One Nights", author: "Scheherazade" }); 

You simply pass your simple data object (the result of your JSON.parse() ) to your constructor call. From there, you can do any number of things to read the values โ€‹โ€‹in your new object.

Javascript does not have a standard way to sort all objects, e.g. in ruby โ€‹โ€‹with Marhsal.dump(obj) , for example.


EDIT: last important point ...

Javascript objects do not have a โ€œtypeโ€ as you think of it in other languages. A Javascript object is just a dictionary of key / value pairs. When you do something like new SomeClass() , you get a new object that has a special prototype property that points to the prototype property of an object of the SomeClass function. Wether, an object is an instance of SomeClass , is a less clear question than you might think at first glance.

Instead, you may ask, "What is a constructor function object?" or "What objects are in my prototype chain?"

So, if you want to pass the โ€œtypeโ€ to JSON, which is stored as a string, then how will you store the reference to the prototype object? Maybe the prototype object is not named at all in the global area and is accessible only through closure? Then, when you write this in a string, you can no longer reference this prototype object.

The fact is that the prototype of javascripts for creating an instance of an object, in combination with its lexical coverage based on closure, and the fact that objects store their values โ€‹โ€‹more than C pointers than literal values, it is very difficult to completely serialize the object to a location external to the virtual machine.

+9
source

Source: https://habr.com/ru/post/894959/


All Articles