2

With .NET 4.5 version of DataContractJsonSerializer and with help of DataContractJsonSerializerSettings.UseSimpleDictionaryFormat I can serialize dictionaries. So for example this dictionary:

var dic = new Dictionary<string, object> 
{
  { "Level", 3 },
  { "Location", "Catacomb" }
};

will be converted into nice JSON:

{
  "Level":3,
  "Location":"Catacomb"
}

But if I have another dictionary as value:

var dic = new Dictionary<string, object> 
{
    { "Level", 3 },
    { "Location", new Dictionary<string, object> 
        {
            { "Name", "Catacobms" }
        }
    }
};

the the resulted JSON looks really bad:

{
   "Level":3,
   "Location":[
      {
         "__type":"KeyValuePairOfstringanyType:#System.Collections.Generic",
         "key":"Name",
         "value":"Catacobms"
      }
   ]
}

Is there any way to fix this problem?

PS: I know that there are other good JSON serializers, but in this case I need to use DataContractJsonSerializer.

dbc
  • 104,963
  • 20
  • 228
  • 340
Aleksandr Ivanov
  • 2,778
  • 5
  • 27
  • 35

1 Answers1

0

Try setting the EmitTypeInformation property on the serializer to EmitTypeInformation.Never

See MSDN Entry

Haney
  • 32,775
  • 8
  • 59
  • 68