コード例 #1
0
        public bool Parse(
            string jsonFragment,
            out string jsonRemainder
            )
        {
            jsonRemainder = StringUtils.StripLeadingJsonWhitespace(
                jsonFragment
                );
            // The first character must be { for an array
            if (
                StringUtils.GetFirstCharacter(
                    jsonRemainder
                    ) != "{"
                )
            {
                Success       = false;
                jsonRemainder = jsonFragment;
                return(Success);
            }
            // Remove the bracket character
            jsonRemainder = StringUtils.StripFirstCharacter(
                jsonRemainder
                );

            /*
             * Now should have a series of json elements follow by commas
             * except for the last json element which is followed by ]
             *
             * Conditions
             * string still has some characters left in it
             * we haven't reached the array closing bracket
             */
            values = new Dictionary <string, IJsonElement>();
            IJsonElement nextKey;
            IJsonElement nextElement;

            while (
                (jsonRemainder.Length > 0) &&
                (
                    StringUtils.GetFirstCharacter(
                        jsonRemainder
                        ) != "}"
                )
                )
            {
                jsonRemainder = StringUtils.StripLeadingJsonWhitespace(
                    jsonRemainder
                    );
                // First try to read in the next key
                nextKey = SimpleJsonParser.ParseOne(
                    jsonRemainder,
                    out jsonRemainder
                    );
                // This must be a string, or invalid json
                if (
                    (nextKey == null) ||
                    !nextKey.IsString()
                    )
                {
                    Success       = false;
                    jsonRemainder = jsonFragment;
                    return(Success);
                }
                // Should be whitespace : whitespace between key & value
                jsonRemainder = StringUtils.StripLeadingJsonWhitespace(
                    jsonRemainder
                    );
                // If no colon, invalid json, fail
                if (
                    StringUtils.GetFirstCharacter(
                        jsonRemainder
                        ) != ":"
                    )
                {
                    Success       = false;
                    jsonRemainder = jsonFragment;
                    return(Success);
                }
                // Step over colon & whitespace after colon
                jsonRemainder = StringUtils.StripFirstCharacter(
                    jsonRemainder
                    );
                jsonRemainder = StringUtils.StripLeadingJsonWhitespace(
                    jsonRemainder
                    );
                // Try to extract element
                nextElement = SimpleJsonParser.ParseOne(
                    jsonRemainder,
                    out jsonRemainder
                    );
                // A returned null means that the json was not parseable
                if (nextElement == null)
                {
                    Success       = false;
                    jsonRemainder = jsonFragment;
                    return(Success);
                }
                // Parsed successfully -> add to dictionary of parsed elements
                values[
                    nextKey.AsString()
                ] = nextElement;
                // Remove leading whitespace
                jsonRemainder = StringUtils.StripLeadingJsonWhitespace(
                    jsonRemainder
                    );
                // Next character should be either comma or bracket }
                if (
                    (jsonRemainder.Length > 0) &&
                    (
                        StringUtils.GetFirstCharacter(
                            jsonRemainder
                            ) == ","
                    )
                    )
                {
                    // If it's a comma, remove it
                    jsonRemainder = StringUtils.StripFirstCharacter(
                        jsonRemainder
                        );
                }
                else if (
                    (jsonRemainder.Length > 0) &&
                    (
                        StringUtils.GetFirstCharacter(
                            jsonRemainder
                            ) == "}"
                    )
                    )
                {
                    /*
                     * For a closing bracket do nothing
                     * It will be caught on next loop condition check
                     */
                }
                else
                {
                    // If it's anything else, bad formatting
                    Success       = false;
                    jsonRemainder = jsonFragment;
                    return(Success);
                }
            }
            // Landing here should mean success
            Success = true;
            // Remember to remove closing bracket
            jsonRemainder = StringUtils.StripFirstCharacter(
                jsonRemainder
                );
            return(Success);
        }
コード例 #2
0
        public bool Parse(
            string jsonFragment,
            out string jsonRemainder
            )
        {
            jsonRemainder = StringUtils.StripLeadingJsonWhitespace(
                jsonFragment
                );
            // The first character must be [ for an array
            if (
                StringUtils.GetFirstCharacter(
                    jsonRemainder
                    ) != "["
                )
            {
                Success       = false;
                jsonRemainder = jsonFragment;
                return(Success);
            }
            // Remove the bracket character
            jsonRemainder = StringUtils.StripFirstCharacter(
                jsonRemainder
                );

            /*
             * Now should have a series of json elements follow by commas
             * except for the last json element which is followed by ]
             *
             * Conditions
             * string still has some characters left in it
             * we haven't reached the array closing bracket
             */
            LinkedList <IJsonElement> elements = new LinkedList <IJsonElement>();
            IJsonElement nextElement;

            while (
                (jsonRemainder.Length > 0) &&
                (
                    StringUtils.GetFirstCharacter(
                        jsonRemainder
                        ) != "]"
                )
                )
            {
                jsonRemainder = StringUtils.StripLeadingJsonWhitespace(
                    jsonRemainder
                    );
                nextElement = SimpleJsonParser.ParseOne(
                    jsonRemainder,
                    out jsonRemainder
                    );
                // A returned null means that the json was not parseable
                if (nextElement == null)
                {
                    Success       = false;
                    jsonRemainder = jsonFragment;
                    return(Success);
                }
                // Parsed successfully -> add to list of parsed elements
                elements.AddLast(
                    nextElement
                    );
                // Remove leading whitespace
                jsonRemainder = StringUtils.StripLeadingJsonWhitespace(
                    jsonRemainder
                    );
                // Next character should be either comma or bracket
                if (
                    (jsonRemainder.Length > 0) &&
                    (
                        StringUtils.GetFirstCharacter(
                            jsonRemainder
                            ) == ","
                    )
                    )
                {
                    // If it's a comma, remove it
                    jsonRemainder = StringUtils.StripFirstCharacter(
                        jsonRemainder
                        );
                }
                else if (
                    (jsonRemainder.Length > 0) &&
                    (
                        StringUtils.GetFirstCharacter(
                            jsonRemainder
                            ) == "]"
                    )
                    )
                {
                    /*
                     * For a closing bracket do nothing
                     * It will be caught on next loop condition check
                     */
                }
                else
                {
                    // If it's anything else, bad formatting
                    Success       = false;
                    jsonRemainder = jsonFragment;
                    return(Success);
                }
            }
            // Landing here should mean success
            Success = true;
            // Move linked list elements to values array
            values = new IJsonElement[elements.Count];
            elements.CopyTo(values, 0);
            // Remember to remove the closing bracket
            jsonRemainder = StringUtils.StripFirstCharacter(
                jsonRemainder
                );
            return(Success);
        }