/// <summary> /// Parses a list. /// </summary> /// <returns> /// A <see cref="IronJSONValue"/> /// </returns> private IronJSONValue ParseList() { IronJSONValue val = new IronJSONValue(ValueType.Array); VerifyToken(m_tokenStream.CurrentToken, new TokenType[]{TokenType.LeftSquareBracket}); // Skip the '[' if (!m_tokenStream.ToNextToken()) throw ParseError("expected ']'"); while (!m_tokenStream.AtEnd()) { // If what we hit a ']', break. if (m_tokenStream.CurrentToken.Type == TokenType.RightSquareBracket) { m_tokenStream.ToNextToken(); break; } // Parse the value. val.Array.Add(ParseValue()); // Skip the ',' or ']'. Error if it's not either. VerifyToken(m_tokenStream.CurrentToken, new TokenType[]{TokenType.Comma, TokenType.RightSquareBracket}); if (!m_tokenStream.ToNextToken()) break; // If what we hit a ']', break. if (m_tokenStream.PreviousToken.Type == TokenType.RightSquareBracket) break; } return val; }
/// <summary> /// /// </summary> /// <param name="key"> /// A <see cref="System.Object"/> /// </param> /// <param name="size"> /// A <see cref="System.Int32"/> /// </param> public void SetToArray(object key, int size) { if (key is int && m_cd.Type == ValueType.Array) { IronJSONValue arr = new IronJSONValue(ValueType.Array); // Set every element to null. for (int i = 0; i < size; ++i) arr.Array.Add(new IronJSONValue(ValueType.Null)); m_cd.Array[(int)key] = arr; } else if (key is string && m_cd.Type == ValueType.Object) { IronJSONValue arr = new IronJSONValue(ValueType.Array); // Set every element to null. for (int i = 0; i < size; ++i) arr.Array.Add(new IronJSONValue(ValueType.Null)); m_cd.Obj[(string)key] = arr; } else throw new InvalidKeyException(key.ToString()); }
/// <summary> /// Opens a new file to be read. Overwrite the current /// JSON object. /// </summary> public void Open(string filename) { StreamReader reader = new StreamReader(filename); IronJSONLexer lexer; IronJSONTokenStream stream; IronJSONParser parser; lexer = new IronJSONLexer(reader.ReadToEnd()); stream = lexer.GenerateTokenStream(); parser = new IronJSONParser(stream); parser.Parse(); m_obj = parser.Obj; m_cd = new IronJSONValue(m_obj); m_curPath = new ArrayList(); reader.Close(); }
/// <summary> /// Constructs an empty JSON object. /// </summary> public JSONManager() { m_obj = new IronJSONObject(); m_cd = new IronJSONValue(m_obj); m_curPath = new ArrayList(); }
/// <summary> /// /// </summary> /// <param name="path"> /// A <see cref="Path"/> /// </param> /// <param name="keys"> /// A <see cref="System.Object[]"/> /// </param> public void Cd(Path path, params object[] keys) { if (path == Path.Absolute) { m_cd = new IronJSONValue(m_obj); m_curPath.Clear(); } foreach (object o in keys) { if (o is int) { if (m_cd.Type != ValueType.Array) throw new InvalidKeyException(CurrentPath + o.ToString()); try { m_cd = (IronJSONValue)m_cd.Array[(int)o]; } // Out of range of the array. catch (System.ArgumentOutOfRangeException) { throw new KeyNotFoundException(CurrentPath + o.ToString()); } } else if (o is string) { if (m_cd.Type != ValueType.Object) throw new InvalidKeyException(CurrentPath + o.ToString()); else if (!m_cd.Obj.ContainsKey((string)o)) throw new KeyNotFoundException(CurrentPath + o.ToString()); m_cd = (IronJSONValue)m_cd.Obj[(string)o]; } else { throw new InvalidKeyException(o.ToString()); } // Validate, make sure we CDed to an object or array. if (m_cd.Type != ValueType.Array && m_cd.Type != ValueType.Object) throw new InvalidLocationException("change directory (Cd)", CurrentPath + o.ToString()); m_curPath.Add(o); } }