private static string _GetStringValue(CharReader reader) { // reader must be on starting quote char if (reader.Read() != '\"') { throw new ArgumentException("Invalid JSON string"); } StringBuilder result = new StringBuilder(); bool escaped = false; char c; while (true) { c = reader.Read(); if (escaped) { switch (c) { case '\"': case '\\': case '/': result.Append(c); break; case 'b': result.Append('\b'); break; case 'f': result.Append('\f'); break; case 'n': result.Append('\n'); break; case 'r': result.Append('\r'); break; case 't': result.Append('\t'); break; case 'u': string unicodeValue = $"{reader.Read()}{reader.Read()}{reader.Read()}{reader.Read()}"; result.Append(Convert.ToChar(Convert.ToUInt16(unicodeValue, 16))); break; default: throw new ArgumentException($"Invalid escaped char in string: {c}"); } escaped = false; continue; } // escape next char if (c == '\\') { escaped = true; continue; } // ending quote, done if (c == '\"') { break; } // normal char result.Append(c); } return(result.ToString()); }
private static object _GetValue(CharReader reader) { char c; string token; c = reader.Peek(); switch (c) { case 'n': token = _GetToken(reader); if (token != "null") { throw new ArgumentException($"Invalid token: {token}"); } return(null); case 'f': token = _GetToken(reader); if (token != "false") { throw new ArgumentException($"Invalid token: {token}"); } return(false); case 't': token = _GetToken(reader); if (token != "true") { throw new ArgumentException($"Invalid token: {token}"); } return(true); case '\"': return(_GetStringValue(reader)); case '[': return(_ParseJArray(reader)); case '{': return(_ParseJObject(reader)); case '-': case '+': case '.': case 'e': case 'E': case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': return(_GetNumeric(reader)); default: throw new ArgumentException($"Unexpected character: {c}"); } }
public new static JObject Parse(CharReader value) { return((JObject)JBase.Parse(value)); }
protected static void _SkipWhitespace(CharReader reader) { char currChar; while (true) { if (reader.EOF()) { break; } currChar = reader.Peek(); // use c# definition of whitespace if (char.IsWhiteSpace(currChar)) { reader.Read(); // gobble char continue; } // check for comments if (currChar == '/') { reader.Read(); // gobble char char nextChar = reader.Read(); if (nextChar == '/') // to EOF { while (true) { // not error if EOF before CR-LF if (reader.EOF()) { break; } currChar = reader.Read(); if (currChar == '\r' || currChar == '\n') { break; } } continue; } else if (nextChar == '*') /* to */ { nextChar = ' '; // clear for comparison while (true) { // will throw an error if EOF before '*/' is found currChar = nextChar; nextChar = reader.Read(); if (currChar == '*' && nextChar == '/') { break; } } continue; } else // not a comment { // push back in reverse order reader.Push(nextChar); reader.Push(currChar); break; } } break; } }
public new static JArray Parse(CharReader value) { return((JArray)JBase.Parse(value)); }