1

what is easiest way to serialize dictionary as list of its values into json using Json.NET?

Serialization is done by SignalR, not me. So I need some attribute-like way or custom contract resolver or something like that.

Dictionary is better for me on the server side but for the binding into data grid on the client, flatten object would be better.

Thanks

dbc
  • 104,963
  • 20
  • 228
  • 340
Karel Bém
  • 97
  • 8
  • Looks like this may not be a duplicate. To confirm can you share the JSON you want to generate and a [mcve] showing your c# calls to SIgnalR? Is your dictionary the root object or a nested object? – dbc Dec 19 '17 at 17:33
  • since I need only serialize object (and never deserialize it), custom JsonConverter did the trick – Karel Bém Dec 19 '17 at 21:24
  • public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) { if (value is Dictionary items) { serializer.Serialize(writer, items.Values); } } – Karel Bém Dec 19 '17 at 21:26

1 Answers1

2

Are you looking for something like that;

JsonConvert.SerializeObject(dictionary.Values.ToList());
lucky
  • 12,734
  • 4
  • 24
  • 46
  • sorry, my bad, I didn't mention that serialization is done by SignalR, not me. So I need some attribute-like way or custom contract resolver or something like that. – Karel Bém Dec 19 '17 at 13:25
  • 1
    Please add that statement to your original question, so nobody else spends minutes on an answer which isn't going to help anyone – Sebastian L Dec 19 '17 at 13:27