/// <summary>
        /// Creates the json string.
        /// </summary>
        public static string CreateJsonString(LogicJSONNode root, int ensureCapacity = 25)
        {
            StringBuilder builder = new StringBuilder(ensureCapacity);

            root.WriteToString(builder);
            return(builder.ToString());
        }
        /// <summary>
        /// Parses the <see cref="LogicJSONArray"/>.
        /// </summary>
        private static LogicJSONArray ParseArray(CharStream stream)
        {
            stream.SkipWhitespace();

            if (stream.Read() != '[')
            {
                LogicJSONParser.ParseError("Not an array");
                return(null);
            }

            LogicJSONArray jsonArray = new LogicJSONArray();

            stream.SkipWhitespace();

            char nextChar = stream.NextChar();

            if (nextChar != '\0')
            {
                if (nextChar == ']')
                {
                    stream.Read();
                    return(jsonArray);
                }

                while (true)
                {
                    LogicJSONNode node = LogicJSONParser.ParseValue(stream);

                    if (node != null)
                    {
                        jsonArray.Add(node);
                        stream.SkipWhitespace();

                        nextChar = stream.Read();

                        if (nextChar != ',')
                        {
                            if (nextChar == ']')
                            {
                                return(jsonArray);
                            }

                            break;
                        }
                    }
                    else
                    {
                        break;
                    }
                }
            }

            LogicJSONParser.ParseError("Not an array");

            return(null);
        }
        /// <summary>
        /// Gets the <see cref="LogicJSONNumber"/> at the specified index.
        /// </summary>
        public LogicJSONNumber GetJsonNumber(int index)
        {
            LogicJSONNode node = this.Items[index];

            if (node.Type != LogicJSONNode.NodeType.Number)
            {
                Debugger.Warning($"Wrong type {node.Type}, Index {index}");
                return(null);
            }

            return((LogicJSONNumber)node);
        }
        /// <summary>
        /// Gets the <see cref="LogicJSONArray"/> at the specified index.
        /// </summary>
        public LogicJSONArray GetJsonArray(int idx)
        {
            LogicJSONNode node = this.Items[idx];

            if (node.Type != LogicJSONNode.NodeType.Array)
            {
                Debugger.Warning($"Wrong type {node.Type}, Index {idx}");
                return(null);
            }

            return((LogicJSONArray)node);
        }
        /// <summary>
        /// Gets the <see cref="LogicJSONBoolean"/> at the specified index.
        /// </summary>
        public LogicJSONBoolean GetJsonBoolean(int idx)
        {
            LogicJSONNode node = this.Items[idx];

            if (node.Type != LogicJSONNode.NodeType.Boolean)
            {
                Debugger.Warning($"Wrong type {node.Type}, Index {idx}");
                return(null);
            }

            return((LogicJSONBoolean)node);
        }
        /// <summary>
        /// Gets the <see cref="LogicJSONString"/> at the specified index.
        /// </summary>
        public LogicJSONString GetJsonString(int idx)
        {
            LogicJSONNode node = this.Items[idx];

            if (node.Type != LogicJSONNode.NodeType.String)
            {
                Debugger.Warning($"LogicJSONObject::getJSONString wrong type {node.Type}, Index {idx}");
                return(null);
            }

            return((LogicJSONString)node);
        }
        /// <summary>
        /// Gets the <see cref="LogicJSONObject"/> at the specified index.
        /// </summary>
        public LogicJSONObject GetJsonObject(int idx)
        {
            LogicJSONNode node = this.Items[idx];

            if (node.Type != LogicJSONNode.NodeType.Object)
            {
                Debugger.Warning($"Wrong type {node.Type} Index {idx}");
                return(null);
            }

            return((LogicJSONObject)node);
        }
        /// <summary>
        /// Parses the <see cref="LogicJSONNode"/>.
        /// </summary>
        private static LogicJSONNode ParseValue(CharStream stream)
        {
            stream.SkipWhitespace();

            char          character = stream.NextChar();
            LogicJSONNode node      = null;

            switch (character)
            {
            case '{':
                node = LogicJSONParser.ParseObject(stream);
                break;

            case '[':
                node = LogicJSONParser.ParseArray(stream);
                break;

            case 'n':
                node = LogicJSONParser.ParseNull(stream);
                break;

            case 'f':
                node = LogicJSONParser.ParseBoolean(stream);
                break;

            case 't':
                node = LogicJSONParser.ParseBoolean(stream);
                break;

            case '"':
                node = LogicJSONParser.ParseString(stream);
                break;

            case '-':
                node = LogicJSONParser.ParseNumber(stream);
                break;

            default:
                if (character >= '0' && character <= '9')
                {
                    node = LogicJSONParser.ParseNumber(stream);
                }
                else
                {
                    LogicJSONParser.ParseError("Not of any recognized value: " + character);
                }

                break;
            }

            return(node);
        }
Exemplo n.º 9
0
        /// <summary>
        /// Gets the <see cref="LogicJSONBoolean"/> using the specified key.
        /// </summary>
        public LogicJSONBoolean GetJsonBoolean(string key)
        {
            int idx = this.Keys.IndexOf(key);

            if (idx == -1)
            {
                return(null);
            }

            LogicJSONNode node = this.Items[idx];

            if (node.Type == LogicJSONNode.NodeType.Boolean)
            {
                return((LogicJSONBoolean)node);
            }

            Debugger.Warning($"Type is {node.Type}, key {key}");

            return(null);
        }
Exemplo n.º 10
0
        /// <summary>
        /// Appends the specified key and <see cref="LogicJSONNode"/> to this <see cref="LogicJSONObject"/>.
        /// </summary>
        public void Put(string key, LogicJSONNode item)
        {
            int keyIdx = this.Keys.IndexOf(key);

            if (keyIdx != -1)
            {
                Debugger.Error($"Already contains key {key}");
            }
            else
            {
                int idx = this.Items.IndexOf(item);

                if (idx != -1)
                {
                    Debugger.Error($"Already contains the given JSONNode pointer. Key {key}");
                }
                else
                {
                    this.Items.Add(item);
                    this.Keys.Add(key);
                }
            }
        }
Exemplo n.º 11
0
        /// <summary>
        /// Parses the <see cref="LogicJSONObject"/>.
        /// </summary>
        private static LogicJSONObject ParseObject(CharStream stream)
        {
            stream.SkipWhitespace();

            if (stream.Read() != '{')
            {
                LogicJSONParser.ParseError("Not an object");
                return(null);
            }

            LogicJSONObject jsonObject = new LogicJSONObject();

            stream.SkipWhitespace();

            char nextChar = stream.NextChar();

            if (nextChar != '\0')
            {
                if (nextChar == '}')
                {
                    stream.Read();
                    return(jsonObject);
                }

                while (true)
                {
                    LogicJSONString key = LogicJSONParser.ParseString(stream);

                    if (key != null)
                    {
                        stream.SkipWhitespace();

                        nextChar = stream.Read();

                        if (nextChar != ':')
                        {
                            break;
                        }

                        LogicJSONNode node = LogicJSONParser.ParseValue(stream);

                        if (node != null)
                        {
                            jsonObject.Put(key.GetStringValue(), node);
                            stream.SkipWhitespace();

                            nextChar = stream.Read();

                            if (nextChar != ',')
                            {
                                if (nextChar == '}')
                                {
                                    return(jsonObject);
                                }

                                break;
                            }
                        }
                        else
                        {
                            break;
                        }
                    }
                    else
                    {
                        break;
                    }
                }
            }

            LogicJSONParser.ParseError("Not an object");
            return(null);
        }
Exemplo n.º 12
0
 /// <summary>
 /// Adds the specified <see cref="LogicJSONNode"/>.
 /// </summary>
 public void Add(LogicJSONNode item)
 {
     this.Items.Add(item);
 }