pederOverland
pederOverland

Reputation: 890

Strange results serializing a dictionary to JSON

I am currently looking into using WCF for REST services. A problem I ran into is serializing results that are dictionaries. I used the wrapper class suggested in this post to serialize a dictionary consisting of string represented dates (e.g. "20.10.2011") and bools. When I test the result I see this:

{
    "DeparturesResult":
    {
        "_x0032_1.10.2011":true,
        "_x0032_4.10.2011":true,
        "_x0032_6.10.2011":true,
        "_x0032_8.10.2011":true,
        "_x0033_1.10.2011":true
    }
}

..the first character in every key is written out as a UTF-8 code. I don't encounter this problem if I prepend the string with a letter. (e.g. d21.10.2011)

Here is the code I am using to serialize the dictionary: public class FlService : IFlService { #region IFlService Members

    public AjaxDictionary<string, bool> Departures(string from, string to, string portFrom, string portTo)
    {
        var startDate = DateTime.Today; // DateTime.ParseExact(from, "dd.MM.yyyy", null);
        var endDate = DateTime.ParseExact(to, "dd.MM.yyyy", null);
        var client = new Timetables();
        var result = client.GetJourneys(startDate, endDate.AddDays(1), portFrom, portTo);
        var js = result
            .GroupBy(x => x.DepartureTime.CarResToDateTime())
            .Select(x => x.Key)
            .OfType<DateTime>()
            .Select(x => x.Date)
            .Distinct()
            .ToDictionary(x => x.Date.ToString("dd.MM.yyy"), x => true);
        return new AjaxDictionary<string, bool>(js);
    }

    #endregion

    #region Nested type: AjaxDictionary

    [Serializable]
    public class AjaxDictionary<TKey, TValue> : ISerializable
    {
        private readonly Dictionary<TKey, TValue> _dictionary;

        public AjaxDictionary()
        {
            _dictionary = new Dictionary<TKey, TValue>();
        }

        public AjaxDictionary(Dictionary<TKey, TValue> dic)
        {
            _dictionary = dic;
        }

        public AjaxDictionary(SerializationInfo info, StreamingContext context)
        {
            _dictionary = new Dictionary<TKey, TValue>();
        }

        public TValue this[TKey key]
        {
            get { return _dictionary[key]; }
            set { _dictionary[key] = value; }
        }

        #region ISerializable Members

        public void GetObjectData(SerializationInfo info, StreamingContext context)
        {
            foreach (var key in _dictionary.Keys)
                info.AddValue(key is string ? key as string : key.ToString(), _dictionary[key]);
        }

        #endregion

        public void Add(TKey key, TValue value)
        {
            _dictionary.Add(key, value);
        }
    }

    #endregion
}

Edit: adding a comment to the accepted answer: This is true for javascript, but not as I can see for JSON. Looks like the serializer is a little fanatic in not only serializing to JSON, but also ensuring that the JSON is javascript. Adding the full JSON syntax as found on https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/JSON

JSON = null
    or true or false
    or JSONNumber
    or JSONString
    or JSONObject
    or JSONArray

JSONNumber = - PositiveNumber
          or PositiveNumber
PositiveNumber = DecimalNumber
              or DecimalNumber . Digits
              or DecimalNumber . Digits ExponentPart
              or DecimalNumber ExponentPart
DecimalNumber = 0
             or OneToNine Digits
ExponentPart = e Exponent
            or E Exponent
Exponent = Digits
        or + Digits
        or - Digits
Digits = Digit
      or Digits Digit
Digit = 0 through 9
OneToNine = 1 through 9

JSONString = ""
          or " StringCharacters "
StringCharacters = StringCharacter
                or StringCharacters StringCharacter
StringCharacter = any character
                  except " or \ or U+0000 through U+001F
               or EscapeSequence
EscapeSequence = \" or \/ or \\ or \b or \f or \n or \r or \t
              or \u HexDigit HexDigit HexDigit HexDigit
HexDigit = 0 through 9
        or A through F
        or a through f

JSONObject = { }
          or { Members }
Members = JSONString : JSON
       or Members , JSONString : JSON

JSONArray = [ ]
         or [ ArrayElements ]
ArrayElements = JSON
             or ArrayElements , JSON

Upvotes: 3

Views: 570

Answers (1)

Ankur
Ankur

Reputation: 33637

This is because in JSON (or JavaScript) the key name (or property in JavaScript) cannot start with a numeric value, hence it put those character after serializing them to make sure they are correct JSON format

Upvotes: 6

Related Questions