88

Is it possible to serialize a .Net Dictionary<Key,Value> into JSON with DataContractJsonSerializer that is of the format:

{
  key0:value0,
  key1:value1,
  ...
}

I use Dictionary <K,V>, because there is not predefined structure of the inputs.

I'm interesting just for DataContractJsonSerializer result! I've already found a "Surrogate" example, but there is an additional "data" in the output, and if the dictionary <K, String> is, the escaping is false too.


I've found the solution, what a needed! First of all, a serializable "dictionary" class: (Of course, this sample works just in one way, but I dont't need deserialization)

[Serializable]
public class MyJsonDictionary<K, V> : ISerializable {
    Dictionary<K, V> dict = new Dictionary<K, V>();

    public MyJsonDictionary() { }

    protected MyJsonDictionary( SerializationInfo info, StreamingContext context ) {
        throw new NotImplementedException();
    }

    public void GetObjectData( SerializationInfo info, StreamingContext context ) {
        foreach( K key in dict.Keys ) {
            info.AddValue( key.ToString(), dict[ key ] );
        }
    }

    public void Add( K key, V value ) {
        dict.Add( key, value );
    }

    public V this[ K index ] {
        set { dict[ index ] = value; }
        get { return dict[ index ]; }
    }
}

Usage:

public class MainClass {
    public static String Serialize( Object data ) {
        var serializer = new DataContractJsonSerializer( data.GetType() );
        var ms = new MemoryStream();
        serializer.WriteObject( ms, data );

        return Encoding.UTF8.GetString( ms.ToArray() );
    }

    public static void Main() {
        MyJsonDictionary<String, Object> result = new MyJsonDictionary<String, Object>();
        result["foo"] = "bar";
        result["Name"] = "John Doe";
        result["Age"] = 32;
        MyJsonDictionary<String, Object> address = new MyJsonDictionary<String, Object>();
        result["Address"] = address;
        address["Street"] = "30 Rockefeller Plaza";
        address["City"] = "New York City";
        address["State"] = "NY";

        Console.WriteLine( Serialize( result ) );

        Console.ReadLine();
    }
}

And the result:

{
      "foo":"bar",
      "Name":"John Doe",
      "Age":32,
      "Address":{
         "__type":"MyJsonDictionaryOfstringanyType:#Json_Dictionary_Test",
         "Street":"30 Rockefeller Plaza",
         "City":"New York City",
         "State":"NY"
      }
   }
Aaaaaaaa
  • 2,015
  • 3
  • 22
  • 40
  • 7
    Any specific reason you want to limit to `DataContractJsonSerializer` ? Every time I've done a comparison (which is several times : I'm very "into" my serializers) this is the least preferable JSON tool for .NET; I always look at JavaScriptSerializer or JSON.Net – Marc Gravell Feb 01 '11 at 10:07
  • In what context are you using this? That is is this a WCF app, ASP.NET app or something else. Are you using the DataContractJsonSerilizer in such as way that you're calling the WriteObject yourself or is some framework doing it for you? – Shiv Kumar Feb 01 '11 at 10:44
  • What's not clear in your post is that you want 1 JSON object with properties (and their values) being the name/value pairs in the dictionary. Either that or something is wrong since it can't be a JSON array since you can't have a JSON array where each element in the array has different property names. So can you be clear about the actual format you're looking to get? – Shiv Kumar Feb 01 '11 at 11:00
  • Hi Shiv Kumar: I call the WriteObject, why? And in one run I have the elements with the same structure, but this structure is not predefined, that's why I can't do a class for it. The DataContractJsonSerializer has company-reasons... :) – Aaaaaaaa Feb 01 '11 at 12:06
  • @Shiv Kumar: Yes, it is definitely possible to have a JSON array with different property names. This actually happens every time you choose to omit properties with null values from the JSON. – erikkallen Feb 02 '11 at 09:19
  • 1
    Anyone figured out the opposite? As in, how to get it to deserialize `MyJsonDictionary` properly? – drzaus Nov 19 '13 at 22:12
  • 5
    [You shouldn't answer your own question in an edit](http://meta.stackexchange.com/questions/74101/what-to-do-when-op-answers-his-her-own-question-in-an-edit). Instead actually add an answer, please edit your question and add the answer, as an answer – Liam Dec 19 '13 at 12:48
  • Possible duplicate of [How do I convert a dictionary to a JSON String in C#?](https://stackoverflow.com/questions/5597349/how-do-i-convert-a-dictionary-to-a-json-string-in-c) – Michael Freidgeim Oct 09 '17 at 11:05

6 Answers6

81

Json.NET does this...

Dictionary<string, string> values = new Dictionary<string, string>();
values.Add("key1", "value1");
values.Add("key2", "value2");

string json = JsonConvert.SerializeObject(values);
// {
//   "key1": "value1",
//   "key2": "value2"
// }

More examples: Serializing Collections with Json.NET

James Newton-King
  • 48,174
  • 24
  • 109
  • 130
20

In .NET 5 and later, you can simply write:

using System;
using System.Collections.Generic;
                    
public class Program
{
    public static void Main()
    {
        Dictionary<string, string> values = new();
        values.Add("key1", "value1");
        values.Add("key2", "value2");

        string json = System.Text.Json.JsonSerializer.Serialize(values);
        Console.WriteLine(json);
    }
}

to get {"key1":"value1","key2":"value2"}.

No external dependency is needed.

MartyIX
  • 27,828
  • 29
  • 136
  • 207
15

use property UseSimpleDictionaryFormat on DataContractJsonSerializer and set it to true.

Does the job :)

Maziar Taheri
  • 2,288
  • 22
  • 27
7

I'm using out of the box MVC4 with this code (note the two parameters inside ToDictionary)

 var result = new JsonResult()
 {
     Data = new
     {
         partials = GetPartials(data.Partials).ToDictionary(x => x.Key, y=> y.Value)
     }
 };

I get what's expected:

{"partials":{"cartSummary":"\u003cb\u003eCART SUMMARY\u003c/b\u003e"}}

Important: WebAPI in MVC4 uses JSON.NET serialization out of the box, but the standard web JsonResult action result doesn't. Therefore I recommend using a custom ActionResult to force JSON.NET serialization. You can also get nice formatting

Here's a simple actionresult JsonNetResult

http://james.newtonking.com/archive/2008/10/16/asp-net-mvc-and-json-net.aspx

You'll see the difference (and can make sure you're using the right one) when serializing a date:

Microsoft way:

 {"wireTime":"\/Date(1355627201572)\/"}

JSON.NET way:

 {"wireTime":"2012-12-15T19:07:03.5247384-08:00"}
Simon_Weaver
  • 140,023
  • 84
  • 646
  • 689
2

Unfortunately, this is not currently possible in the latest version of DataContractJsonSerializer. See: http://connect.microsoft.com/VisualStudio/feedback/details/558686/datacontractjsonserializer-should-serialize-dictionary-k-v-as-a-json-associative-array

The current suggested workaround is to use the JavaScriptSerializer as Mark suggested above.

Good luck!

Justin Fyles
  • 150
  • 2
  • 11
1

The MyJsonDictionary class worked well for me EXCEPT that the resultant output is XML encoded - so "0" becomes "0030". I am currently stuck at .NET 3.5, as are many others, so many of the other solutions are not available to me. "Turns the pictures" upside down and realized I could never convince Microsoft to give me the format I wanted but...

string json = XmlConvert.DecodeName(xmlencodedJson);

TADA!

The result is what you would expect to see - regular human readable and non-XML encoded. Works in .NET 3.5.

Mark Z. Kumler
  • 231
  • 2
  • 4