Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is there a JConstructor?

Json.NET defines a JConstructor type.

This is confusing, because (to the best of my knowledge) constructors are not part of JSON. I double-checked the JSON spec and browsed json.org but couldn't find anything. There also doesn't seem to be much documentation about this type anywhere on the web.

Because Json.NET is so widely used (it is even cosigned by Microsoft) I assume there has to be some reasonable motivation for including this representation in the object model. The problem is, any attempt on my part to determine that motivation is nothing but speculation.

I tested out the type and its serialization, and the apparent behavior is to just wrap JavaScript code such as new constructorName(...), e.g.:

new JConstructor("ctorExample", 
    new JValue("value"), 
    new JObject(
        new JProperty("prop1", new JValue(1)), 
        new JProperty("prop2", new JValue(2)))
        )
.ToString()

outputs

 new ctorExample( 
   "value", 
   { 
     "prop1": 1, 
     "prop2": 2 
   } 
 ) 

So, what is the JConstructor type intended to represent and why does it exist?

like image 394
smartcaveman Avatar asked Aug 17 '14 16:08

smartcaveman


People also ask

Why do constructors exist?

A constructor is a special function that creates and initializes an object instance of a class. In JavaScript, a constructor gets called when an object is created using the new keyword. The purpose of a constructor is to create a new object and set values for any existing object properties.

Why is a constructor important?

Importance of constructorsConstructors are used to initialize the objects of the class with initial values. Constructors are invoked automatically when the objects are created. Constructors can have default parameters. If constructor is not declared for a class , the C++ compiler generates a default constructor.

What is a constructor and why do we need it?

A constructor is a special method of a class or structure in object-oriented programming that initializes a newly created object of that type. Whenever an object is created, the constructor is called automatically.

Is a constructor always needed?

You don't have to provide any constructors for your class, but you must be careful when doing this. The compiler automatically provides a no-argument, default constructor for any class without constructors. This default constructor will call the no-argument constructor of the superclass.


1 Answers

Json.NET includes many features which are not part of JSON specification. In particular, it allows parsing some JSON files which are "officially" invalid. This includes unquoted properties, comments, constructors etc. It includes serialization of references and many other features. Some features, like comments or unquoted properties, can later become part of the standard.

JConstructor allows producing code to be used by JavaScript apps. Data serialized this way is invalid JSON, but valid JavaScript code. Such JSON files cannot be parsed using JSON.parse method, but eval will be able to handle them. In some cases it may be useful, it's probably bad practice though, mostly useful for backwards compatibility with existing JS scripts, so this is probably the reason it's not advertised.

like image 104
Athari Avatar answered Sep 19 '22 19:09

Athari