Esempio n. 1
0
        /// <summary>
        /// Parses a JSON-encoded array into a collection of objects created with the specified delegate.
        /// </summary>
        /// <param name="input">The JSON-encoded character array to parse.</param>
        /// <param name="func">The delegate method used to create objects from the JSON array's contents.</param>
        /// <returns>A list of decoded objects.</returns>
        public static ArrayList ParseArrayToObjects(char[] input, ObjectCreationFunction func)
        {
            ArrayList result;

            TryParseArrayToObjects(input, func, out result, true);
            return(result);
        }
Esempio n. 2
0
        private static bool TryParseArrayToObjects(char[] input, ObjectCreationFunction func, out ArrayList result, bool throwIfError)
        {
            int index = 0;

            object obj;
            bool   success = TryParseArray(input, ref index, func, out obj);

            result = obj as ArrayList;

            if (throwIfError && (!success || result == null))
            {
                throw new Exception("JSON parse error at index " + index);
            }

            // Done
            return(success);
        }
Esempio n. 3
0
 /// <summary>
 /// Attempts to parse a JSON-encoded array into a collection of objects created with the specified delegate.
 /// </summary>
 /// <param name="input">The JSON-encoded character array to parse.</param>
 /// <param name="func">The delegate method used to create objects from the JSON array's contents.</param>
 /// <param name="result">Returns the list of decoded objects.</param>
 /// <returns>true if parsing was successful; otherwise, false.</returns>
 public static bool TryParseArrayToObjects(char[] input, ObjectCreationFunction func, out ArrayList result)
 {
     return(TryParseArrayToObjects(input, func, out result, false));
 }
Esempio n. 4
0
        /// <summary>
        /// Attempts to parse a JSON array starting at the specified index.
        /// </summary>
        private static bool TryParseArray(char[] input, ref int index, ObjectCreationFunction func, out object result)
        {
            if (input[index++] != '[')
            {
                result = null;
                return(false);
            }

            SkipWhitespace(input, ref index);

            object    value;
            char      c;
            ArrayList arrayList = new ArrayList();

            while (index < input.Length)
            {
                // Are we at the end of the array?
                if (input[index] == ']')
                {
                    index++;
                    result = arrayList;
                    return(true);
                }

                // Try to parse a value
                if (!TryParseValue(input, ref index, out value))
                {
                    break;
                }

                // Success, add it to the list
                if (func == null)
                {
                    arrayList.Add(value);
                }
                else
                {
                    arrayList.Add(func(value));
                }

                // Skip ahead to the next non-whitespace character
                SkipWhitespace(input, ref index);
                if (index >= input.Length)
                {
                    break;
                }

                c = input[index];

                if (c == ']')
                {
                    continue;
                }

                if (c == ',')
                {
                    index++;
                    SkipWhitespace(input, ref index);
                    continue;
                }

                // Error: there should be a ',' or ']' after each value
                break;
            }

            // An error occurred or we reached the end of the input stream before finishing
            result = null;
            return(false);
        }