public void JsonArrayAddRemoveFunctionalTest()
        {
            int seed = 1;

            for (int i = 0; i < iterationCount / 10; i++)
            {
                seed++;
                Log.Info("Seed: {0}", seed);
                Random rndGen   = new Random(seed);
                bool   retValue = true;

                JsonArray   sourceJson = SpecialJsonValueHelper.CreatePrePopulatedJsonArray(seed, arrayLength);
                JsonValue[] cloneJson  = SpecialJsonValueHelper.CreatePrePopulatedJsonValueArray(seed, 3);

                // JsonArray.AddRange(JsonValue[])
                sourceJson.AddRange(cloneJson);
                if (sourceJson.Count != arrayLength + cloneJson.Length)
                {
                    Log.Info("[JsonArrayAddRemoveFunctionalTest] JsonArray.AddRange(JsonValue[]) failed to function properly.");
                    retValue = false;
                }
                else
                {
                    Log.Info("[JsonArrayAddRemoveFunctionalTest] JsonArray.AddRange(JsonValue[]) passed test.");
                }

                // JsonArray.RemoveAt(int)
                int count = sourceJson.Count;
                for (int j = 0; j < count; j++)
                {
                    sourceJson.RemoveAt(0);
                }

                if (sourceJson.Count > 0)
                {
                    Log.Info("[JsonArrayAddRemoveFunctionalTest] JsonArray.RemoveAt(int) failed to function properly.");
                    retValue = false;
                }
                else
                {
                    Log.Info("[JsonArrayAddRemoveFunctionalTest] JsonArray.RemoveAt(int) passed test.");
                }

                // JsonArray.JsonType
                if (sourceJson.JsonType != JsonType.Array)
                {
                    Log.Info("[JsonArrayAddRemoveFunctionalTest] JsonArray.JsonType failed to function properly.");
                    retValue = false;
                }

                // JsonArray.Clear()
                sourceJson = SpecialJsonValueHelper.CreatePrePopulatedJsonArray(seed, arrayLength);
                sourceJson.Clear();
                if (sourceJson.Count > 0)
                {
                    Log.Info("[JsonArrayAddRemoveFunctionalTest] JsonArray.Clear() failed to function properly.");
                    retValue = false;
                }
                else
                {
                    Log.Info("[JsonArrayAddRemoveFunctionalTest] JsonArray.Clear() passed test.");
                }

                // JsonArray.AddRange(JsonValue)
                sourceJson = SpecialJsonValueHelper.CreatePrePopulatedJsonArray(seed, arrayLength);

                // adding one additional value to the array
                sourceJson.AddRange(SpecialJsonValueHelper.GetRandomJsonPrimitives(seed));
                if (sourceJson.Count != arrayLength + 1)
                {
                    Log.Info("[JsonArrayAddRemoveFunctionalTest] JsonArray.AddRange(JsonValue) failed to function properly.");
                    retValue = false;
                }
                else
                {
                    Log.Info("[JsonArrayAddRemoveFunctionalTest] JsonArray.AddRange(JsonValue) passed test.");
                }

                // JsonArray.AddRange(IEnumerable<JsonValue> items)
                sourceJson = SpecialJsonValueHelper.CreatePrePopulatedJsonArray(seed, arrayLength);
                MyJsonValueCollection <JsonValue> myCols = new MyJsonValueCollection <JsonValue>();
                myCols.Add(new JsonPrimitive(PrimitiveCreator.CreateInstanceOfUInt32(rndGen)));
                string str;
                do
                {
                    str = PrimitiveCreator.CreateInstanceOfString(rndGen);
                } while (str == null);

                myCols.Add(new JsonPrimitive(str));
                myCols.Add(new JsonPrimitive(PrimitiveCreator.CreateInstanceOfDateTime(rndGen)));

                // adding 3 additional value to the array
                sourceJson.AddRange(myCols);
                if (sourceJson.Count != arrayLength + 3)
                {
                    Log.Info("[JsonArrayAddRemoveFunctionalTest] JsonArray.AddRange(IEnumerable<JsonValue> items) failed to function properly.");
                    retValue = false;
                }
                else
                {
                    Log.Info("[JsonArrayAddRemoveFunctionalTest] JsonArray.AddRange(IEnumerable<JsonValue> items) passed test.");
                }

                // JsonArray[index].set_Item
                sourceJson = SpecialJsonValueHelper.CreatePrePopulatedJsonArray(seed, arrayLength);
                string temp;
                do
                {
                    temp = PrimitiveCreator.CreateInstanceOfString(rndGen);
                } while (temp == null);

                sourceJson[1] = temp;
                if ((string)sourceJson[1] != temp)
                {
                    Log.Info("[JsonArrayAddRemoveFunctionalTest] JsonArray[index].set_Item failed to function properly.");
                    retValue = false;
                }
                else
                {
                    Log.Info("[JsonArrayAddRemoveFunctionalTest] JsonArray[index].set_Item passed test.");
                }

                // JsonArray.Remove(JsonValue)
                count = sourceJson.Count;
                for (int j = 0; j < count; j++)
                {
                    sourceJson.Remove(sourceJson[0]);
                }

                if (sourceJson.Count > 0)
                {
                    Log.Info("[JsonArrayAddRemoveFunctionalTest] JsonArray.Remove(JsonValue) failed to function properly.");
                    retValue = false;
                }

                Assert.True(retValue);
            }
        }
        public void MixedJsonTypeFunctionalTest()
        {
            bool oldValue = CreatorSettings.CreateDateTimeWithSubMilliseconds;

            CreatorSettings.CreateDateTimeWithSubMilliseconds = false;
            try
            {
                int seed = 1;

                for (int i = 0; i < iterationCount; i++)
                {
                    seed++;
                    Log.Info("Seed: {0}", seed);
                    Random rndGen = new Random(seed);

                    JsonArray sourceJson = new JsonArray(new List <JsonValue>()
                    {
                        PrimitiveCreator.CreateInstanceOfBoolean(rndGen),
                        PrimitiveCreator.CreateInstanceOfByte(rndGen),
                        PrimitiveCreator.CreateInstanceOfDateTime(rndGen),
                        PrimitiveCreator.CreateInstanceOfDateTimeOffset(rndGen),
                        PrimitiveCreator.CreateInstanceOfDecimal(rndGen),
                        PrimitiveCreator.CreateInstanceOfDouble(rndGen),
                        PrimitiveCreator.CreateInstanceOfInt16(rndGen),
                        PrimitiveCreator.CreateInstanceOfInt32(rndGen),
                        PrimitiveCreator.CreateInstanceOfInt64(rndGen),
                        PrimitiveCreator.CreateInstanceOfSByte(rndGen),
                        PrimitiveCreator.CreateInstanceOfSingle(rndGen),
                        PrimitiveCreator.CreateInstanceOfString(rndGen),
                        PrimitiveCreator.CreateInstanceOfUInt16(rndGen),
                        PrimitiveCreator.CreateInstanceOfUInt32(rndGen),
                        PrimitiveCreator.CreateInstanceOfUInt64(rndGen),
                        new JsonObject(new Dictionary <string, JsonValue>()
                        {
                            { "Boolean", PrimitiveCreator.CreateInstanceOfBoolean(rndGen) },
                            { "Byte", PrimitiveCreator.CreateInstanceOfByte(rndGen) },
                            { "DateTime", PrimitiveCreator.CreateInstanceOfDateTime(rndGen) },
                            { "DateTimeOffset", PrimitiveCreator.CreateInstanceOfDateTimeOffset(rndGen) },
                            { "Decimal", PrimitiveCreator.CreateInstanceOfDecimal(rndGen) },
                            { "Double", PrimitiveCreator.CreateInstanceOfDouble(rndGen) },
                            { "Int16", PrimitiveCreator.CreateInstanceOfInt16(rndGen) },
                            { "Int32", PrimitiveCreator.CreateInstanceOfInt32(rndGen) },
                            { "Int64", PrimitiveCreator.CreateInstanceOfInt64(rndGen) },
                            { "SByte", PrimitiveCreator.CreateInstanceOfSByte(rndGen) },
                            { "Single", PrimitiveCreator.CreateInstanceOfSingle(rndGen) },
                            { "String", PrimitiveCreator.CreateInstanceOfString(rndGen) },
                            { "UInt16", PrimitiveCreator.CreateInstanceOfUInt16(rndGen) },
                            { "UInt32", PrimitiveCreator.CreateInstanceOfUInt32(rndGen) },
                            { "UInt64", PrimitiveCreator.CreateInstanceOfUInt64(rndGen) }
                        })
                    });

                    JsonArray newJson = (JsonArray)JsonValue.Parse(sourceJson.ToString());
                    Assert.True(JsonValueVerifier.Compare(sourceJson, newJson));
                }
            }
            finally
            {
                CreatorSettings.CreateDateTimeWithSubMilliseconds = oldValue;
            }
        }
예제 #3
0
        private static JsonArray BuildJsonArrayinSequence1(bool myBool, byte myByte, DateTime myDatetime, DateTimeOffset myDateTimeOffset, decimal myDecimal, double myDouble, short myInt16, int myInt32, long myInt64, sbyte mySByte, float mySingle, string myString, ushort myUInt16, uint myUInt32, ulong myUInt64, JsonArray myArray, JsonArray myArrayLevel2, JsonArray myArrayLevel3, JsonArray myArrayLevel4, JsonArray myArrayLevel5, JsonArray myArrayLevel6, JsonArray myArrayLevel7)
        {
            JsonArray sourceJson = new JsonArray
            {
                new JsonObject {
                    { "Name", "myArray" }, { "Index", 1 }, { "Obj", myArray }
                },
                new JsonObject {
                    { "Name", "myArrayLevel2" }, { "Index", 2 }, { "Obj", myArrayLevel2 }
                },
                new JsonObject {
                    { "Name", "myArrayLevel2" }, { "Index", 2 }, { "Obj", myArrayLevel2 }
                },
                new JsonObject {
                    { "Name", "myArrayLevel3" }, { "Index", 3 }, { "Obj", myArrayLevel3 }
                },
                new JsonObject {
                    { "Name", "myArrayLevel3" }, { "Index", 3 }, { "Obj", myArrayLevel3 }
                },
                new JsonObject {
                    { "Name", "myArrayLevel3" }, { "Index", 3 }, { "Obj", myArrayLevel3 }
                },
                new JsonObject {
                    { "Name", "myArrayLevel4" }, { "Index", 4 }, { "Obj", myArrayLevel4 }
                },
                new JsonObject {
                    { "Name", "myArrayLevel4" }, { "Index", 4 }, { "Obj", myArrayLevel4 }
                },
                new JsonObject {
                    { "Name", "myArrayLevel4" }, { "Index", 4 }, { "Obj", myArrayLevel4 }
                },
                new JsonObject {
                    { "Name", "myArrayLevel4" }, { "Index", 4 }, { "Obj", myArrayLevel4 }
                },
                new JsonObject {
                    { "Name", "myArrayLevel5" }, { "Index", 5 }, { "Obj", myArrayLevel5 }
                },
                new JsonObject {
                    { "Name", "myArrayLevel5" }, { "Index", 5 }, { "Obj", myArrayLevel5 }
                },
                new JsonObject {
                    { "Name", "myArrayLevel5" }, { "Index", 5 }, { "Obj", myArrayLevel5 }
                },
                new JsonObject {
                    { "Name", "myArrayLevel5" }, { "Index", 5 }, { "Obj", myArrayLevel5 }
                },
                new JsonObject {
                    { "Name", "myArrayLevel5" }, { "Index", 5 }, { "Obj", myArrayLevel5 }
                },
                new JsonObject {
                    { "Name", "myArrayLevel6" }, { "Index", 6 }, { "Obj", myArrayLevel6 }
                },
                new JsonObject {
                    { "Name", "myArrayLevel6" }, { "Index", 6 }, { "Obj", myArrayLevel6 }
                },
                new JsonObject {
                    { "Name", "myArrayLevel6" }, { "Index", 6 }, { "Obj", myArrayLevel6 }
                },
                new JsonObject {
                    { "Name", "myArrayLevel6" }, { "Index", 6 }, { "Obj", myArrayLevel6 }
                },
                new JsonObject {
                    { "Name", "myArrayLevel6" }, { "Index", 6 }, { "Obj", myArrayLevel6 }
                },
                new JsonObject {
                    { "Name", "myArrayLevel6" }, { "Index", 6 }, { "Obj", myArrayLevel6 }
                },
                new JsonObject {
                    { "Name", "myArrayLevel7" }, { "Index", 7 }, { "Obj", myArrayLevel7 }
                },
                new JsonObject {
                    { "Name", "myArrayLevel7" }, { "Index", 7 }, { "Obj", myArrayLevel7 }
                },
                new JsonObject {
                    { "Name", "myArrayLevel7" }, { "Index", 7 }, { "Obj", myArrayLevel7 }
                },
                new JsonObject {
                    { "Name", "myArrayLevel7" }, { "Index", 7 }, { "Obj", myArrayLevel7 }
                },
                new JsonObject {
                    { "Name", "myArrayLevel7" }, { "Index", 7 }, { "Obj", myArrayLevel7 }
                },
                new JsonObject {
                    { "Name", "myArrayLevel7" }, { "Index", 7 }, { "Obj", myArrayLevel7 }
                },
                new JsonObject {
                    { "Name", "myArrayLevel7" }, { "Index", 7 }, { "Obj", myArrayLevel7 }
                },
                new JsonObject {
                    { "Name", "myBool" }, { "Index", 8 }, { "Obj", myBool }
                },
                new JsonObject {
                    { "Name", "myByte" }, { "Index", 9 }, { "Obj", myByte }
                },
                null,
                new JsonObject {
                    { "Name", "myByte" }, { "Index", 9 }, { "Obj", myByte }
                },
                new JsonObject {
                    { "Name", "myDatetime" }, { "Index", 10 }, { "Obj", myDatetime }
                },
                new JsonObject {
                    { "Name", "myDatetime" }, { "Index", 10 }, { "Obj", myDatetime }
                },
                new JsonObject {
                    { "Name", "myDatetime" }, { "Index", 10 }, { "Obj", myDatetime }
                },
                new JsonObject {
                    { "Name", "myDateTimeOffset" }, { "Index", 11 }, { "Obj", myDateTimeOffset }
                },
                new JsonObject {
                    { "Name", "myDateTimeOffset" }, { "Index", 11 }, { "Obj", myDateTimeOffset }
                },
                new JsonObject {
                    { "Name", "myDateTimeOffset" }, { "Index", 11 }, { "Obj", myDateTimeOffset }
                },
                new JsonObject {
                    { "Name", "myDateTimeOffset" }, { "Index", 11 }, { "Obj", myDateTimeOffset }
                },
                new JsonObject {
                    { "Name", "myDecimal" }, { "Index", 12 }, { "Obj", myDecimal }
                },
                new JsonObject {
                    { "Name", "myDecimal" }, { "Index", 12 }, { "Obj", myDecimal }
                },
                new JsonObject {
                    { "Name", "myDecimal" }, { "Index", 12 }, { "Obj", myDecimal }
                },
                new JsonObject {
                    { "Name", "myDecimal" }, { "Index", 12 }, { "Obj", myDecimal }
                },
                new JsonObject {
                    { "Name", "myDecimal" }, { "Index", 12 }, { "Obj", myDecimal }
                },
                new JsonObject {
                    { "Name", "myDouble" }, { "Index", 13 }, { "Obj", myDouble }
                },
                new JsonObject {
                    { "Name", "myInt16" }, { "Index", 14 }, { "Obj", myInt16 }
                },
                new JsonObject {
                    { "Name", "myInt16" }, { "Index", 14 }, { "Obj", myInt16 }
                },
                new JsonObject {
                    { "Name", "myInt32" }, { "Index", 15 }, { "Obj", myInt32 }
                },
                new JsonObject {
                    { "Name", "myInt32" }, { "Index", 15 }, { "Obj", myInt32 }
                },
                new JsonObject {
                    { "Name", "myInt32" }, { "Index", 15 }, { "Obj", myInt32 }
                },
                new JsonObject {
                    { "Name", "myInt64" }, { "Index", 16 }, { "Obj", myInt64 }
                },
                new JsonObject {
                    { "Name", "myInt64" }, { "Index", 16 }, { "Obj", myInt64 }
                },
                new JsonObject {
                    { "Name", "myInt64" }, { "Index", 16 }, { "Obj", myInt64 }
                },
                new JsonObject {
                    { "Name", "myInt64" }, { "Index", 16 }, { "Obj", myInt64 }
                },
                new JsonObject {
                    { "Name", "mySByte" }, { "Index", 17 }, { "Obj", mySByte }
                },
                new JsonObject {
                    { "Name", "mySByte" }, { "Index", 17 }, { "Obj", mySByte }
                },
                new JsonObject {
                    { "Name", "mySByte" }, { "Index", 17 }, { "Obj", mySByte }
                },
                new JsonObject {
                    { "Name", "mySByte" }, { "Index", 17 }, { "Obj", mySByte }
                },
                new JsonObject {
                    { "Name", "mySByte" }, { "Index", 17 }, { "Obj", mySByte }
                },
                new JsonObject {
                    { "Name", "mySingle" }, { "Index", 18 }, { "Obj", mySingle }
                },
                new JsonObject {
                    { "Name", "myString" }, { "Index", 19 }, { "Obj", myString }
                },
                new JsonObject {
                    { "Name", "myString" }, { "Index", 19 }, { "Obj", myString }
                },
                new JsonObject {
                    { "Name", "myUInt16" }, { "Index", 20 }, { "Obj", myUInt16 }
                },
                new JsonObject {
                    { "Name", "myUInt16" }, { "Index", 20 }, { "Obj", myUInt16 }
                },
                new JsonObject {
                    { "Name", "myUInt16" }, { "Index", 20 }, { "Obj", myUInt16 }
                },
                new JsonObject {
                    { "Name", "myUInt32" }, { "Index", 21 }, { "Obj", myUInt32 }
                },
                new JsonObject {
                    { "Name", "myUInt32" }, { "Index", 21 }, { "Obj", myUInt32 }
                },
                new JsonObject {
                    { "Name", "myUInt32" }, { "Index", 21 }, { "Obj", myUInt32 }
                },
                new JsonObject {
                    { "Name", "myUInt32" }, { "Index", 21 }, { "Obj", myUInt32 }
                },
                new JsonObject {
                    { "Name", "myUInt64" }, { "Index", 22 }, { "Obj", myUInt64 }
                }
            };

            return(sourceJson);
        }
예제 #4
0
        JsonValue ReadCore()
        {
            SkipSpaces();
            int c = PeekChar();

            if (c < 0)
            {
                throw JsonError("Incomplete JSON input");
            }
            switch (c)
            {
            case '[':
                ReadChar();
                var list = new JsonArray();
                SkipSpaces();
                if (PeekChar() == ']')
                {
                    ReadChar();
                    return(list);
                }
                while (true)
                {
                    list.Add(ReadCore());
                    SkipSpaces();
                    c = PeekChar();
                    if (c != ',')
                    {
                        break;
                    }
                    ReadChar();
                    continue;
                }
                if (ReadChar() != ']')
                {
                    throw JsonError("JSON array must end with ']'");
                }
                return(list);

            case '{':
                ReadChar();
                var obj = new JsonObject();
                SkipSpaces();
                if (PeekChar() == '}')
                {
                    ReadChar();
                    return(obj);
                }
                while (true)
                {
                    SkipSpaces();
                    string name = ReadStringLiteral();
                    SkipSpaces();
                    Expect(':');
                    SkipSpaces();
                    obj.Add(name, ReadCore());
                    SkipSpaces();
                    c = ReadChar();
                    if (c == ',')
                    {
                        continue;
                    }
                    if (c == '}')
                    {
                        break;
                    }
                }
                return(obj);

            case 't':
                Expect("true");
                return(new JsonPrimitive(true));

            case 'f':
                Expect("false");
                return(new JsonPrimitive(false));

            case 'n':
                Expect("null");
                // FIXME: what should we return?
                return(new JsonPrimitive((string)null));

            case '"':
                return(new JsonPrimitive(ReadStringLiteral()));

            default:
                if ('0' <= c && c <= '9' || c == '-')
                {
                    return(ReadNumericLiteral());
                }
                else
                {
                    throw JsonError(String.Format("Unexpected character '{0}'", (char)c));
                }
            }
        }
예제 #5
0
        public void JsonObjectEventsTest()
        {
            int       seed   = 1;
            const int maxObj = 10;

            const string key1 = "first";
            const string key2 = "second";
            const string key3 = "third";
            const string key4 = "fourth";
            const string key5 = "fifth";
            JsonObject   jo   = new JsonObject
            {
                { key1, SpecialJsonValueHelper.GetRandomJsonPrimitives(seed) },
                { key2, SpecialJsonValueHelper.GetRandomJsonPrimitives(seed) },
                { key3, null },
            };

            JsonObject objToAdd = SpecialJsonValueHelper.CreateRandomPopulatedJsonObject(seed, maxObj);

            TestEvents(
                jo,
                obj => obj.Add(key4, objToAdd),
                new List <Tuple <bool, JsonValue, JsonValueChangeEventArgs> >
            {
                new Tuple <bool, JsonValue, JsonValueChangeEventArgs>(true, jo, new JsonValueChangeEventArgs(objToAdd, JsonValueChange.Add, key4)),
                new Tuple <bool, JsonValue, JsonValueChangeEventArgs>(false, jo, new JsonValueChangeEventArgs(objToAdd, JsonValueChange.Add, key4)),
            },
                obj => obj.Add("key44", objToAdd));

            JsonArray jaToAdd  = SpecialJsonValueHelper.CreatePrePopulatedJsonArray(seed, maxObj);
            JsonValue replaced = jo[key2];

            TestEvents(
                jo,
                obj => obj[key2] = jaToAdd,
                new List <Tuple <bool, JsonValue, JsonValueChangeEventArgs> >
            {
                new Tuple <bool, JsonValue, JsonValueChangeEventArgs>(true, jo, new JsonValueChangeEventArgs(jaToAdd, JsonValueChange.Replace, key2)),
                new Tuple <bool, JsonValue, JsonValueChangeEventArgs>(false, jo, new JsonValueChangeEventArgs(replaced, JsonValueChange.Replace, key2)),
            });

            JsonValue jpToAdd = SpecialJsonValueHelper.GetRandomJsonPrimitives(seed);

            TestEvents(
                jo,
                obj => obj[key5] = jpToAdd,
                new List <Tuple <bool, JsonValue, JsonValueChangeEventArgs> >
            {
                new Tuple <bool, JsonValue, JsonValueChangeEventArgs>(true, jo, new JsonValueChangeEventArgs(jpToAdd, JsonValueChange.Add, key5)),
                new Tuple <bool, JsonValue, JsonValueChangeEventArgs>(false, jo, new JsonValueChangeEventArgs(jpToAdd, JsonValueChange.Add, key5)),
            });

            jo.Remove(key4);
            jo.Remove(key5);

            JsonValue jp1 = SpecialJsonValueHelper.GetRandomJsonPrimitives(seed);
            JsonValue jp2 = SpecialJsonValueHelper.GetRandomJsonPrimitives(seed);

            TestEvents(
                jo,
                obj => obj.AddRange(new JsonObject {
                { key4, jp1 }, { key5, jp1 }
            }),
                new List <Tuple <bool, JsonValue, JsonValueChangeEventArgs> >
            {
                new Tuple <bool, JsonValue, JsonValueChangeEventArgs>(true, jo, new JsonValueChangeEventArgs(jp1, JsonValueChange.Add, key4)),
                new Tuple <bool, JsonValue, JsonValueChangeEventArgs>(true, jo, new JsonValueChangeEventArgs(jp2, JsonValueChange.Add, key5)),
                new Tuple <bool, JsonValue, JsonValueChangeEventArgs>(false, jo, new JsonValueChangeEventArgs(jp1, JsonValueChange.Add, key4)),
                new Tuple <bool, JsonValue, JsonValueChangeEventArgs>(false, jo, new JsonValueChangeEventArgs(jp2, JsonValueChange.Add, key5)),
            },
                obj => obj.AddRange(new JsonObject {
                { "new key", jp1 }, { "newnewKey", jp2 }
            }));

            TestEvents(
                jo,
                obj => obj.Remove(key5),
                new List <Tuple <bool, JsonValue, JsonValueChangeEventArgs> >
            {
                new Tuple <bool, JsonValue, JsonValueChangeEventArgs>(true, jo, new JsonValueChangeEventArgs(jp2, JsonValueChange.Remove, key5)),
                new Tuple <bool, JsonValue, JsonValueChangeEventArgs>(false, jo, new JsonValueChangeEventArgs(jp2, JsonValueChange.Remove, key5)),
            });

            TestEvents(
                jo,
                obj => obj.Remove("not there"),
                new List <Tuple <bool, JsonValue, JsonValueChangeEventArgs> >());

            jo = new JsonObject {
                { key1, 1 }, { key2, 2 }, { key3, 3 }
            };

            TestEvents(
                jo,
                obj => obj.Clear(),
                new List <Tuple <bool, JsonValue, JsonValueChangeEventArgs> >
            {
                new Tuple <bool, JsonValue, JsonValueChangeEventArgs>(true, jo, new JsonValueChangeEventArgs(null, JsonValueChange.Clear, null)),
                new Tuple <bool, JsonValue, JsonValueChangeEventArgs>(false, jo, new JsonValueChangeEventArgs(null, JsonValueChange.Clear, null)),
            });

            jo = new JsonObject {
                { key1, 1 }, { key2, 2 }, { key3, 3 }
            };
            TestEvents(
                jo,
                obj => ((IDictionary <string, JsonValue>)obj).Remove(new KeyValuePair <string, JsonValue>(key2, jo[key2])),
                new List <Tuple <bool, JsonValue, JsonValueChangeEventArgs> >
            {
                new Tuple <bool, JsonValue, JsonValueChangeEventArgs>(true, jo, new JsonValueChangeEventArgs(2, JsonValueChange.Remove, key2)),
                new Tuple <bool, JsonValue, JsonValueChangeEventArgs>(false, jo, new JsonValueChangeEventArgs(2, JsonValueChange.Remove, key2)),
            },
                obj => ((IDictionary <string, JsonValue>)obj).Remove(new KeyValuePair <string, JsonValue>(key1, jo[key1])));
        }
예제 #6
0
        public static JsonValue JXMLToJsonValue(XmlDictionaryReader jsonReader)
        {
            if (jsonReader == null)
            {
                throw new ArgumentNullException("jsonReader");
            }

            const string      RootObjectName = "RootObject";
            Stack <JsonValue> jsonStack      = new Stack <JsonValue>();
            string            nodeType       = null;
            bool isEmptyElement = false;

            JsonValue parent = new JsonObject();

            jsonStack.Push(parent);
            string currentName = RootObjectName;

            try
            {
                MoveToRootNode(jsonReader);

                while (jsonStack.Count > 0 && jsonReader.NodeType != XmlNodeType.None)
                {
                    if (parent is JsonObject && currentName == null)
                    {
                        currentName = GetMemberName(jsonReader);
                    }

                    nodeType = jsonReader.GetAttribute(TypeAttributeName) ?? StringAttributeValue;

                    if (parent is JsonArray)
                    {
                        // For arrays, the element name has to be "item"
                        if (jsonReader.Name != ItemElementName)
                        {
                            throw new FormatException(Properties.Resources.IncorrectJsonFormat);
                        }
                    }

                    switch (nodeType)
                    {
                    case NullAttributeValue:
                    case BooleanAttributeValue:
                    case StringAttributeValue:
                    case NumberAttributeValue:
                        JsonPrimitive jsonPrimitive = ReadPrimitive(nodeType, jsonReader);
                        InsertJsonValue(jsonStack, ref parent, ref currentName, jsonPrimitive, true);
                        break;

                    case ArrayAttributeValue:
                        JsonArray jsonArray = CreateJsonArray(jsonReader, ref isEmptyElement);
                        InsertJsonValue(jsonStack, ref parent, ref currentName, jsonArray, isEmptyElement);
                        break;

                    case ObjectAttributeValue:
                        JsonObject jsonObject = CreateObjectWithTypeHint(jsonReader, ref isEmptyElement);
                        InsertJsonValue(jsonStack, ref parent, ref currentName, jsonObject, isEmptyElement);
                        break;

                    default:
                        throw new FormatException(Properties.Resources.IncorrectJsonFormat);
                    }

                    while (jsonReader.NodeType == XmlNodeType.EndElement && jsonStack.Count > 0)
                    {
                        jsonReader.Read();
                        SkipWhitespace(jsonReader);
                        jsonStack.Pop();
                        if (jsonStack.Count > 0)
                        {
                            parent = jsonStack.Peek();
                        }
                    }
                }
            }
            catch (XmlException xmlException)
            {
                throw new FormatException(Properties.Resources.IncorrectJsonFormat, xmlException);
            }

            if (jsonStack.Count != 1)
            {
                throw new FormatException(Properties.Resources.IncorrectJsonFormat);
            }

            return(parent[RootObjectName]);
        }
예제 #7
0
        public void JsonArrayEventsTest()
        {
            int       seed           = 1;
            const int maxArrayLength = 1024;
            Random    rand           = new Random(seed);
            JsonArray ja             = SpecialJsonValueHelper.CreatePrePopulatedJsonArray(seed, rand.Next(maxArrayLength));
            int       addPosition    = ja.Count;
            JsonValue insertValue    = SpecialJsonValueHelper.GetRandomJsonPrimitives(seed);

            TestEvents(
                ja,
                arr => arr.Add(insertValue),
                new List <Tuple <bool, JsonValue, JsonValueChangeEventArgs> >
            {
                new Tuple <bool, JsonValue, JsonValueChangeEventArgs>(true, ja, new JsonValueChangeEventArgs(insertValue, JsonValueChange.Add, addPosition)),
                new Tuple <bool, JsonValue, JsonValueChangeEventArgs>(false, ja, new JsonValueChangeEventArgs(insertValue, JsonValueChange.Add, addPosition)),
            });

            addPosition = ja.Count;
            JsonValue jv1 = SpecialJsonValueHelper.GetRandomJsonPrimitives(seed);
            JsonValue jv2 = SpecialJsonValueHelper.GetRandomJsonPrimitives(seed);

            TestEvents(
                ja,
                arr => arr.AddRange(jv1, jv2),
                new List <Tuple <bool, JsonValue, JsonValueChangeEventArgs> >
            {
                new Tuple <bool, JsonValue, JsonValueChangeEventArgs>(true, ja,
                                                                      new JsonValueChangeEventArgs(
                                                                          jv1,
                                                                          JsonValueChange.Add, addPosition)),
                new Tuple <bool, JsonValue, JsonValueChangeEventArgs>(true, ja,
                                                                      new JsonValueChangeEventArgs(
                                                                          jv2,
                                                                          JsonValueChange.Add, addPosition + 1)),
                new Tuple <bool, JsonValue, JsonValueChangeEventArgs>(false, ja,
                                                                      new JsonValueChangeEventArgs(
                                                                          jv1,
                                                                          JsonValueChange.Add, addPosition)),
                new Tuple <bool, JsonValue, JsonValueChangeEventArgs>(false, ja,
                                                                      new JsonValueChangeEventArgs(
                                                                          jv2,
                                                                          JsonValueChange.Add, addPosition + 1)),
            });

            int       replacePosition = rand.Next(ja.Count - 1);
            JsonValue oldValue        = ja[replacePosition];
            JsonValue newValue        = SpecialJsonValueHelper.GetRandomJsonPrimitives(seed);

            TestEvents(
                ja,
                arr => arr[replacePosition] = newValue,
                new List <Tuple <bool, JsonValue, JsonValueChangeEventArgs> >
            {
                new Tuple <bool, JsonValue, JsonValueChangeEventArgs>(true, ja, new JsonValueChangeEventArgs(newValue, JsonValueChange.Replace, replacePosition)),
                new Tuple <bool, JsonValue, JsonValueChangeEventArgs>(false, ja, new JsonValueChangeEventArgs(oldValue, JsonValueChange.Replace, replacePosition)),
            });

            int insertPosition = rand.Next(ja.Count - 1);

            insertValue = SpecialJsonValueHelper.GetRandomJsonPrimitives(seed);

            TestEvents(
                ja,
                arr => arr.Insert(insertPosition, insertValue),
                new List <Tuple <bool, JsonValue, JsonValueChangeEventArgs> >
            {
                new Tuple <bool, JsonValue, JsonValueChangeEventArgs>(true, ja, new JsonValueChangeEventArgs(insertValue, JsonValueChange.Add, insertPosition)),
                new Tuple <bool, JsonValue, JsonValueChangeEventArgs>(false, ja, new JsonValueChangeEventArgs(insertValue, JsonValueChange.Add, insertPosition)),
            });

            TestEvents(
                ja,
                arr => arr.RemoveAt(insertPosition),
                new List <Tuple <bool, JsonValue, JsonValueChangeEventArgs> >
            {
                new Tuple <bool, JsonValue, JsonValueChangeEventArgs>(true, ja, new JsonValueChangeEventArgs(insertValue, JsonValueChange.Remove, insertPosition)),
                new Tuple <bool, JsonValue, JsonValueChangeEventArgs>(false, ja, new JsonValueChangeEventArgs(insertValue, JsonValueChange.Remove, insertPosition)),
            });

            ja.Insert(0, insertValue);
            TestEvents(
                ja,
                arr => arr.Remove(insertValue),
                new List <Tuple <bool, JsonValue, JsonValueChangeEventArgs> >
            {
                new Tuple <bool, JsonValue, JsonValueChangeEventArgs>(true, ja, new JsonValueChangeEventArgs(insertValue, JsonValueChange.Remove, 0)),
                new Tuple <bool, JsonValue, JsonValueChangeEventArgs>(false, ja, new JsonValueChangeEventArgs(insertValue, JsonValueChange.Remove, 0)),
            });

            TestEvents(
                ja,
                arr => arr.Clear(),
                new List <Tuple <bool, JsonValue, JsonValueChangeEventArgs> >
            {
                new Tuple <bool, JsonValue, JsonValueChangeEventArgs>(true, ja, new JsonValueChangeEventArgs(null, JsonValueChange.Clear, 0)),
                new Tuple <bool, JsonValue, JsonValueChangeEventArgs>(false, ja, new JsonValueChangeEventArgs(null, JsonValueChange.Clear, 0)),
            });

            ja = new JsonArray(1, 2, 3);
            TestEvents(
                ja,
                arr => arr.Remove(new JsonPrimitive("Not there")),
                new List <Tuple <bool, JsonValue, JsonValueChangeEventArgs> >());

            JsonValue elementInArray = ja[1];

            TestEvents(
                ja,
                arr => arr.Remove(elementInArray),
                new List <Tuple <bool, JsonValue, JsonValueChangeEventArgs> >
            {
                new Tuple <bool, JsonValue, JsonValueChangeEventArgs>(true, ja, new JsonValueChangeEventArgs(elementInArray, JsonValueChange.Remove, 1)),
                new Tuple <bool, JsonValue, JsonValueChangeEventArgs>(false, ja, new JsonValueChangeEventArgs(elementInArray, JsonValueChange.Remove, 1)),
            });
        }
예제 #8
0
 private static void TestEvents(JsonArray array, Action <JsonArray> actionToTriggerEvent, List <Tuple <bool, JsonValue, JsonValueChangeEventArgs> > expectedEvents)
 {
     TestEvents <JsonArray>(array, actionToTriggerEvent, expectedEvents);
 }
예제 #9
0
        public void JsonTypeTest()
        {
            JsonArray target = AnyInstance.AnyJsonArray;

            Assert.Equal(JsonType.Array, target.JsonType);
        }
예제 #10
0
        public void ChangingEventsTest()
        {
            JsonArray ja = new JsonArray(AnyInstance.AnyInt, AnyInstance.AnyBool, null);

            TestEvents(
                ja,
                arr => arr.Add(1),
                new List <Tuple <bool, JsonValue, JsonValueChangeEventArgs> >
            {
                new Tuple <bool, JsonValue, JsonValueChangeEventArgs>(true, ja, new JsonValueChangeEventArgs(1, JsonValueChange.Add, 3)),
                new Tuple <bool, JsonValue, JsonValueChangeEventArgs>(false, ja, new JsonValueChangeEventArgs(1, JsonValueChange.Add, 3)),
            });

            TestEvents(
                ja,
                arr => arr.AddRange(AnyInstance.AnyString, AnyInstance.AnyDouble),
                new List <Tuple <bool, JsonValue, JsonValueChangeEventArgs> >
            {
                new Tuple <bool, JsonValue, JsonValueChangeEventArgs>(true, ja, new JsonValueChangeEventArgs(AnyInstance.AnyString, JsonValueChange.Add, 4)),
                new Tuple <bool, JsonValue, JsonValueChangeEventArgs>(true, ja, new JsonValueChangeEventArgs(AnyInstance.AnyDouble, JsonValueChange.Add, 5)),
                new Tuple <bool, JsonValue, JsonValueChangeEventArgs>(false, ja, new JsonValueChangeEventArgs(AnyInstance.AnyString, JsonValueChange.Add, 4)),
                new Tuple <bool, JsonValue, JsonValueChangeEventArgs>(false, ja, new JsonValueChangeEventArgs(AnyInstance.AnyDouble, JsonValueChange.Add, 5)),
            });

            TestEvents(
                ja,
                arr => arr[1] = 2,
                new List <Tuple <bool, JsonValue, JsonValueChangeEventArgs> >
            {
                new Tuple <bool, JsonValue, JsonValueChangeEventArgs>(true, ja, new JsonValueChangeEventArgs(2, JsonValueChange.Replace, 1)),
                new Tuple <bool, JsonValue, JsonValueChangeEventArgs>(false, ja, new JsonValueChangeEventArgs(AnyInstance.AnyBool, JsonValueChange.Replace, 1)),
            });

            ja = new JsonArray {
                1, 2, 3
            };
            TestEvents(
                ja,
                arr => arr.Insert(1, "new value"),
                new List <Tuple <bool, JsonValue, JsonValueChangeEventArgs> >
            {
                new Tuple <bool, JsonValue, JsonValueChangeEventArgs>(true, ja, new JsonValueChangeEventArgs("new value", JsonValueChange.Add, 1)),
                new Tuple <bool, JsonValue, JsonValueChangeEventArgs>(false, ja, new JsonValueChangeEventArgs("new value", JsonValueChange.Add, 1)),
            });

            TestEvents(
                ja,
                arr => arr.RemoveAt(1),
                new List <Tuple <bool, JsonValue, JsonValueChangeEventArgs> >
            {
                new Tuple <bool, JsonValue, JsonValueChangeEventArgs>(true, ja, new JsonValueChangeEventArgs("new value", JsonValueChange.Remove, 1)),
                new Tuple <bool, JsonValue, JsonValueChangeEventArgs>(false, ja, new JsonValueChangeEventArgs("new value", JsonValueChange.Remove, 1)),
            });

            TestEvents(
                ja,
                arr => arr.Clear(),
                new List <Tuple <bool, JsonValue, JsonValueChangeEventArgs> >
            {
                new Tuple <bool, JsonValue, JsonValueChangeEventArgs>(true, ja, new JsonValueChangeEventArgs(null, JsonValueChange.Clear, 0)),
                new Tuple <bool, JsonValue, JsonValueChangeEventArgs>(false, ja, new JsonValueChangeEventArgs(null, JsonValueChange.Clear, 0)),
            });

            ja = new JsonArray(1, 2, 3);
            TestEvents(
                ja,
                arr => arr.Remove(new JsonPrimitive("Not there")),
                new List <Tuple <bool, JsonValue, JsonValueChangeEventArgs> >());

            JsonValue elementInArray = ja[1];

            TestEvents(
                ja,
                arr => arr.Remove(elementInArray),
                new List <Tuple <bool, JsonValue, JsonValueChangeEventArgs> >
            {
                new Tuple <bool, JsonValue, JsonValueChangeEventArgs>(true, ja, new JsonValueChangeEventArgs(elementInArray, JsonValueChange.Remove, 1)),
                new Tuple <bool, JsonValue, JsonValueChangeEventArgs>(false, ja, new JsonValueChangeEventArgs(elementInArray, JsonValueChange.Remove, 1)),
            });
        }
예제 #11
0
        public void IsReadOnlyTest()
        {
            JsonArray target = AnyInstance.AnyJsonArray;

            Assert.False(target.IsReadOnly);
        }