escaped characters

Nov 26, 2008 at 2:15 AM
Hi Karl,

When deserializing a JSON string generated by another application the parser choked on the character sequence '\/'
(aka backslash forwardslash). Checking on the JSON site http://www.json.org/ revealed that this indeed a valid sequence and
should result in a forward slash. I have fixed this in a local copy of the code. While doing so I also took the opportunity
to handle the \u four-hex-digits  case as well. The code I added is in JsonReader.cs as follows...
----------------- a new function -----------------

        private bool IsHexDigit(char x)
        {
            return   (x >= '0' && x <= '9')
                   ||(x >= 'a' && x <= 'f')
                   ||(x >= 'A' && x <= 'F');       
        }
---------------- in ReadString changed the 'isEscaped' path to read ---------------
                if (isEscaped)
                {
                    // hex character e.g. \uFFFF
                    if (c == 'u')
                    {
                        StringBuilder sb2 = new StringBuilder();
                        for (int i = 0; i < 4; i++)
                        {
                            char c2 = Read();
                            if (!IsHexDigit(c2)) throw new JsonException(string.Format("Expected hex digit but got: '{0}'", c2));
                            sb2.Append(c2);
                        }
                        sb.Append((char)int.Parse(sb2.ToString(), System.Globalization.NumberStyles.HexNumber));
                    }
                    else
                    {
                        sb.Append(FromEscaped(c));
                    }
                    isEscaped = false;
                    continue;
                }
------------------------- and changed the FromEscaped function to handle one extra case -----------
       public virtual string FromEscaped(char c)
        {
            switch (c)
            {
                case '"':
                    return "\"";
                case '\\':
                    return "\\";
                case '/':
                    return "/";
                case 'b':
                    return "\b";
                case 'f':
                    return "\f";
                case 'r':
                    return "\r";
                case 'n':
                    return "\n";
                case 't':
                    return "\t";
                default:
                    throw new ArgumentException("Unrecognized escape character: " + c);
            }
        }
----------------------------------------------------------------------------------------

I haven't looked into whether or not the serializing code would need any matching changes.


Cheers Greg
Coordinator
Mar 24, 2009 at 2:47 PM
Sorry about the *huge* delay.  Your change was implemented in the trunk. Hopefully I'll bundle up a new release shortly.