2

I understand that JSON is for exchanging information. What's confusing me is, I would like to be able to use JSON for storing and calling Objects and their constructors if possible, but since JSON is literal notation I was wondering if there was a way to fill in the JSON object parameters with some type of constructor much like the way a normal constructor would work.

The closest thing I've found is this:

Normal Constructor:

var dude = function(name, age) {
this.name = name;
this.age = age;
}
var bro = new dude("chad", 22);

JSON:

var bro = {
'name': "chad", 
'age': 22
};

But even these aren't really the same considering with the constructor you can call var bro2 = new dude("tony", 21); at any time and have a new instance of dude whenever you want. How could you keep this type of functionality with JSON thrown into the mix?

ryanhagz
  • 193
  • 1
  • 2
  • 15
  • JSON is a format for mainly strings, so your question isn't as easy to understand as you might think, are you really talking about just regular javascript objects ? – adeneo Jan 02 '14 at 02:23
  • Yes, but you can store complex objects in JSON as well. – ryanhagz Jan 02 '14 at 02:27
  • 2
    What you are calling JSON in your second code example is not JSON. It is the creation of an object using JavaScript's literal syntax. JSON is a portable, platform-neutral data serialization scheme which uses a subset of JavaScript literal syntax. – JAAulde Jan 02 '14 at 02:28
  • I am not sure your JSON example is actually a JSON. JSON mean's javascript object notation, and is a string that can be easily serialised into a Javascript Object. – Yogesh Jan 02 '14 at 02:28
  • I'm not sure what you mean. According to w3schools.com, this is a JSON object: `var JSONObject= { "name":"John Johnson", "street":"Oslo West 555", "age":33, "phone":"555 1234567"};` it's the same syntax as object literal notation so what's the distinguishing difference? – ryanhagz Jan 02 '14 at 02:32
  • Then w3schools is wrong, that's just a regular javascript object, JSON is a string in javascript. – adeneo Jan 02 '14 at 02:33
  • It looks like what you're really asking is [how the new keyword works](http://stackoverflow.com/questions/1646698/what-is-the-new-keyword-in-javascript) – adeneo Jan 02 '14 at 02:35
  • 1
    Stop reading w3schools. They are notorious for bad advice, misnomers, etc. Further, they have no affiliation with the W3C and refuse to make that clear. – JAAulde Jan 02 '14 at 02:36
  • @adeneo, you're correct--JSON is a string in JavaScript. To be more precise, it is a specially formatted string to represent a serialized data structure in any language. – JAAulde Jan 02 '14 at 02:39

2 Answers2

5

You have big misconception about what JSON really is.

This

var bro = {
  'name': "chad", 
  'age': 22
};

is javascript assignment operator with object literal. It is NOT JSON, however JSON representation of bro object would look surprisingly similar:

{ "name":"chad","age":22}

up to the tabs and spaces.

So real instantiation of object out of JSON in javascript would be

var bro = JSON.parse('{ "name":"chad","age":22}')

In order to better understand what is going on read this MDN article and this old John Resig's article on simple inheritance. Most likely it will answer your question

Ja͢ck
  • 170,779
  • 38
  • 263
  • 309
vittore
  • 17,449
  • 6
  • 44
  • 82
0

JSON is a very limited subset of JavaScript and can contain only data, not executable code. If you want to execute code for each object of certain types, you will have to manage that yourself.

One solution is to give each JSON object a type property:

// JSON data:
[{"type":"Book", "title"="Of Mice And Men", "author": { "type":"Dude", "name"="John Steinbeck" }}, ...]

and define "constructor" functions for each type:

var constructorsMap = {};
constructorsMap[ "Book" ] = function(o){ /* Book constructor code */ }
constructorsMap[ "Dude" ] = function(o){ /* Dude constructor code */ }

Then scan through each object after parsing it, to apply the "constructor" function after the fact:

var books = JSON.parse( data );// the book data above
applyConstructor( books );

Where applyConstructor is defined something like:

function applyConstructor(o)
{
   for( var p in o )
   {
     if( typeof o[p] == "object" )
     {
        applyConstructor(o[p]);
     }
     if( o.hasOwnProperty("type") )
     {
        if( typeof constructorsMap[o.type] == "function" )
        {
           constructorsMap[ o.type ].call( o, constructorsMap[o.type] );
        }
     }
   }
}
Peter Hall
  • 53,120
  • 14
  • 139
  • 204