Beispiel #1
0
        public bool Parse(
            string jsonFragment,
            out string jsonRemainder
            )
        {
            jsonRemainder = StringUtils.StripLeadingJsonWhitespace(
                jsonFragment
                );
            // Find character to denote end of value
            int i = 0;

            while (
                (i < jsonRemainder.Length) &&
                (jsonRemainder.Substring(i, 1) != ",") &&
                (jsonRemainder.Substring(i, 1) != "]") &&
                (jsonRemainder.Substring(i, 1) != "}")
                )
            {
                i++;
            }
            // If no closing character, badly formatted json
            if (i == jsonRemainder.Length)
            {
                Success       = false;
                jsonRemainder = jsonFragment;
                return(Success);
            }
            string numberString = jsonRemainder.Substring(
                0, i
                );

            try
            {
                valueInteger = Int32.Parse(
                    numberString
                    );
                isInteger     = true;
                isDouble      = false;
                Success       = true;
                jsonRemainder = jsonRemainder.Substring(i);
                return(Success);
            } catch (Exception e1)
            {
                try
                {
                    valueDouble = Double.Parse(
                        numberString
                        );
                    isInteger     = false;
                    isDouble      = true;
                    Success       = true;
                    jsonRemainder = jsonRemainder.Substring(i);
                    return(Success);
                } catch (Exception e2)
                {
                    isInteger     = false;
                    isDouble      = false;
                    Success       = false;
                    jsonRemainder = jsonFragment;
                    return(Success);
                }
            }
        }
        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);
        }