private string ReadString() { #if NET35 _buffer = new StringBuilder(); #else _buffer.Clear(); #endif bool escaped = false; while (true) { int next = ReadNextChar(); if (next == -1 || next == '\n') { throw new JsonDeserializerException( JsonDeserializerResource.JSON_OpenString, _line, _column); } else if (escaped) { if ((next == '"') || (next == '\\') || (next == '/')) { _buffer.Append((char)next); } else if (next == 'b') { // '\b' backspace _buffer.Append('\b'); } else if (next == 'f') { // '\f' form feed _buffer.Append('\f'); } else if (next == 'n') { // '\n' line feed _buffer.Append('\n'); } else if (next == 'r') { // '\r' carriage return _buffer.Append('\r'); } else if (next == 't') { // '\t' tab _buffer.Append('\t'); } else if (next == 'u') { // '\uXXXX' unicode int unicodeLine = _line; int unicodeColumn = _column; #if NET35 _codePointBuffer = new StringBuilder(4); #else _codePointBuffer.Clear(); #endif for (int i = 0; i < 4; ++i) { next = ReadNextChar(); if (next == -1) { throw new JsonDeserializerException( JsonDeserializerResource.JSON_InvalidEnd, unicodeLine, unicodeColumn); } else { _codePointBuffer[i] = (char)next; } } try { int unicodeValue = int.Parse(_codePointBuffer.ToString(), NumberStyles.HexNumber, CultureInfo.InvariantCulture); _buffer.Append((char)unicodeValue); } catch (FormatException ex) { throw new JsonDeserializerException( JsonDeserializerResource.Format_InvalidUnicode(_codePointBuffer.ToString()), ex, unicodeLine, unicodeColumn); } } else { throw new JsonDeserializerException( JsonDeserializerResource.Format_InvalidSyntaxNotExpected("character escape", "\\" + next), _line, _column); } escaped = false; } else if (next == '\\') { escaped = true; } else if (next == '"') { break; } else { _buffer.Append((char)next); } } return(_buffer.ToString()); }
private static JsonObject DeserializeObject(JsonToken head, JsonBuffer buffer) { Dictionary <string, JsonValue> dictionary = new Dictionary <string, JsonValue>(); // Loop through each JSON entry in the input object while (true) { JsonToken next = buffer.Read(); if (next.Type == JsonTokenType.EOF) { throw new JsonDeserializerException( JsonDeserializerResource.Format_InvalidSyntaxExpectation("JSON object", '}'), next); } if (next.Type == JsonTokenType.Colon) { throw new JsonDeserializerException( JsonDeserializerResource.Format_InvalidSyntaxNotExpected("JSON object", ':'), next); } else if (next.Type == JsonTokenType.RightCurlyBracket) { break; } else { if (next.Type != JsonTokenType.String) { throw new JsonDeserializerException( JsonDeserializerResource.Format_InvalidSyntaxExpectation("JSON object member name", "JSON string"), next); } string memberName = next.Value; if (dictionary.ContainsKey(memberName)) { throw new JsonDeserializerException( JsonDeserializerResource.Format_DuplicateObjectMemberName(memberName), next); } next = buffer.Read(); if (next.Type != JsonTokenType.Colon) { throw new JsonDeserializerException( JsonDeserializerResource.Format_InvalidSyntaxExpectation("JSON object", ':'), next); } dictionary[memberName] = DeserializeInternal(buffer.Read(), buffer); next = buffer.Read(); if (next.Type == JsonTokenType.RightCurlyBracket) { break; } else if (next.Type != JsonTokenType.Comma) { throw new JsonDeserializerException( JsonDeserializerResource.Format_InvalidSyntaxExpectation("JSON object", ',', '}'), next); } } } return(new JsonObject(dictionary, head.Line, head.Column)); }