

Convert JSON To C# classes - ronnier
http://json2csharp.com

======
random42
Cool. Whats is the use-case?

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.

~~~
lukencode
There are a couple of places in .net where json can be deserialized into a c#
class you provide, for example posting json to an asp.net mvc controller
action. I find it is a massive pain to go through and create the classes
manually.

~~~
random42
Just to clarify, you mean that a json object can be deserialized into an
_object_ of the c# class, right? or just the class definition (as done by the
site)?

If latter, how do you use the class definition generated dynamically? (without
the values, that is)

~~~
lukencode
Yeah an object of the class. Sorry about that.

~~~
random42
Thats OK. I have had the case of convert Json to Language Class objects myself
before.

------
6ren
Thinking on this further, you're really doing two things: extracting a schema
from an instance; and converting it to C# classes.

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)

------
6ren
Cute. Minor point: it doesn't check for invalid field/classnames, e.g.

    
    
        {"{ [ string" : ""}

~~~
jonathankeith
Thank you, 6ren. The service will be extended and the first feature on my list
is displaying erroneous submissions such as this.

------
igorgue
I did this in Python a while ago, but I was following JSON Schema [1]:

<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).

[1]: <http://groups.google.com/group/json-schema>

