0

This is the class I want to custom serialize:

public class MyClass
{
    public string Key {get; set;}
    public IEnumerable<string> Value {get; set;}
}

If I've got a list of it, normal serialization would produce the following output:

[
    {
        "Key": "MyKey1",
        "Value": [
            "Value1",
            "Value2"
        ]
    },
    {
        "Key": "MyKey2",
        "Value": [
            "Value3",
            "Value4"
        ]
    }
]

What I want is to remove the property-names from the json-result like so:

[    
    {
        "MyKey1": [
            "Value1",
            "Value2"
        ]
    },
    {
        "MyKey2": [
            "Value3",
            "Value4"
        ]
    }
]

So i decided to implement a custom JsonConverter like so:

public class MyClassJsonConverter : JsonConverter<MyClass>
{
    public override void Write(Utf8JsonWriter writer, MyClass value, JsonSerializerOptions options)
    {
        //custom code goes here
        writer.WriteStartArray(value.Key);

        foreach (var val in value.Value)
            writer.WriteStringValue(val);

        writer.WriteEndArray();
    }
}

But this produces invalid json:

[
    "MyKey1": [
        "Value1",
        "Value2"
    ],
   "MyKey2": [
       "Value3",
       "Value4"
    ]
]

How to resolve this?

Pavel Anikhouski
  • 21,776
  • 12
  • 51
  • 66
UNeverNo
  • 549
  • 3
  • 8
  • 29
  • 1
    You should write start and end elements – Pavel Anikhouski Jun 04 '20 at 11:37
  • Why don't you consider using a`Dictionary>` data structure? – Ashish Jun 04 '20 at 11:55
  • `Dictionary> p = new Dictionary>();` `p.Add("MyKey1", new List { "Value1", "Value2" });` `p.Add("MyKey2", new List { "Value3", "Value4" });` `Console.WriteLine(JsonConvert.SerializeObject(p));` Output: `{"MyKey1":["Value1","Value2"],"MyKey2":["Value3","Value4"]}` – Ashish Jun 04 '20 at 11:58

1 Answers1

3

You should use WriteStartObject() before writing the array and WriteEndObject() after

public override void Write(Utf8JsonWriter writer, MyClass value, JsonSerializerOptions options)
{
    writer.WriteStartObject();

    //custom code goes here
    writer.WriteStartArray(value.Key);

    foreach (var val in value.Value)
        writer.WriteStringValue(val);

    writer.WriteEndArray();
    writer.WriteEndObject();
}

Then the following code

var list = new List<MyClass>
{
    new MyClass { Key = "MyKey1", Value = new[] { "Value1", "Value2" } },
    new MyClass { Key = "MyKey2", Value = new[] { "Value3", "Value4" } }
};

var options = new JsonSerializerOptions();
options.Converters.Add(new MyClassJsonConverter());
var json = JsonSerializer.Serialize(list, options);

gives you the expected JSON

[
    {
        "MyKey1": [
            "Value1",
            "Value2"
        ]
    },
    {
        "MyKey2": [
            "Value3",
            "Value4"
        ]
    }
]
Pavel Anikhouski
  • 21,776
  • 12
  • 51
  • 66