private static object Deserialize(JsonReaderBase reader) { reader.SkipSpace(); switch (reader.Peek()) { case '[': return(DeserializeArray(reader)); case '{': return(DeserializeObject(reader)); case '"': return(DeserializeString(reader)); case 't': // true if (reader.ReadStr(4) != "true") { throw new FormatException(); } return(true); case 'f': // false if (reader.ReadStr(5) != "false") { throw new FormatException(); } return(false); case 'n': // null if (reader.ReadStr(4) != "null") { throw new FormatException(); } return(null); case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': return(DeserializeNumber(1, reader)); case '-': reader.ReadChar(); return(DeserializeNumber(-1, reader)); default: throw new FormatException(); } }
private static string DeserializeString(JsonReaderBase reader) { if (reader.ReadChar() != '"') { throw new FormatException(); } var sb = new StringBuilder(); while (!reader.IsEnd) { char c = reader.ReadChar(); if (c == '"') { break; } if (c == '\\') { char c2 = reader.ReadChar(); switch (c2) { case 'b': sb.Append('\b'); break; case 'f': sb.Append('\f'); break; case 'n': sb.Append('\n'); break; case 'r': sb.Append('\r'); break; case 't': sb.Append('\t'); break; case 'u': { var s = reader.ReadStr(4); var ch = char.ConvertFromUtf32(Int32.Parse(s, NumberStyles.AllowHexSpecifier, CultureInfo.InvariantCulture)); sb.Append(ch); } break; case '"': case '\\': case '/': sb.Append(c2); break; default: throw new FormatException("不正なエスケープシーケンスが見つかりました。"); } } else { sb.Append(c); } } return(sb.ToString()); }