J
J
JackBoner2018-08-08 09:17:43
JSON
JackBoner, 2018-08-08 09:17:43

How to add extra field when serializing JSON?

When serializing an object of any type, if the property of this object has a specific name, then you need to add an additional field and set its value.
Example:

class MyClass
{
    public object Item;
}

You should get the following json
{
    "Item" : {},
    "Item_Type" : "123"
}

It is also necessary to change the Item value during deserialization depending on the Item_Type, which is not described by the class and exists only in json.
Such objects can be nested, contained in arrays, and there are a lot of such types. Therefore, setting up a serialization attribute for each field is not the best option
. Newton JSON.net serializer is used

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
Peter, 2018-08-08
@petermzg

Most likely you need to use a custom class from JsonConverter with an overridden WriteJson method

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question