Exemplo n.º 1
0
        public static void GetNextString(string json, ref int index, out Substring substring)
        {
            if (json[index] != '\"')
            {
                throw new Exception("No string here...");
            }

            index++;
            int startingIndex = index;

            while (index < json.Length)
            {
                var character = json[index];
                if (character == '\\')
                {
                    BuildNextStringFromEscaped(json, ref index, startingIndex, out substring);
                    return;
                }
                else if (character == '\"')
                {
                    int stringLength = index - startingIndex;
                    substring = new Substring(json, startingIndex, stringLength);
                    index++;
                    return;
                }
                else
                {
                    index++;
                }
            }

            throw new Exception("No ending quote to string.");
        }
Exemplo n.º 2
0
        private static void BuildNextStringFromEscaped(string json, ref int index, int startingIndex, out Substring substring)
        {
            int lengthFromStartToFirstBackslash = index - startingIndex;
            var stringBuilder = new StringBuilder(json, startingIndex, lengthFromStartToFirstBackslash, (int)(1.6f * lengthFromStartToFirstBackslash)); ;
            
            while (index < json.Length)
            {
                var character = json[index];
                if (character == '\\')
                {
                    var indexOfBackslashForEscaping = index;
                    index++;
                    character = json[index];

                    switch (character)
                    {
                        case '\'':
                        case '\"':
                        case '\\':
                        case '/':
                            stringBuilder.Append(character);
                            index++;
                            break;
                        case 'n':
                            stringBuilder.Append('\n');
                            index++;
                            break;
                        case 'r':
                            stringBuilder.Append('\r');
                            index++;
                            break;
                        case 'f':
                            stringBuilder.Append('\f');
                            index++;
                            break;
                        case 'b':
                            stringBuilder.Append('\b');
                            index++;
                            break;
                        case 't':
                            stringBuilder.Append('\t');                    
                            index++;
                            break;
                        case 'u':
                            {
                                const int numberOfHexCharacterForJsonEncodedUtfCharacter = 4;
                                
                                index++;

                                var hexValue = HexSerializer.Parse(json, ref index, numberOfHexCharacterForJsonEncodedUtfCharacter);

                                var unicodeCharacter = (char)hexValue;
                                stringBuilder.Append(unicodeCharacter);
                            }
                            break;

                        default:
                            throw new Exception("Invalid escape character");
                    }
                }
                else if (character == '\"')
                {
                    string jsonWithEscapedCharactersRemoved = stringBuilder.ToString();
                    substring = new Substring(jsonWithEscapedCharactersRemoved);
                    
                    index++;
                    
                    return;
                }
                else
                {
                    stringBuilder.Append(character);
                    index++;
                }
            }

            throw new Exception("No ending quote to string.");
        }