Convert JSON To C# classes
json2csharp.com
json2csharp.com
Edit - I hate to bitch about downvotes (as per http://ycombinator.com/newsguidelines.html), but can the downvoters to my comments in the thread, please respond, why the downvotes? Are my comments Off-Topic, rude or not adding to the discussion? Since, I am genuinely curious about all my queries.
If latter, how do you use the class definition generated dynamically? (without the values, that is)
Is it possible to create class dynamically in C#, or is it supposed to be one time effort (and the tool does it automatically it, as opposed to doing it manually)?
1) extracting the schema from an instance: in a JSON instance, a list will have some number of elements; but in a schema, you just have the concept of a list, and what it may possibly contain. Because JSON doesn't explicitly facilitate choice or references, they can't be automatically extracted (in the rare cases where someone implements this, they do it in their own non-standard way on top of JSON). JSON also doesn't represent the idea of productions - fair enough, since productions are a schema/grammar-level concept, which simply don't appear in an instance. (In general, there are many different ways of dividing up a grammar into production that yield identical languages ie. the set of possible instances.)
2) representing this schema in C# classes. This is conceptually straightforward, once you've got the schema.
In practice, these two steps almost disappear, even conceptually, because (with the exception of lists), you are just making classes that have the equivalent contents to each object in the JSON instance.
i.e. you can see it as an isomorphism:
mapping from JSON "objects" (within {}) to C# classes; and
recursively within that, from
JSON "fields" to C# fields and
JSON values (int, boolean, string etc) to C# primitive types.
(BTW: The mapping of objects to classes is instance-to-schema.)Lists are an exception to this isomorphism:
mapping a JSON list (within []) to a C# list/array,
the _elements_ of a JSON list are not _all_ mapped -
only one is mapped (eg. a JSON "object" is mapped to a class) {"{ [ string" : ""}https://github.com/igorgue/py2json
Its use was very internal and to be hones we would have just pickle or unpickle Python objects and it'd have been the same result (for our use case).