public JsonObject(Queue <TextToken> textQueue) { Type = TokenType.Object; TextToken current = textQueue.Dequeue(); if (current.type != TextToken.TextTokenType.BeginObject) { throw new FormatException("Texttoken is not an object beginning"); } while (textQueue.Count != 0) { current = textQueue.Dequeue(); string propertyName; JsonToken value; switch (current.type) { case TextToken.TextTokenType.Reference: case TextToken.TextTokenType.String: propertyName = current.content; TextToken.TextTokenType CurType = current.type; current = textQueue.Dequeue(); if (current.type != TextToken.TextTokenType.NameSeparator) { throw new FormatException("No name separator"); } value = JsonReader.ReadNext(textQueue); JsonValue keyAsStr = new JsonValue(propertyName, TokenType.String); JsonValue keyAsRef = new JsonValue(propertyName, TokenType.Reference); if (Values.ContainsKey(keyAsRef) || Values.ContainsKey(keyAsStr)) { throw new ArgumentException("Cant add existing key"); } if (CurType == TextToken.TextTokenType.Reference) { Values.Add(keyAsRef, value); } else if (CurType == TextToken.TextTokenType.String) { Values.Add(keyAsStr, value); } break; case TextToken.TextTokenType.ValueSeparator: break; case TextToken.TextTokenType.EndObject: return; default: throw new FormatException("Found something improper in an object"); } } }
public JsonToken UnJson() { Queue <TextToken> fileContent = new Queue <TextToken>(); TextToken temp = TextToken.GetNextTextToken(Reader); while (temp != null) { fileContent.Enqueue(temp); temp = TextToken.GetNextTextToken(Reader); } ; JsonObject a = new JsonObject(fileContent); return(a); }
public JsonArray(Queue <TextToken> textQueue) { Type = TokenType.Array; TextToken current = textQueue.Dequeue(); if (current.type != TextToken.TextTokenType.BeginArray) { throw new FormatException("Texttoken is not an Array beginning"); } TextToken peek = textQueue.Peek(); while (textQueue.Count != 0) { peek = textQueue.Peek(); switch (peek.type) { case TextToken.TextTokenType.Double: case TextToken.TextTokenType.False: case TextToken.TextTokenType.Integer: case TextToken.TextTokenType.Reference: case TextToken.TextTokenType.True: case TextToken.TextTokenType.String: case TextToken.TextTokenType.BeginArray: case TextToken.TextTokenType.BeginObject: JsonToken value = JsonReader.ReadNext(textQueue); //if (value.Type == JsonToken.TokenType.Array) // throw new FormatException("Cant have array in array)"); Values.Add(value); break; case TextToken.TextTokenType.ValueSeparator: textQueue.Dequeue(); break; case TextToken.TextTokenType.EndArray: textQueue.Dequeue(); return; default: throw new FormatException("Found something improper in an array"); } } }
public JsonValue(Queue <TextToken> textQueue) { TextToken current = textQueue.Dequeue(); switch (current.type) { case TextToken.TextTokenType.Double: Type = TokenType.Double; Content = Convert.ToDouble(current.content); break; case TextToken.TextTokenType.False: Type = TokenType.Boolean; Content = false; break; case TextToken.TextTokenType.Integer: Type = TokenType.Integer; Content = Convert.ToInt32(current.content); break; case TextToken.TextTokenType.Reference: Type = TokenType.Reference; Content = current.content; break; case TextToken.TextTokenType.String: Type = TokenType.String; Content = current.content; break; case TextToken.TextTokenType.True: Type = TokenType.Boolean; Content = true; break; default: throw new FormatException("TextToken cannot be a value"); } }
public static JsonToken ReadNext(Queue <TextToken> textQueue) { TextToken peek = textQueue.Peek(); switch (peek.type) { case TextToken.TextTokenType.BeginObject: return(new JsonObject(textQueue)); case TextToken.TextTokenType.BeginArray: return(new JsonArray(textQueue)); case TextToken.TextTokenType.Double: case TextToken.TextTokenType.False: case TextToken.TextTokenType.Integer: case TextToken.TextTokenType.Reference: case TextToken.TextTokenType.String: case TextToken.TextTokenType.True: return(new JsonValue(textQueue)); default: throw new FormatException("Queue is not proper for token extraction"); } }
public static TextToken GetNextTextToken(StringReader reader) { TextToken result = new TextToken() { content = "" }; int peekResult = reader.Peek(); while (peekResult != -1) { if (!Enum.IsDefined(typeof(WhiteSpace), peekResult)) { int nextPeekResult; result.content = ((char)reader.Read()).ToString(); switch (peekResult) { case (int)TokenMarkers.BeginArray: result.type = TextTokenType.BeginArray; break; case (int)TokenMarkers.BeginObject: result.type = TextTokenType.BeginObject; break; case (int)TokenMarkers.EndArray: result.type = TextTokenType.EndArray; break; case (int)TokenMarkers.EndObject: result.type = TextTokenType.EndObject; break; case (int)TokenMarkers.NameSeparator: result.type = TextTokenType.NameSeparator; break; case (int)TokenMarkers.ValueSeparator2: case (int)TokenMarkers.ValueSeparator1: result.type = TextTokenType.ValueSeparator; break; case (int)TokenMarkers.StringStart: result.type = TextTokenType.String; nextPeekResult = reader.Peek(); while (true) { switch (nextPeekResult) { case (int)TokenMarkers.EscapeChar: //next character was escaped result.content = result.content + ((char)reader.Read()).ToString(); result.content = result.content + ((char)reader.Read()).ToString(); break; case -1: case (int)TokenMarkers.StringStart: result.content = result.content + ((char)reader.Read()).ToString(); if (result.content.Length == 2) { result.content = ""; } else { result.content = result.content.Substring(1, result.content.Length - 2); } return(result); default: result.content = result.content + ((char)reader.Read()).ToString(); break; } nextPeekResult = reader.Peek(); } default: nextPeekResult = reader.Peek(); while (true) { if (Enum.IsDefined(typeof(WhiteSpace), nextPeekResult)) { reader.Read(); } else { switch (nextPeekResult) { case -1: case (int)TokenMarkers.StringStart: throw new FormatException(); case (int)TokenMarkers.EndObject: case (int)TokenMarkers.ValueSeparator1: case (int)TokenMarkers.ValueSeparator2: case (int)TokenMarkers.NameSeparator: case (int)TokenMarkers.EndArray: result.type = TextTokenType.Reference; if (Regex.IsMatch(result.content, @"^\-?[0-9]+$")) { //int and double are not strongly typed in Json, lets not do the same result.type = TextTokenType.Double; } else if (Regex.IsMatch(result.content, @"^\-?[0-9\.]+$")) { result.type = TextTokenType.Double; } else if (Regex.IsMatch(result.content, @"^\-?[0-9\.]+f$")) { result.type = TextTokenType.Double; result.content = result.content.Substring(0, result.content.Length - 1); } else if (Regex.IsMatch(result.content, @"false")) { result.type = TextTokenType.False; } else if (Regex.IsMatch(result.content, @"true")) { result.type = TextTokenType.True; } return(result); default: result.content = result.content + ((char)reader.Read()).ToString();; break; } } nextPeekResult = reader.Peek(); } } return(result); } reader.Read(); peekResult = reader.Peek(); } return(null); }