escaped characters

Nov 26, 2008 at 3: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 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));
                        sb.Append((char)int.Parse(sb2.ToString(), System.Globalization.NumberStyles.HexNumber));
                    isEscaped = false;
------------------------- 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";
                    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
Mar 24, 2009 at 3:47 PM
Sorry about the *huge* delay.  Your change was implemented in the trunk. Hopefully I'll bundle up a new release shortly.