0

I have a class with multiple properties. One property containes properly formed JSON.

Something like:

public MyClass {
    public string Val {get;set;}
    public string AlreadyJson {get;set;} // properly formed JSON
}

Now I need to send this object to some external api using PostAsync. To do this I have to serialize MyClass object to JSON first. However, as AlreadyJSON property is already serialized (it is already json), I need to ignore it (by ignore I mean - I still have to include it into resulting json, but I do not need to serialize it).

The same should apply to deserialization. I believe it should be easy with some attribute to MyClass, but unfourtunately I could not achieve this. For instance, adding JsonIgnore would simple ignore property and would not include it into resulting string.

Any solution would be good: using Newtonsoft or using built in DataContractJsonSerializer

renathy
  • 5,125
  • 20
  • 85
  • 149
  • 2
    You would need a custom serializer for this, or you would have to change the type of the property to one of the Json.Net types that can hold dynamic object hierarchies. You could also do this by adding another property handling the serialization. See this [gist](https://gist.github.com/lassevk/909a85740fd9372e28ab865a45c11d92) for an example. – Lasse V. Karlsen Jul 11 '18 at 10:52
  • You could write a (NewtonSoft) custom converter and place the _JsonConverter_ attribute on that property, using your custom converter. https://www.newtonsoft.com/json/help/html/CustomJsonConverter.htm – RoelA Jul 11 '18 at 12:09

0 Answers0