-1

I have an ISerializer interface with several different implementations and a simple contract:

string Serialize<T>(T data);
T Deserialize<T>(string serializedData);

One of the implementations is a JSON serializer. I'm debating how it should behave if the value passed into either of these methods is null. Should it return null? Or empty string? I realize this is a design decision, I'm curious how others have handled it and what kind of gotchas or design considerations that should factor into my decision? Thanks.

snappymcsnap
  • 2,050
  • 2
  • 29
  • 53
  • related if not duplicate: http://stackoverflow.com/questions/21120999/representing-null-in-json – Habib Nov 11 '15 at 18:33
  • @Habib No it's not a duplicate. I'm not asking how to handle a PROPERTY that is null when converting to/from JSON, I'm asking what to do when the whole object is null – snappymcsnap Nov 11 '15 at 18:37

1 Answers1

0

If an object is null, then it should be serialized to JSON null and deserialized to C# null (and the reverse).

You're going to run into a problem though if T is a non-nullable type (e.g. int or Timespan). I'm not sure what to do there - usually the behavior (e.g. in json.net) is to return a zero value - for example, trying to deserialize null to int returns an int 0.