Beispiel #1
0
    /// <summary>
    /// Registers file extension mapping.
    /// </summary>
    /// <param name="json">The source.</param>
    /// <param name="overrideIfExist">true if override the existed one; otherwise, false.</param>
    /// <returns>The count of item added or changed.</returns>
    public static int RegisterFileExtensionMapping(JsonArrayNode json, bool overrideIfExist = false)
    {
        var mapping = FileExtensionMapping;

        if (json == null || mapping == null)
        {
            return(0);
        }
        var i = 0;

        foreach (var item in json)
        {
            if (item is not JsonObjectNode ele)
            {
                continue;
            }
            var ext  = ele.TryGetStringValue("extension") ?? ele.TryGetStringValue("ext");
            var mime = ele.TryGetStringValue("mime");
            if (mapping.Set(mime, ext, overrideIfExist))
            {
                i++;
            }
        }

        return(i);
    }
        public void ToObject_Collection_Custom()
        {
            // Arrange
            var arrayNode = new JsonArrayNode();

            arrayNode.Add(new JsonIntegerNode(1));
            arrayNode.Add(new JsonIntegerNode(2));
            arrayNode.Add(new JsonIntegerNode(4));
            arrayNode.Add(new JsonIntegerNode(8));
            arrayNode.Add(new JsonIntegerNode(16));

            // Act
            var categorySet = arrayNode.ConvertTo <CategorySet>();

            // Assert
            Assert.IsNotNull(categorySet);
            Assert.AreEqual(5, categorySet.Count);
            Assert.IsTrue(categorySet.Contains(1));
            Assert.IsTrue(categorySet.Contains(2));
            Assert.IsTrue(categorySet.Contains(4));
            Assert.IsTrue(categorySet.Contains(8));
            Assert.IsTrue(categorySet.Contains(16));
            Assert.IsFalse(categorySet.Contains(0));
            Assert.IsFalse(categorySet.Contains(-1));
            Assert.IsFalse(categorySet.Contains(-2));
            Assert.IsFalse(categorySet.Contains(3));
        }
        public void FromCollection_ArrayList_Mixed()
        {
            // Arrange
            var mixed = new ArrayList(new object[] { "A", 1, "C", 2, "E", 3 });

            // Act
            var arrayNode = JsonArrayNode.FromCollection(mixed);

            // Assert
            Assert.IsNotNull(arrayNode);
            Assert.AreEqual(6, arrayNode.Count);

            for (int i = 0; i < mixed.Count; ++i)
            {
                var elementNode = arrayNode[i];

                if (elementNode is JsonIntegerNode)
                {
                    var integerNode = elementNode as JsonIntegerNode;
                    Assert.AreEqual(mixed[i], (int)integerNode.Value);
                }
                else if (elementNode is JsonStringNode)
                {
                    var stringNode = elementNode as JsonStringNode;
                    Assert.AreEqual(mixed[i], stringNode.Value);
                }
                else
                {
                    Assert.Fail("Invalid node type.");
                }
            }
        }
        public void FromArray_MixedElements()
        {
            // Arrange
            object[] mixed = { "A", 1, "C", 2, "E", 3 };

            // Act
            var arrayNode = JsonArrayNode.FromArray(mixed);

            // Assert
            Assert.IsNotNull(arrayNode);
            Assert.AreEqual(mixed.Length, arrayNode.Count);

            for (int i = 0; i < mixed.Length; ++i)
            {
                var elementNode = arrayNode[i];

                if (elementNode is JsonIntegerNode)
                {
                    var integerNode = elementNode as JsonIntegerNode;
                    Assert.AreEqual(mixed[i], (int)integerNode.Value);
                }
                else if (elementNode is JsonStringNode)
                {
                    var stringNode = elementNode as JsonStringNode;
                    Assert.AreEqual(mixed[i], stringNode.Value);
                }
                else
                {
                    Assert.Fail("Invalid node type.");
                }
            }
        }
        public void FromCollection_Null()
        {
            // Arrange
            ICollection collection = null;

            // Act
            JsonArrayNode.FromCollection(collection);
        }
Beispiel #6
0
        public static JsonArrayNode CreateIdentityCardArray()
        {
            var cards = new JsonArrayNode();

            cards.Add(CreateIdentityCard("Bob"));
            cards.Add(CreateIdentityCard("Jessica"));
            return(cards);
        }
        public void FromArray_Null()
        {
            // Arrange
            object[] array = null;

            // Act
            JsonArrayNode.FromArray(array);
        }
        public void ToObject_Null()
        {
            // Arrange
            var  arrayNode = new JsonArrayNode();
            Type type      = null;

            // Act
            arrayNode.ConvertTo(type);
        }
        public void Create_Array_InitialLength()
        {
            // Arrange

            // Act
            var arrayNode = new JsonArrayNode(10);

            // Assert
            Assert.AreEqual(10, arrayNode.Count);
        }
        public void Create_Array()
        {
            // Arrange

            // Act
            var arrayNode = new JsonArrayNode();

            // Assert
            Assert.AreEqual(0, arrayNode.Count);
        }
        public void Create_List_Empty()
        {
            // Arrange
            var list = new List <JsonNode>();

            // Act
            var arrayNode = new JsonArrayNode(list);

            // Assert
            Assert.AreEqual(0, arrayNode.Count);
        }
        public void Create_Array_Empty()
        {
            // Arrange
            JsonNode[] array = { };

            // Act
            var arrayNode = new JsonArrayNode(new JsonNode[0]);

            // Assert
            Assert.AreEqual(0, arrayNode.Count);
        }
Beispiel #13
0
        public static JsonArrayNode CreateNumericArrayWithEvenNumbers()
        {
            var arr = new JsonArrayNode();

            arr.Add(new JsonIntegerNode(0));
            arr.Add(new JsonIntegerNode(2));
            arr.Add(new JsonIntegerNode(4));
            arr.Add(new JsonIntegerNode(6));
            arr.Add(new JsonIntegerNode(8));
            arr.Add(new JsonIntegerNode(10));
            return(arr);
        }
        public void FromArray_Empty()
        {
            // Arrange
            object[] array = { };

            // Act
            var arrayNode = JsonArrayNode.FromArray(array);

            // Assert
            Assert.IsNotNull(arrayNode);
            Assert.AreEqual(0, arrayNode.Count);
        }
        public void FromCollection_List_Empty()
        {
            // Arrange
            var list = new List <int>();

            // Act
            var arrayNode = JsonArrayNode.FromCollection(list);

            // Assert
            Assert.IsNotNull(arrayNode);
            Assert.AreEqual(0, arrayNode.Count);
        }
        public void ToString_Empty()
        {
            // Arrange
            var arrayNode = new JsonArrayNode();

            // Act
            string result         = arrayNode.ToString();
            string expectedResult = "[]";

            // Assert
            Assert.AreEqual(expectedResult, result);
        }
        public void ToString_StringArray()
        {
            // Arrange
            var arrayNode = JsonArrayNode.FromArray(new string[] { "A", "B" });

            // Act
            string result         = arrayNode.ToString();
            string expectedResult = File.ReadAllText("ToString_StringArray.json");

            // Assert
            Assert.AreEqual(expectedResult, result);
        }
        public void Clone_Empty()
        {
            // Arrange
            var arrayNode = new JsonArrayNode();

            // Act
            var cloneNode = arrayNode.Clone() as JsonArrayNode;

            // Assert
            Assert.IsNotNull(cloneNode);
            Assert.AreNotSame(arrayNode, cloneNode);
            Assert.AreEqual(0, cloneNode.Count);
        }
Beispiel #19
0
            static EmbeddedDiagnostic?CheckArray(JsonArrayNode node)
            {
                foreach (var child in node.Sequence)
                {
                    if (child.Kind == JsonKind.Property)
                    {
                        return(new EmbeddedDiagnostic(
                                   FeaturesResources.Properties_not_allowed_in_an_array,
                                   ((JsonPropertyNode)child).ColonToken.GetSpan()));
                    }
                }

                return(null);
            }
        public void WriteTo_Empty()
        {
            // Arrange
            var arrayNode = new JsonArrayNode();
            var writer    = JsonWriter.Create();

            // Act
            arrayNode.Write(writer);

            // Assert
            string result         = writer.ToString();
            string expectedResult = "[]";

            Assert.AreEqual(expectedResult, result);
        }
        public void WriteTo_StringArray()
        {
            // Arrange
            var arrayNode = JsonArrayNode.FromArray(new string[] { "A", "B" });
            var writer    = JsonWriter.Create();

            // Act
            arrayNode.Write(writer);

            // Assert
            string result         = writer.ToString();
            string expectedResult = File.ReadAllText("ToString_StringArray.json");

            Assert.AreEqual(expectedResult, result);
        }
        public void Clone_NumberElementsWithNullElement()
        {
            // Arrange
            var arrayNode = JsonArrayNode.FromArray(new object[] { 1, null, 5 });

            // Act
            var cloneNode = arrayNode.Clone() as JsonArrayNode;

            // Assert
            Assert.IsNotNull(cloneNode);
            Assert.AreNotSame(arrayNode, cloneNode);
            Assert.AreEqual(3, cloneNode.Count);

            Assert.AreEqual(1, (int)(cloneNode[0] as JsonIntegerNode).Value);
            Assert.IsNull(cloneNode[1]);
            Assert.AreEqual(5, (int)(cloneNode[2] as JsonIntegerNode).Value);
        }
        public void Create_List_WitherIntegerNodes()
        {
            // Arrange
            var list = new List <JsonNode>();

            list.Add(new JsonIntegerNode(1));
            list.Add(new JsonIntegerNode(2));
            list.Add(new JsonIntegerNode(3));

            // Act
            var arrayNode = new JsonArrayNode(list);

            // Assert
            Assert.AreEqual(3, arrayNode.Count);
            Assert.AreEqual(1, (int)(arrayNode[0] as JsonIntegerNode).Value);
            Assert.AreEqual(2, (int)(arrayNode[1] as JsonIntegerNode).Value);
            Assert.AreEqual(3, (int)(arrayNode[2] as JsonIntegerNode).Value);
        }
        public void Create_Array_WithIntegerNodes()
        {
            // Arrange
            JsonNode[] nodes =
            {
                new JsonIntegerNode(1),
                new JsonIntegerNode(2),
                new JsonIntegerNode(3)
            };

            // Act
            var arrayNode = new JsonArrayNode(nodes);

            // Assert
            Assert.AreEqual(3, arrayNode.Count);
            Assert.AreEqual(1, (int)(arrayNode[0] as JsonIntegerNode).Value);
            Assert.AreEqual(2, (int)(arrayNode[1] as JsonIntegerNode).Value);
            Assert.AreEqual(3, (int)(arrayNode[2] as JsonIntegerNode).Value);
        }
        public void FromArray_StringElements()
        {
            // Arrange
            string[] strings = { "A", "B", "C", "D", "E" };

            // Act
            var arrayNode = JsonArrayNode.FromArray(strings);

            // Assert
            Assert.IsNotNull(arrayNode);
            Assert.AreEqual(strings.Length, arrayNode.Count);

            for (int i = 0; i < strings.Length; ++i)
            {
                var elementNode = arrayNode[i] as JsonStringNode;
                Assert.IsNotNull(elementNode);
                Assert.AreEqual(strings[i], elementNode.Value);
            }
        }
        public void FromCollection_List_Numbers()
        {
            // Arrange
            var numbers = new List <int>(new int[] { 2, 4, 6, 8, 10 });

            // Act
            var arrayNode = JsonArrayNode.FromCollection(numbers);

            // Assert
            Assert.IsNotNull(arrayNode);
            Assert.AreEqual(5, arrayNode.Count);

            for (int i = 0; i < numbers.Count; ++i)
            {
                var elementNode = arrayNode[i] as JsonIntegerNode;
                Assert.IsNotNull(elementNode);
                Assert.AreEqual(numbers[i], (int)elementNode.Value);
            }
        }
        public void FromCollection_List_Strings()
        {
            // Arrange
            var strings = new List <string>(new string[] { "A", "B", "C", "D", "E" });

            // Act
            var arrayNode = JsonArrayNode.FromCollection(strings);

            // Assert
            Assert.IsNotNull(arrayNode);
            Assert.AreEqual(5, arrayNode.Count);

            for (int i = 0; i < strings.Count; ++i)
            {
                var elementNode = arrayNode[i] as JsonStringNode;
                Assert.IsNotNull(elementNode);
                Assert.AreEqual(strings[i], elementNode.Value);
            }
        }
        public void FromArray_NumericElements()
        {
            // Arrange
            int[] numbers = { 2, 4, 6, 8, 10 };

            // Act
            var arrayNode = JsonArrayNode.FromArray(numbers);

            // Assert
            Assert.IsNotNull(arrayNode);
            Assert.AreEqual(numbers.Length, arrayNode.Count);

            for (int i = 0; i < numbers.Length; ++i)
            {
                var elementNode = arrayNode[i] as JsonIntegerNode;
                Assert.IsNotNull(elementNode);
                Assert.AreEqual(numbers[i], (int)elementNode.Value);
            }
        }
        public void Clone_NumberElements()
        {
            // Arrange
            var arrayNode = JsonArrayNode.FromArray(new int[] { 1, 2, 3, 4, 5 });

            // Act
            var cloneNode = arrayNode.Clone() as JsonArrayNode;

            // Assert
            Assert.IsNotNull(cloneNode);
            Assert.AreNotSame(arrayNode, cloneNode);
            Assert.AreEqual(5, cloneNode.Count);

            Assert.AreEqual(1, (int)(cloneNode[0] as JsonIntegerNode).Value);
            Assert.AreEqual(2, (int)(cloneNode[1] as JsonIntegerNode).Value);
            Assert.AreEqual(3, (int)(cloneNode[2] as JsonIntegerNode).Value);
            Assert.AreEqual(4, (int)(cloneNode[3] as JsonIntegerNode).Value);
            Assert.AreEqual(5, (int)(cloneNode[4] as JsonIntegerNode).Value);
        }
        private JsonArrayNode ProduceJsonArray(JsonTokenizer tokenizer)
        {
            var lsq = NextAndGetType(tokenizer); // pop the leading [.

            if (lsq != TokenType.LSQUARE)
            {
                // this cannot happen.
                throw new InvalidJsonTokenException("JSON array should start with [.", GetTailCharStream(tokenizer), PeekCharStream(tokenizer));
            }

            IList <object> list = new List <object>();
            var            type = PeekAndGetType(tokenizer);

            if (type == TokenType.RSQUARE)
            {
                // empty array
                tokenizer.Next(); // discard the trailing ].
            }
            else
            {
                IList <JsonNode> elements = ProduceJsonArrayElements(tokenizer);

                var rsq = NextAndGetType(tokenizer); // discard the trailing ].
                if (rsq == TokenType.RSQUARE)
                {
                    // Done
                    ((List <object>)list).AddRange(elements);
                }
                else
                {
                    // ???
                    throw new InvalidJsonTokenException("JSON array should end with ].", GetTailCharStream(tokenizer), PeekCharStream(tokenizer));
                }
            }
            JsonArrayNode jArray = (JsonArrayNode)jsonTypeFactory.CreateArray(list);

            //if (log.isLoggable(Level.FINE)) {
            //    log.fine("jArray = " + jArray);
            //}
            return(jArray);
        }