I am wrapping another API and need to mirror it’s output format. I have a tricky field containing unstructured data that I wrote a custom converter for in Newtonsoft to get it to parse. I initially made the property an ExpandoObject which worked fine but the typescript DTO generation doesn’t make the type any and I have to manually change it each time I generate DTO.
I found another post where you say it’s bad to return ExpandoObjects and to use Dictionary<string, object> instead but I am having some issues with that.
I have declared my property like so:
[JsonProperty("attrs")]
[JsonConverter(typeof(ArrayToNullConverter))]
public Dictionary<string, object>? Attrs { get; set; }
I am able to serialize/deserialize it fine with Newtonsoft but if I try to return the object on my endpoint the ServiceStack serializer turns it into a series of nessted empty arrays.
var rootObject = JsonConvert.DeserializeObject<RootObject>(data);
Console.WriteLine("Newtonsoft serialization");
Console.WriteLine(JsonConvert.SerializeObject(rootObject, Formatting.Indented));
Console.WriteLine("ServiceStack serialization");
Console.WriteLine(rootObject.ToJson());
Output
Newtonsoft serialization
{
"Blocks": [
{
"Name": "block1",
"Type": "type1",
"Attrs": {
"key1": "value1",
"key2": [
{
"nestedKey1": "nestedValue1",
"nestedKey2": "nestedValue2"
},
"value3",
"value4"
]
}
},
{
"Name": "block2",
"Type": "type2",
"Attrs": null
},
{
"Name": "block3",
"Type": "type3",
"Attrs": null
}
]
}
ServiceStack serialization
{"Blocks":[{"Name":"block1","Type":"type1","Attrs":{"key1":"value1","key2":[[[[]],[[]]],[],[]]}},{"Name":"block2","Type":"type2"},{"Name":"block3","Type":"type3"}]}
I think the issue is that nested objects and arrays are typed to Newtonsoft JObject or JArray which is causing problems but it also doesn’t return the property names.
I am trying to figure out how I can use JsConfig.SerializeFn to make it work but I can’t see how to do it. I always end up having to use an ExpandoObject and then looping over all properties to convert it to camel case.
Any help and advice appreciated.