Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You may create a DTO before serializing.</p> <p>I use a class like: (pseudo code)</p> <pre><code>class JsonDto string Content {get;set;} string Type {get;set;} ctor(object) =&gt; sets Content &amp; Type Properties static JsonDto FromJson(string) // =&gt; Reads a Serialized JsonDto // and sets Content+Type Properties string ToJson() // =&gt; serializes itself into a json string object Deserialize() // =&gt; deserializes the wrapped object to its saved Type // using Content+Type properties T Deserialize&lt;T&gt;() // deserializes the object as above and tries to cast to T </code></pre> <p>Using the <code>JsonDto</code> you can easily serialize arbitrary objects to JSON and deserialize them to their common base type because the deserializer will always know the original type and returns an type of object reference which will be casted if you use the generic <code>Deserialize&lt;T&gt;</code> method.</p> <p>One caveat: If you set the <code>Type</code> property you should use the AssemblyQualifiedName of the type, however without the version attribute (ex: <code>MyCompany.SomeNamespace.MyType, MyCompany.SomeAssembly</code>). If you just use the <code>AssemblyQualifiedName</code> property of the <code>Type</code> class you will end up with errors if your assembly version changes.</p> <p>I implemented a <code>JsonDtoCollection</code> the same way, which derives from <code>List&lt;JsonDto&gt;</code> and provides methods to handle collections of objects.</p> <pre><code>class JsonDtoCollection : List&lt;JsonDto&gt; ctor(List&lt;T&gt;) =&gt; wraps all items of the list and adds them to itself static JsonDtoCollection FromJson(string) // =&gt; Reads a collection of serialized // JsonDtos and deserializes them, // returning a Collection string ToJson() // =&gt; serializes itself into a json string List&lt;object&gt; Deserialize() // =&gt; deserializes the wrapped objects using // JsonDto.Deserialize List&lt;T&gt; Deserialize&lt;T&gt;() // deserializes the as above and tries to cast to T </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload