Mapping JSON to objects and vice versa
In order to consume data in JSON format inside .Net programs, the natural approach that comes to mind is to use JSON text to populate a new instance of a particular class; either a custom one, built to match the structure of the input JSON text, or a more general one which acts as a dictionary.
Conversely, in order to build new JSON strings from data stored in objects, a simple export–like operation sounds like a good idea.
For this purpose, LitJSON includes the JsonMapper
class,
which provides two main methods used to do JSON–to–object and
object–to–JSON conversions. These methods are
JsonMapper.ToObject
and JsonMapper.ToJson
.
Simple JsonMapper
examples
As the following example demonstrates, the ToObject
method has a generic
variant, JsonMapper.ToObject<T>
, that is used to specify the type of the
object to be returned.
1 |
|
Output from the example:
1 |
|
Using the non–generic variant of JsonMapper.ToObject
When JSON data is to be read and a custom class that matches a particular
data structure is not available or desired, users can use the non–generic
variant of ToObject
, which returns a JsonData
instance. JsonData
is a
general purpose type that can hold any of the data types supported by JSON,
including lists and dictionaries.
1 |
|
Output from the example:
1 |
|
Readers and Writers
An alternative interface to handling JSON data that might be familiar to
some developers is through classes that make it possible to read and write
data in a stream–like fashion. These classes are JsonReader
and
JsonWriter
.
These two types are in fact the foundation of this library, and the
JsonMapper
type is built on top of them, so in a way, the developer can
think of the reader and writer classes as the low–level programming
interface for LitJSON.
Using JsonReader
1 |
|
This example would produce the following output:
1 |
|
Using JsonWriter
The JsonWriter
class is quite simple. Keep in mind that if you want to
convert some arbitrary object into a JSON string, you’d normally just use
JsonMapper.ToJson
.
1 |
|
Output from the example:
1 |
|
Configuring the library’s behaviour
JSON is a very concise data–interchange format; nothing more, nothing less. For this reason, handling data in JSON format inside a program may require a deliberate decision on your part regarding some little detail that goes beyond the scope of JSON’s specs.
Consider, for example, reading data from JSON strings where single–quotes are used to delimit strings, or Javascript–style comments are included as a form of documentation. Those things are not part of the JSON standard, but they are commonly used by some developers, so you may want to be forgiving or strict depending on the situation. Or what about if you want to convert a .Net object into a JSON string, but pretty–printed (using indentation)?
To declare the behaviour you want, you may change a few properties from your
JsonReader
and JsonWriter
objects.
Configuration of JsonReader
1 |
|
The output would be:
1 |
|
Configuration of JsonWriter
1 |
|
The output from this example is:
1 |
|