2 ms·
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
by 6ren 16y ago
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)