Mar 27, 2011 at 4:00 PM

Currently DynamicJson does not output valid JSON when enum types are involved. GetJsonType() returns JsonType.Number (as Type.GetTypeCode() returns one of the numeric types), and CreateJsonNode() returns as-is enum value.

DataContractJsonSerializer does not handle enums well either, and serializes enums into integer values.

I have two solutions, though both are incomplete:

  • serialize as string: in GetJsonType(), return JsonType.@string when obj.GetType().IsEnum is true.
  • serialize as int: in CreateJsonNode(), return Convert.ToInt64(obj) when (obj is Enum) is true.

They both break roundtrip scenarios. Though current JSON output is invalid anyway, I'm not really sure if there were such scenarios.

Any good idea?

Mar 27, 2011 at 5:04 PM

Thank you for a report.

I think that I had better convert enum as string than int.
The first of your solution looks good.

There is a problem for mutual conversion, but thinks that there is no help for it.
It is the first concept in DynamicJson to look like Json which is pure, that there is.

I consider it a little more and revise it by the next release.