I have a C# object. I want to serialize it to an array rather than a key-value map.
My C# class:
[JsonArray()]
public class Foo
{
[JsonProperty(Order = 1)]
public Boolean Bar = true;
[JsonProperty(Order = 2)]
public Boolean Baz = false;
}
The desired JSON output:
[true,false]
The actual JSON output (if I remove the JsonArray attribute at the top):
{"Bar":true,"Baz":false}
I have a large number of objects in a complex object hierarchy that I need to have serialized as an array rather than key-value map. I would prefer a solution that doesn't require that the thing doing the serialization know anything about what it is serializing. I want to be able to just call JsonConvert.SerializeObject(myThing) on a top level object and have all of the sub-objects serialize correctly due to attributes they have on themselves.
Currently, I have all of my objects implement IEnumerable and then I manually teach each one of them how to return the properties in the correct order but this causes confusion when working in C# because intellisense tries to offer me all of the LINQ operations on the object even though none of them are appropriate.
I believe it is possible, though I don't know how, to add an attribute to Foo that points at another class that knows how to serialize Foo. Something like [JsonConverter(FooSerializer)] and then FooSerializer would implement JsonConverter. However, I am struggling to find much information on how to use JsonConverter for serialization, most of the questions people ask seem to be related to deserialization.