title: Returning Dictionary via WCF
body: <p>I'm trying to output a <code>Dictionary</code> via <code>WCF</code> as <code>JSON</code>. The first problem I had that the <code>JSON</code> was riddled with the 'Key' 'Value' elements, but I fixed this by using my own <code>JsonDictionary</code> (code from StackOverflow).</p>
  
  <p>What I now get is:</p>
  
  <pre><code>{
      "_x0030_": {
          "__type": "Car:#WcfService",
          "Brand": "Ford",
          "Model": "Focus"
      },
      "_x0031_": {
          "__type": "Car:#WcfService",
          "Brand": "Renault",
          "Model": "Megane"
      }
  }
  </code></pre>
  
  <p>The <code>__type__</code> doesn't concern me (great that it can be removed, but not my focus currently). The problem is the index. The integer is converted to '<em>x0030</em>' for 0 for example.
  I would expect to have:</p>
  
  <pre><code>{
      "0": {
          "__type": "Car:#WcfService",
          "Brand": "Ford",
          "Model": "Focus"
      },
      "1": {
          "__type": "Car:#WcfService",
          "Brand": "Renault",
          "Model": "Megane"
      }
  }
  </code></pre>
  
  <p>I see on <a href="http://msdn.microsoft.com/en-us/library/bb412170.aspx" rel="nofollow">MSDN</a> that this is default behavior, but no solution how to prevent this. Any ideas?</p>
  
  <p>My code:</p>
  
  <pre><code>//Interface
  [WebGet(UriTemplate = "car", ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Bare)]
  JsonDictionary&lt;int, Car&gt; GetCars();
  
  //Implementation
  public class CarService : ICarService
  {
      private JsonDictionary&lt;int, Car&gt; cars = new JsonDictionary&lt;int, Car&gt;();
      private int lastIndex = 0;        
  
      public CarService()
      {
          cars.Add(lastIndex++, new Car("Ford", "Focus"));
          cars.Add(lastIndex++, new Car("Renault", "Megane"));
      }
  
      public JsonDictionary&lt;int, Car&gt; GetCars()
      {
          return cars;
      }
  }
  
  [DataContract]
  public class Car
  {
      public Car(string brand, string model)
      {
          this.Brand = brand;
          this.Model = model;
      }
  
      [DataMember]
      public string Brand { get; private set; }
  
      [DataMember]
      public string Model { get; private set; }
  }
  
  [Serializable]
  [KnownType(typeof(Car))]
  public class JsonDictionary&lt;TKey, TValue&gt; : ISerializable
  {
      public Dictionary&lt;TKey, TValue&gt; dictionary;
  
      public JsonDictionary()
      {
          dictionary = new Dictionary&lt;TKey, TValue&gt;();
      }
  
      public JsonDictionary(SerializationInfo info, StreamingContext context)
      {
          dictionary = new Dictionary&lt;TKey, TValue&gt;();
      }
  
      public TValue this[TKey key]
      {
          get { return dictionary[key]; }
          set { dictionary[key] = value; }
      }
  
      public void Add(TKey key, TValue value)
      {
          dictionary.Add(key, value);
      }
  
      public bool ContainsKey(TKey key)
      {
          return dictionary.ContainsKey(key);
      }
  
      public bool Remove(TKey key)
      {
          return dictionary.Remove(key);
      }
  
      public void GetObjectData(SerializationInfo info, StreamingContext context)
      {
          foreach (TKey key in dictionary.Keys)
          {
              info.AddValue(key.ToString(), dictionary[key]);
          }
      }
  }
  
  &lt;?xml version="1.0"?&gt;
  &lt;configuration&gt;
  
    &lt;appSettings&gt;
      &lt;add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" /&gt;
    &lt;/appSettings&gt;
    &lt;system.web&gt;
      &lt;compilation debug="true" targetFramework="4.5" /&gt;
      &lt;httpRuntime targetFramework="4.5"/&gt;
    &lt;/system.web&gt;
    &lt;system.serviceModel&gt;
      &lt;services&gt;
        &lt;service name="WcfService.CarService"&gt;
          &lt;endpoint 
            behaviorConfiguration="restfulBehaviour" 
            binding="webHttpBinding" 
            contract="WcfService.ICarService"&gt;
          &lt;/endpoint&gt;
          &lt;host&gt;
            &lt;baseAddresses&gt;
              &lt;add baseAddress="http://localhost/carservice"/&gt;
            &lt;/baseAddresses&gt;
          &lt;/host&gt;
        &lt;/service&gt;
      &lt;/services&gt;
      &lt;behaviors&gt;
        &lt;endpointBehaviors&gt;
          &lt;behavior name="restfulBehaviour"&gt;
            &lt;webHttp defaultOutgoingResponseFormat="Json"/&gt;
          &lt;/behavior&gt;
        &lt;/endpointBehaviors&gt;
        &lt;serviceBehaviors&gt;
          &lt;behavior&gt;
            &lt;serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/&gt;
            &lt;serviceDebug includeExceptionDetailInFaults="false"/&gt;
          &lt;/behavior&gt;
        &lt;/serviceBehaviors&gt;
      &lt;/behaviors&gt;
      &lt;protocolMapping&gt;
          &lt;add binding="basicHttpsBinding" scheme="https" /&gt;
      &lt;/protocolMapping&gt;    
      &lt;serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" /&gt;
    &lt;/system.serviceModel&gt;
    &lt;system.webServer&gt;
      &lt;modules runAllManagedModulesForAllRequests="true"/&gt;
      &lt;directoryBrowse enabled="true"/&gt;
    &lt;/system.webServer&gt;
    &lt;system.diagnostics&gt;
      &lt;sources&gt;
        &lt;source name="System.ServiceModel"
                switchValue="Information, ActivityTracing"
                propagateActivity="true"&gt;
          &lt;listeners&gt;
            &lt;add name="traceListener"
                type="System.Diagnostics.XmlWriterTraceListener"
                initializeData="d:\WebTrace.svclog"  /&gt;
          &lt;/listeners&gt;
        &lt;/source&gt;
      &lt;/sources&gt;
    &lt;/system.diagnostics&gt;
  &lt;/configuration&gt;
  </code></pre>