internal static JsonTokenInfo[] GetTokensFromNode(IJsonNavigatorNode node, IJsonNavigator navigator, bool performCorrectnessCheck)
        {
            switch (navigator.GetNodeType(node))
            {
            case JsonNodeType.Null:
                return(new JsonTokenInfo[] { JsonTokenInfo.Null() });

            case JsonNodeType.False:
                return(new JsonTokenInfo[] { JsonTokenInfo.Boolean(false) });

            case JsonNodeType.True:
                return(new JsonTokenInfo[] { JsonTokenInfo.Boolean(true) });

            case JsonNodeType.Number:
                return(new JsonTokenInfo[] { JsonTokenInfo.Number(navigator.GetNumberValue(node)) });

            case JsonNodeType.String:
                return(new JsonTokenInfo[] { JsonTokenInfo.String(navigator.GetStringValue(node)) });

            case JsonNodeType.Array:
                return(JsonNavigatorTests.GetTokensFromArrayNode(node, navigator, performCorrectnessCheck));

            case JsonNodeType.Object:
                return(JsonNavigatorTests.GetTokensFromObjectNode(node, navigator, performCorrectnessCheck));

            case JsonNodeType.FieldName:
                return(new JsonTokenInfo[] { JsonTokenInfo.FieldName(navigator.GetStringValue(node)) });

            default:
                throw new InvalidOperationException();
            }
        }
        internal static JsonToken[] GetTokensFromNode(IJsonNavigatorNode node, IJsonNavigator navigator, bool performCorrectnessCheck)
        {
            switch (navigator.GetNodeType(node))
            {
            case JsonNodeType.Null:
                return(new JsonToken[] { JsonToken.Null() });

            case JsonNodeType.False:
                return(new JsonToken[] { JsonToken.Boolean(false) });

            case JsonNodeType.True:
                return(new JsonToken[] { JsonToken.Boolean(true) });

            case JsonNodeType.Number:
                return(new JsonToken[] { JsonToken.Number(navigator.GetNumberValue(node)) });

            case JsonNodeType.String:
                return(new JsonToken[] { JsonToken.String(navigator.GetStringValue(node)) });

            case JsonNodeType.Array:
                return(JsonNavigatorTests.GetTokensFromArrayNode(node, navigator, performCorrectnessCheck));

            case JsonNodeType.Object:
                return(JsonNavigatorTests.GetTokensFromObjectNode(node, navigator, performCorrectnessCheck));

            case JsonNodeType.FieldName:
                return(new JsonToken[] { JsonToken.FieldName(navigator.GetStringValue(node)) });

            case JsonNodeType.Int8:
                return(new JsonToken[] { JsonToken.Int8(navigator.GetInt8Value(node)) });

            case JsonNodeType.Int16:
                return(new JsonToken[] { JsonToken.Int16(navigator.GetInt16Value(node)) });

            case JsonNodeType.Int32:
                return(new JsonToken[] { JsonToken.Int32(navigator.GetInt32Value(node)) });

            case JsonNodeType.Int64:
                return(new JsonToken[] { JsonToken.Int64(navigator.GetInt64Value(node)) });

            case JsonNodeType.UInt32:
                return(new JsonToken[] { JsonToken.UInt32(navigator.GetUInt32Value(node)) });

            case JsonNodeType.Float32:
                return(new JsonToken[] { JsonToken.Float32(navigator.GetFloat32Value(node)) });

            case JsonNodeType.Float64:
                return(new JsonToken[] { JsonToken.Float64(navigator.GetFloat64Value(node)) });

            case JsonNodeType.Guid:
                return(new JsonToken[] { JsonToken.Guid(navigator.GetGuidValue(node)) });

            case JsonNodeType.Binary:
                return(new JsonToken[] { JsonToken.Binary(navigator.GetBinaryValue(node)) });

            default:
                throw new InvalidOperationException();
            }
        }
示例#3
0
        private void VerifyNavigator(string input, Exception expectedException, bool performExtraChecks = true)
        {
            CultureInfo defaultCultureInfo = System.Threading.Thread.CurrentThread.CurrentCulture;

            CultureInfo[] cultureInfoList = new CultureInfo[]
            {
                defaultCultureInfo,
                System.Globalization.CultureInfo.GetCultureInfo("fr-FR")
            };

            try
            {
                foreach (CultureInfo cultureInfo in cultureInfoList)
                {
                    System.Threading.Thread.CurrentThread.CurrentCulture = cultureInfo;

                    IJsonReader     jsonReader       = JsonReader.Create(Encoding.UTF8.GetBytes(input));
                    JsonTokenInfo[] tokensFromReader = JsonNavigatorTests.GetTokensWithReader(jsonReader);

                    // Test text
                    IJsonNavigator     textNavigator           = JsonNavigator.Create(Encoding.UTF8.GetBytes(input));
                    IJsonNavigatorNode textRootNode            = textNavigator.GetRootNode();
                    JsonTokenInfo[]    tokensFromTextNavigator = JsonNavigatorTests.GetTokensFromNode(textRootNode, textNavigator, performExtraChecks);

                    Assert.IsTrue(tokensFromTextNavigator.SequenceEqual(tokensFromReader));

                    // Test binary
                    byte[]             binaryInput               = JsonTestUtils.ConvertTextToBinary(input);
                    IJsonNavigator     binaryNavigator           = JsonNavigator.Create(binaryInput);
                    IJsonNavigatorNode binaryRootNode            = binaryNavigator.GetRootNode();
                    JsonTokenInfo[]    tokensFromBinaryNavigator = JsonNavigatorTests.GetTokensFromNode(binaryRootNode, binaryNavigator, performExtraChecks);

                    Assert.IsTrue(tokensFromBinaryNavigator.SequenceEqual(tokensFromReader));

                    // Test binary + user string encoding
                    JsonStringDictionary jsonStringDictionary = new JsonStringDictionary(capacity: 4096);
                    byte[] binaryWithUserStringEncodingInput  = JsonTestUtils.ConvertTextToBinary(input, jsonStringDictionary);
                    if (jsonStringDictionary.TryGetStringAtIndex(index: 0, value: out string temp))
                    {
                        Assert.IsFalse(binaryWithUserStringEncodingInput.SequenceEqual(binaryInput), "Binary should be different with user string encoding");
                    }
                    IJsonNavigator     binaryNavigatorWithUserStringEncoding           = JsonNavigator.Create(binaryInput, jsonStringDictionary);
                    IJsonNavigatorNode binaryRootNodeWithUserStringEncoding            = binaryNavigatorWithUserStringEncoding.GetRootNode();
                    JsonTokenInfo[]    tokensFromBinaryNavigatorWithUserStringEncoding = JsonNavigatorTests.GetTokensFromNode(binaryRootNode, binaryNavigator, performExtraChecks);

                    Assert.IsTrue(tokensFromBinaryNavigatorWithUserStringEncoding.SequenceEqual(tokensFromReader));
                }
            }
            finally
            {
                System.Threading.Thread.CurrentThread.CurrentCulture = defaultCultureInfo;
            }
        }
        internal static JsonTokenInfo[] GetTokensFromArrayNode(IJsonNavigatorNode node, IJsonNavigator navigator, bool performCorrectnessCheck)
        {
            // Get tokens once through IEnumerable
            List <JsonTokenInfo>             tokensFromIEnumerable = new List <JsonTokenInfo>();
            IEnumerable <IJsonNavigatorNode> arrayItems            = navigator.GetArrayItems(node);

            tokensFromIEnumerable.Add(JsonTokenInfo.ArrayStart());
            foreach (IJsonNavigatorNode arrayItem in arrayItems)
            {
                tokensFromIEnumerable.AddRange(JsonNavigatorTests.GetTokensFromNode(arrayItem, navigator, performCorrectnessCheck));
            }

            tokensFromIEnumerable.Add(JsonTokenInfo.ArrayEnd());

            if (performCorrectnessCheck)
            {
                // Get tokens once again through indexer
                List <JsonTokenInfo> tokensFromIndexer = new List <JsonTokenInfo>();
                tokensFromIndexer.Add(JsonTokenInfo.ArrayStart());
                for (int i = 0; i < navigator.GetArrayItemCount(node); ++i)
                {
                    tokensFromIndexer.AddRange(JsonNavigatorTests.GetTokensFromNode(navigator.GetArrayItemAt(node, i), navigator, performCorrectnessCheck));
                }

                tokensFromIndexer.Add(JsonTokenInfo.ArrayEnd());

                Assert.AreEqual(arrayItems.Count(), navigator.GetArrayItemCount(node));
                Assert.IsTrue(tokensFromIEnumerable.SequenceEqual(tokensFromIndexer));

                try
                {
                    navigator.GetArrayItemAt(node, navigator.GetArrayItemCount(node) + 1);
                    Assert.Fail("Expected to get an index out of range exception from going one past the end of the array.");
                }
                catch (IndexOutOfRangeException)
                {
                    Assert.AreEqual(navigator.SerializationFormat, JsonSerializationFormat.Binary);
                }
                catch (ArgumentOutOfRangeException)
                {
                    Assert.AreEqual(navigator.SerializationFormat, JsonSerializationFormat.Text);
                }
            }

            return(tokensFromIEnumerable.ToArray());
        }
示例#5
0
        public static TimeSpan MeasureNavigationPerformance(IJsonNavigator jsonNavigator, int numberOfIterations = 1)
        {
            JsonToken[] tokensFromNode;
            Stopwatch   stopwatch = new Stopwatch();

            for (int i = 0; i < numberOfIterations; i++)
            {
                stopwatch.Start();
                tokensFromNode = JsonNavigatorTests.GetTokensFromNode(jsonNavigator.GetRootNode(), jsonNavigator, false);
                stopwatch.Stop();

                if (tokensFromNode.Length == 0)
                {
                    throw new InvalidOperationException("got back zero tokens");
                }
            }

            return(stopwatch.Elapsed);
        }
        internal static JsonTokenInfo[] GetTokensFromObjectNode(IJsonNavigatorNode node, IJsonNavigator navigator, bool performCorrectnessCheck)
        {
            // Get the tokens through .GetObjectProperties
            List <JsonTokenInfo>         tokensFromGetProperties = new List <JsonTokenInfo>();
            IEnumerable <ObjectProperty> properties = navigator.GetObjectProperties(node);

            tokensFromGetProperties.Add(JsonTokenInfo.ObjectStart());
            foreach (ObjectProperty property in properties)
            {
                string fieldname = navigator.GetStringValue(property.NameNode);
                tokensFromGetProperties.Add(JsonTokenInfo.FieldName(fieldname));
                tokensFromGetProperties.AddRange(JsonNavigatorTests.GetTokensFromNode(property.ValueNode, navigator, performCorrectnessCheck));
            }
            tokensFromGetProperties.Add(JsonTokenInfo.ObjectEnd());

            if (performCorrectnessCheck)
            {
                // Get the tokens again through .TryGetObjectProperty
                List <JsonTokenInfo> tokensFromTryGetProperty = new List <JsonTokenInfo>();

                tokensFromTryGetProperty.Add(JsonTokenInfo.ObjectStart());
                foreach (ObjectProperty objectProperty in properties)
                {
                    ObjectProperty propertyFromTryGetProperty;
                    string         fieldname = navigator.GetStringValue(objectProperty.NameNode);
                    if (navigator.TryGetObjectProperty(node, fieldname, out propertyFromTryGetProperty))
                    {
                        tokensFromTryGetProperty.Add(JsonTokenInfo.FieldName(fieldname));
                        tokensFromTryGetProperty.AddRange(JsonNavigatorTests.GetTokensFromNode(propertyFromTryGetProperty.ValueNode, navigator, performCorrectnessCheck));
                    }
                    else
                    {
                        Assert.Fail($"Failed to get object property with name: {fieldname}");
                    }
                }
                tokensFromTryGetProperty.Add(JsonTokenInfo.ObjectEnd());
                Assert.AreEqual(properties.Count(), navigator.GetObjectPropertyCount(node));
                Assert.IsTrue(tokensFromGetProperties.SequenceEqual(tokensFromTryGetProperty));
            }

            return(tokensFromGetProperties.ToArray());
        }
        private void VerifyNavigator(string input, Exception expectedException, bool performExtraChecks = true)
        {
            IJsonReader jsonReader = JsonReader.Create(Encoding.UTF8.GetBytes(input));

            JsonTokenInfo[] tokensFromReader = JsonNavigatorTests.GetTokensWithReader(jsonReader);

            // Test text
            IJsonNavigator     textNavigator = JsonNavigator.Create(Encoding.UTF8.GetBytes(input));
            IJsonNavigatorNode textRootNode  = textNavigator.GetRootNode();

            JsonTokenInfo[] tokensFromTextNavigator = JsonNavigatorTests.GetTokensFromNode(textRootNode, textNavigator, performExtraChecks);

            Assert.IsTrue(tokensFromTextNavigator.SequenceEqual(tokensFromReader));

            // Test binary
            byte[]             binaryInput     = JsonTestUtils.ConvertTextToBinary(input);
            IJsonNavigator     binaryNavigator = JsonNavigator.Create(binaryInput);
            IJsonNavigatorNode binaryRootNode  = binaryNavigator.GetRootNode();

            JsonTokenInfo[] tokensFromBinaryNavigator = JsonNavigatorTests.GetTokensFromNode(binaryRootNode, binaryNavigator, performExtraChecks);

            Assert.IsTrue(tokensFromBinaryNavigator.SequenceEqual(tokensFromReader));

            // Test binary + user string encoding
            JsonStringDictionary jsonStringDictionary = new JsonStringDictionary(capacity: 4096);

            byte[] binaryWithUserStringEncodingInput = JsonTestUtils.ConvertTextToBinary(input, jsonStringDictionary);
            if (jsonStringDictionary.TryGetStringAtIndex(index: 0, value: out string temp))
            {
                Assert.IsFalse(binaryWithUserStringEncodingInput.SequenceEqual(binaryInput), "Binary should be different with user string encoding");
            }
            IJsonNavigator     binaryNavigatorWithUserStringEncoding = JsonNavigator.Create(binaryInput, jsonStringDictionary);
            IJsonNavigatorNode binaryRootNodeWithUserStringEncoding  = binaryNavigatorWithUserStringEncoding.GetRootNode();

            JsonTokenInfo[] tokensFromBinaryNavigatorWithUserStringEncoding = JsonNavigatorTests.GetTokensFromNode(binaryRootNode, binaryNavigator, performExtraChecks);

            Assert.IsTrue(tokensFromBinaryNavigatorWithUserStringEncoding.SequenceEqual(tokensFromReader));
        }
        private void VerifyNavigator(string input, Exception expectedException, bool performExtraChecks = true)
        {
            IJsonReader jsonReader = JsonReader.Create(Encoding.UTF8.GetBytes(input));

            JsonTokenInfo[] tokensFromReader = JsonNavigatorTests.GetTokensWithReader(jsonReader);

            // Test text
            IJsonNavigator     textNavigator = JsonNavigator.Create(Encoding.UTF8.GetBytes(input));
            IJsonNavigatorNode textRootNode  = textNavigator.GetRootNode();

            JsonTokenInfo[] tokensFromTextNavigator = JsonNavigatorTests.GetTokensFromNode(textRootNode, textNavigator, performExtraChecks);

            Assert.IsTrue(tokensFromTextNavigator.SequenceEqual(tokensFromReader));

            // Test binary
            byte[]             binaryInput     = JsonTestUtils.ConvertTextToBinary(input);
            IJsonNavigator     binaryNavigator = JsonNavigator.Create(binaryInput);
            IJsonNavigatorNode binaryRootNode  = binaryNavigator.GetRootNode();

            JsonTokenInfo[] tokensFromBinaryNavigator = JsonNavigatorTests.GetTokensFromNode(binaryRootNode, binaryNavigator, performExtraChecks);

            Assert.IsTrue(tokensFromBinaryNavigator.SequenceEqual(tokensFromReader));
        }