0

I've been using several serializers from .Net 4.x for a while and think I inherited them from .Net 3.5 times. I just realized that most of these serializers back then were not able to serialize and deserialize any C# Dictionary in general.

In the code I am refactoring right now, I see workarounds like streaming out a List<T> instead of a Dictionary<string, T> (which I consider a work around and want o avoid and a rather use the built-in capabilities of a serializer).

What's the status on your favorite JSON serializer?

Also: do you know if your serializer is JSON API compliant? (JSONAPI.org)

dr. rAI
  • 1,773
  • 1
  • 12
  • 15
  • Possible duplicate of [How do I convert a dictionary to a JSON String in C#?](http://stackoverflow.com/questions/5597349/how-do-i-convert-a-dictionary-to-a-json-string-in-c) – Vadim Martynov Mar 23 '16 at 21:28
  • Use `ToList()` on the dictionary to transform into a `List>` – Alexander Derck Mar 24 '16 at 15:47
  • Thanks Alex. That's probably a fair work around if the serializer is not doing something like this out of the box. This question was meant more about experiences with Serializers that do indeed support Dictionary serialization. And: I used this technique a lot when serializing XML and didn't like it too much because of the KeyValuePair "clutter" that it generated in the output file. Not with JSON I suppose. Worth a try. Thanks again. – dr. rAI Mar 30 '16 at 20:31

2 Answers2

0

http://www.newtonsoft.com/json is the one I've used the most. One of the faster and very flexible. It is the default serializer used by asp.net MVC/WebAPI

https://github.com/kevin-montrose/Jil is used by StackOverflow and is very fast.

Jsonapi is a higher level concern. It does not change how json is de/serialized. It is just a pattern, a convention, a standard, a schema, for the shape/format to return your data in. It does not have much bearing on which serializer to choose.

AndyPook
  • 2,762
  • 20
  • 23
  • Thanks for pointing me to Jil. Looks interesting and could be a good match for me in the current project. – dr. rAI Mar 30 '16 at 20:10
  • Jsonapi: yes, you are right. It seems to go further by conventions about how to store/handle references/relationships and then some. Have to see how I get my WebService Layer to work with the standard JSON API adapter for Ember. – dr. rAI Mar 30 '16 at 20:15
0

After many years of experience of different serializers and trying to get them to cope with all sort of collection types, I have come to a conclusion:

Often it is better to convert my complex objects, into simple DTO (data transfer objects) that JUST have simple public properties (or fields), with arrays for all “collections”. And then ALL serializers can cope, including the serializers I don’t yet know about, but will have to port to next year….

In other words, write more code, but at least it is code that I can write in my sleep! Automapper can also help with reducing the amount of custom code needed to create the DTOs.

Ian Ringrose
  • 51,220
  • 55
  • 213
  • 317
  • Thanks for your comment, Ian. It's probably a bit philosophical which approach to use. A DTO layer for every internal class that I want to make persistent has it's charm. With C# classes being serialized to XML I was a fan of this approach. Currently I am a bit more in favor to use the C# classes as they are - as well on the client side/JavaScript end. – dr. rAI Mar 30 '16 at 20:32