最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - JSON.parse, JS typecasting, and revivers - Stack Overflow

programmeradmin6浏览0评论

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

I'm trying to deserialize a custom object in JS using JSON. The problem is, JSON.parse() does not return the object as its original type. Since directly casting the returned object to the desired type doesn't seem possible, that leaves me with the 'reviver' option...

Am I missing something here? Wasn't the entire point of JSON to avoid having to write custom methods to serialize and deserialize objects? What's the point of using JSON if I have to write my own method 'revive' my object?

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

I'm trying to deserialize a custom object in JS using JSON. The problem is, JSON.parse() does not return the object as its original type. Since directly casting the returned object to the desired type doesn't seem possible, that leaves me with the 'reviver' option...

Am I missing something here? Wasn't the entire point of JSON to avoid having to write custom methods to serialize and deserialize objects? What's the point of using JSON if I have to write my own method 'revive' my object?

Share Improve this question asked Aug 13, 2011 at 0:48 MikeMike 2,4341 gold badge16 silver badges19 bronze badges 1
  • See my answer about using a reviver [here][1]. [1]: stackoverflow./questions/14027168/… – Sean Kinsey Commented Dec 25, 2012 at 8:17
Add a ment  | 

1 Answer 1

Reset to default 9

JSON is a format for raw data. It's very primitive. It supports dictionaries (aka. javascript objects, hashes, associative arrays), arrays, strings, numbers, booleans and null. That's it. The reason it doesn't do more is that those primitives are language agnostic, and nearly all programming language have built in tools to handle those types of data.

If you had other notation like "class" then suddenly it bees very bound to specific languages or codeboases, and lose it's wide and generic applicability.

So think of JSON as simple raw data. JSON is not instance marshalling or plete object serialization. So yes you need to write "revivers" if you intend to serialize JS objects instantiated from constructors.

This is the approach things like backbone.js take:

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

You simply pass your plain object of data (the result of your JSON.parse()) to the call to your constructor of choice. From there you can do any number of things to read it's values into your new object.

Javascript has no standard way of Marshalling whole objects like you can in ruby with Marhsal.dump(obj) for instance.


EDIT: One last major point...

Javascript objects don't have a "type" as you would think of it in other languages. A Javascript object is simply a 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 the SomeClass function object. Wether an object is an instance of SomeClass is less of a clear cut question than you may think at first glance.

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

So if you wanted to convey "type" in JSON, which is stored as a string, then how would you store a reference to a prototype object? Maybe the prototype object is not named in the global scope at all, and only available via closure? Then when you write that out to string, you have no way of referencing that prototype object anymore at all.

The point is that javascripts prototypical approach to object instantiation, bined with it's closure based lexical scoping and the fact that objects hold their values more like C pointers than literal values, makes it very very hard to fully serialize an object out to a location external to the VM.

发布评论

评论列表(0)

  1. 暂无评论