예제 #1
0
        /// <summary>
        /// Deserialize a Json Array into an object JsonArray
        /// </summary>
        /// <returns>
        /// JsonArray object as a deserialized JSON array
        /// </returns>
        /// <param name='array'>
        /// valid JSON array except the brackets
        /// </param>

        private JsonArray SerializeArray(String array)
        {
            JsonArray arrayObject = new JsonArray();
            var       elements    = SplitElements(array);

            foreach (String item in elements)
            {
                if (item[0] == DoubleQuotes)
                {
                    String     withoutQuotes = item.Trim(DoubleQuotes);
                    JsonObject o             = new JsonString(withoutQuotes);
                    arrayObject.AddElementToArray(o);
                }
                else if (item[0] == ObjectBegin)
                {
                    JsonObject o = SerializeObject(item);
                    arrayObject.AddElementToArray(o);
                }
                else if (item[0] == ArrayBegin)
                {
                    String     itemArray      = item.Substring(1, item.Length - 2);
                    JsonArray  secondaryArray = SerializeArray(itemArray);
                    JsonObject o = secondaryArray;
                    arrayObject.AddElementToArray(o);
                }
                else if (item[0] == '-' || item[0] == '0' || item[0] == '1' || item[0] == '2' || item[0] == '3' ||
                         item[0] == '4' || item[0] == '5' || item[0] == '6' || item[0] == '7' || item[0] == '8' || item[0] == '9')
                {
                    Double     doubleValue = Convert.ToDouble(item);
                    float      floatValue  = (float)doubleValue;
                    JsonObject o           = new JsonNumber(floatValue);
                    arrayObject.AddElementToArray(o);
                }
                else if (String.Compare(SafeSubString(item, 0, 4), TrueValue, StringComparison.Ordinal) == 0)
                {
                    JsonObject o = new JsonBoolean(true);
                    arrayObject.AddElementToArray(o);
                }
                else if (String.Compare(SafeSubString(item, 0, 5), FalseValue, StringComparison.Ordinal) == 0)
                {
                    JsonObject o = new JsonBoolean(false);
                    arrayObject.AddElementToArray(o);
                }
                else if (String.Compare(SafeSubString(item, 0, 4), NullValue, StringComparison.Ordinal) == 0)
                {
                    JsonObject o = new JsonNullable();
                    arrayObject.AddElementToArray(o);
                }
            }

            return(arrayObject);
        }
예제 #2
0
        /// <summary>
        /// This method performs deserialization of an object(except array)
        /// </summary>
        /// <returns>
        /// JsonObject object as a deserialized JSON object
        /// </returns>
        /// <param name='json'>
        /// A string which contains a valid Json object
        /// </param>
        private JsonObject SerializeObject(String json)
        {
            json = json.Replace(@"\", "");
            JsonObject document   = new JsonObject();
            int        n          = 1;
            int        lengthJson = json.Length;
            String     keyString  = "";

            while (n <= lengthJson - 1)
            {
                if (json[n] == DoubleQuotes && json[n - 1] != DoublePoint) // (key-value Pair) key Name
                {
                    int secondDoubleQuotes = FindNextQuote(json, n + 1);
                    keyString = json.Substring(n + 1, (secondDoubleQuotes - (n + 1)));
                    n         = secondDoubleQuotes + 1;
                }
                else if (json[n] == DoubleQuotes && json[n - 1] == DoublePoint) //  (key-value Pair) value Name (if string)
                {
                    if (json[n + 1] != DoubleQuotes)
                    {
                        int        secondDoublesQuotes = FindNextQuote(json, n + 1);
                        String     text        = json.Substring(n + 1, (secondDoublesQuotes - (n + 1)));
                        JsonString stringValue = new JsonString(text);
                        JsonObject o           = stringValue;
                        document.AddJsonValue(keyString, o);
                        n = secondDoublesQuotes + 1;
                    }
                    else
                    {
                        JsonObject o = new JsonString("");
                        document.AddJsonValue(keyString, o);
                    }
                }
                else if (json[n] == '-' || json[n] == '0' || json[n] == '1' || json[n] == '2' || json[n] == '3' ||
                         json[n] == '4' || json[n] == '5' || json[n] == '6' || json[n] == '7' || json[n] == '8' ||
                         json[n] == '9') // (key-value Pair) value (if number)
                {
                    char[]     arrayEndings = { ObjectEnd, Comma };
                    int        nextComma    = json.IndexOfAny(arrayEndings, n);
                    String     stringNumber = json.Substring(n, nextComma - n);
                    Double     valueNumber  = Convert.ToDouble(stringNumber);
                    float      floatNumber  = (float)valueNumber;
                    JsonNumber number       = new JsonNumber(floatNumber);
                    JsonObject o            = number;
                    document.AddJsonValue(keyString, o);
                    n = nextComma + 1;
                }
                else if (json[n] == ArrayBegin) //(key-value Pair) value (if array)
                {
                    if (json[n + 1] != ArrayEnd)
                    {
                        String     subJson      = json.Substring(n, json.Length - n);
                        int        arrayClose   = CloseBracketArray(subJson);
                        String     arrayUnknown = json.Substring(n + 1, arrayClose - 2);
                        JsonArray  arrayObjects = SerializeArray(arrayUnknown);
                        JsonObject o            = arrayObjects;
                        document.AddJsonValue(keyString, o);
                        n = n + arrayClose;
                    }
                    else
                    {
                        if (!string.IsNullOrEmpty(keyString))
                        {
                            JsonArray arrayTempEmpty = new JsonArray {
                                Array = null
                            };
                            JsonObject emptyArray = arrayTempEmpty;
                            document.AddJsonValue(keyString, emptyArray);
                            keyString = "";
                        }
                        else
                        {
                            n++;
                        }
                    }
                }
                else if (json[n] == ObjectBegin) // (key-value Pair) value (if object)
                {
                    if (json[n + 1] != ObjectEnd)
                    {
                        String subJson       = json.Substring(n, json.Length - n);
                        int    objectClose   = CloseBracketObject(subJson);
                        String objectUnknown = json.Substring(n, objectClose);
                        var    o             = SerializeObject(objectUnknown);
                        document.AddJsonValue(keyString, o);
                        n = n + objectClose + 1;
                    }
                    else
                    {
                        JsonObject o = new JsonObject {
                            Values = null
                        };
                        document.AddJsonValue(keyString, o);
                    }
                }
                else if (String.Compare(SafeSubString(json, n, 4), NullValue, StringComparison.Ordinal) == 0) // (key-value Pair) value (if NULL)
                {
                    JsonObject o = new JsonNullable();
                    document.AddJsonValue(keyString, o);
                    n = n + 5;
                }
                else if (String.Compare(SafeSubString(json, n, 4), TrueValue, StringComparison.Ordinal) == 0) // (key-value Pair) value (if TRUE)
                {
                    JsonObject o = new JsonBoolean(true);
                    document.AddJsonValue(keyString, o);
                    n = n + 5;
                }
                else if (String.Compare(SafeSubString(json, n, 5), FalseValue, StringComparison.Ordinal) == 0) // (key-value Pair) value (if FALSE)
                {
                    JsonObject o = new JsonBoolean(false);
                    document.AddJsonValue(keyString, o);
                    n = n + 6;
                }
                else
                {
                    n++;
                }
            }

            return(document);
        }