Ejemplo n.º 1
0
        public void NumberRegressionTest()
        {
            // regression test - the value 0.00085647800000000004 was being incorrectly rejected
            string numberValueString = "0.00085647800000000004";

            JsonNavigatorTests.VerifyNavigator(numberValueString);
        }
Ejemplo n.º 2
0
        private 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.Number64:
                return(new JsonToken[] { JsonToken.Number(navigator.GetNumber64Value(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();
            }
        }
Ejemplo n.º 3
0
 public void GuidTest()
 {
     Guid[] values = new Guid[] { Guid.Empty, Guid.NewGuid() };
     foreach (Guid value in values)
     {
         string input = $"G{value.ToString()}";
         JsonNavigatorTests.VerifyNavigator(input);
     }
 }
Ejemplo n.º 4
0
 public void Float64Test()
 {
     double[] values = new double[] { double.MinValue, double.MinValue + 1, 0, 1, double.MaxValue, double.MaxValue - 1 };
     foreach (double value in values)
     {
         string input = $"D{value.ToString("G17", CultureInfo.InvariantCulture)}";
         JsonNavigatorTests.VerifyNavigator(input);
     }
 }
Ejemplo n.º 5
0
 public void Float32Test()
 {
     float[] values = new float[] { float.MinValue, float.MinValue + 1, 0, 1, float.MaxValue, float.MaxValue - 1 };
     foreach (float value in values)
     {
         string input = $"S{value.ToString("G9", CultureInfo.InvariantCulture)}";
         JsonNavigatorTests.VerifyNavigator(input);
     }
 }
Ejemplo n.º 6
0
 public void UInt32Test()
 {
     uint[] values = new uint[] { uint.MinValue, uint.MinValue + 1, 0, 1, uint.MaxValue, uint.MaxValue - 1 };
     foreach (uint value in values)
     {
         string input = $"UL{value}";
         JsonNavigatorTests.VerifyNavigator(input);
     }
 }
Ejemplo n.º 7
0
 public void Int64Test()
 {
     long[] values = new long[] { long.MinValue, long.MinValue + 1, -1, 0, 1, long.MaxValue, long.MaxValue - 1 };
     foreach (long value in values)
     {
         string input = $"LL{value}";
         JsonNavigatorTests.VerifyNavigator(input);
     }
 }
Ejemplo n.º 8
0
 public void Int32Test()
 {
     int[] values = new int[] { int.MinValue, int.MinValue + 1, -1, 0, 1, int.MaxValue, int.MaxValue - 1 };
     foreach (int value in values)
     {
         string input = $"L{value}";
         JsonNavigatorTests.VerifyNavigator(input);
     }
 }
Ejemplo n.º 9
0
 public void Int16Test()
 {
     short[] values = new short[] { short.MinValue, short.MinValue + 1, -1, 0, 1, short.MaxValue, short.MaxValue - 1 };
     foreach (short value in values)
     {
         string input = $"H{value}";
         JsonNavigatorTests.VerifyNavigator(input);
     }
 }
Ejemplo n.º 10
0
 public void Int8Test()
 {
     sbyte[] values = new sbyte[] { sbyte.MinValue, sbyte.MinValue + 1, -1, 0, 1, sbyte.MaxValue, sbyte.MaxValue - 1 };
     foreach (sbyte value in values)
     {
         string input = $"I{value}";
         JsonNavigatorTests.VerifyNavigator(input);
     }
 }
Ejemplo n.º 11
0
        private static void VerifyNavigator(
            string input,
            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));
                    JsonToken[] tokensFromReader = JsonNavigatorTests.GetTokensWithReader(jsonReader);

                    // Test text
                    IJsonNavigator     textNavigator           = JsonNavigator.Create(Encoding.UTF8.GetBytes(input));
                    IJsonNavigatorNode textRootNode            = textNavigator.GetRootNode();
                    JsonToken[]        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();
                    JsonToken[]        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 _))
                    {
                        Assert.IsFalse(binaryWithUserStringEncodingInput.SequenceEqual(binaryInput), "Binary should be different with user string encoding");
                    }

                    IJsonNavigator     binaryNavigatorWithUserStringEncoding           = JsonNavigator.Create(binaryInput, jsonStringDictionary);
                    IJsonNavigatorNode binaryRootNodeWithUserStringEncoding            = binaryNavigatorWithUserStringEncoding.GetRootNode();
                    JsonToken[]        tokensFromBinaryNavigatorWithUserStringEncoding = JsonNavigatorTests.GetTokensFromNode(binaryRootNodeWithUserStringEncoding, binaryNavigatorWithUserStringEncoding, performExtraChecks);

                    Assert.IsTrue(tokensFromBinaryNavigatorWithUserStringEncoding.SequenceEqual(tokensFromReader));
                }
            }
            finally
            {
                System.Threading.Thread.CurrentThread.CurrentCulture = defaultCultureInfo;
            }
        }
Ejemplo n.º 12
0
        public void StrangeNumberArrayTest()
        {
            string input = @"[
                1111111110111111111011111111101111111110,
                1111111110111111111011111111101111111110111111111011111111101111111110,
               11111111101111111110111111111011111111101111111110111111111011111111101111111110111111111011111111101111111110111111111011111111101111111110,
                1111111110111111111011111111101111111110111111111011111111101111111110111111111011111111101111111110111111111011111111101111111110111111111011111111101111111110111111111011111111101111111110111111111011111111101111111110111111111011111111101111111110111111111011111111101111111110
                    ]";

            JsonNavigatorTests.VerifyNavigator(input);
        }
Ejemplo n.º 13
0
        private static void VerifyNavigatorWithCurratedDoc(string path, bool performExtraChecks = true)
        {
            path = string.Format("TestJsons/{0}", path);
            string json = TextFileConcatenation.ReadMultipartFile(path);

#if true
            json = JsonTestUtils.RandomSampleJson(json, maxNumberOfItems: 1);
#endif

            JsonNavigatorTests.VerifyNavigator(json, performExtraChecks);
        }
Ejemplo n.º 14
0
        public void BinaryTest()
        {
            {
                // Empty Binary
                string input = $"B";
                JsonNavigatorTests.VerifyNavigator(input);
            }

            {
                // Binary 1 Byte Length
                IReadOnlyList <byte> binary = Enumerable.Range(0, 25).Select(x => (byte)x).ToList();
                string input = $"B{Convert.ToBase64String(binary.ToArray())}";
                JsonNavigatorTests.VerifyNavigator(input);
            }
        }
Ejemplo n.º 15
0
        private static JsonToken[] GetTokensFromArrayNode(IJsonNavigatorNode node, IJsonNavigator navigator, bool performCorrectnessCheck)
        {
            // Get tokens once through IEnumerable
            List <JsonToken> tokensFromIEnumerable      = new List <JsonToken>();
            IEnumerable <IJsonNavigatorNode> arrayItems = navigator.GetArrayItems(node);

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

            tokensFromIEnumerable.Add(JsonToken.ArrayEnd());

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

                tokensFromIndexer.Add(JsonToken.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());
        }
Ejemplo n.º 16
0
        public void StrangeNumberArrayTest()
        {
            string input = @"[
                1111111110111111111011111111101111111110,
                1111111110111111111011111111101111111110111111111011111111101111111110,
                11111111101111111110111111111011111111101111111110111111111011111111101111111110111111111011111111101111111110111111111011111111101111111110,
                1111111110111111111011111111101111111110111111111011111111101111111110111111111011111111101111111110111111111011111111101111111110111111111011111111101111111110111111111011111111101111111110111111111011111111101111111110111111111011111111101111111110111111111011111111101111111110
            ]";

            try
            {
                JsonNavigatorTests.VerifyNavigator(input);
            }
            catch (MaterilizationFailedToMatchException)
            {
                // Newtonsoft does not use IEEE double precision for these long integers.
            }
        }
Ejemplo n.º 17
0
        internal static JsonToken[] GetTokensFromObjectNode(IJsonNavigatorNode node, IJsonNavigator navigator, bool performCorrectnessCheck)
        {
            // Get the tokens through .GetObjectProperties
            List <JsonToken>             tokensFromGetProperties = new List <JsonToken>();
            IEnumerable <ObjectProperty> properties = navigator.GetObjectProperties(node);

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

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

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

            return(tokensFromGetProperties.ToArray());
        }
Ejemplo n.º 18
0
        public static TimeSpan MeasureNavigationPerformance(IJsonNavigator jsonNavigator, int numberOfIterations = 1)
        {
            GC.Collect();
            GC.WaitForPendingFinalizers();

            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);
        }
Ejemplo n.º 19
0
        public void AllPrimitivesObjectTest()
        {
            string input = @"{
                        ""id"": ""7029d079-4016-4436-b7da-36c0bae54ff6"",
                        ""double"": 0.18963001816981939,
                        ""int"": -1330192615,
                        ""string"": ""XCPCFXPHHF"",
                        ""boolean"": true,
                        ""null"": null,
                        ""datetime"": ""2526-07-11T18:18:16.4520716"",
                        ""spatialPoint"": {
                            ""type"": ""Point"",
                            ""coordinates"": [
                                118.9897,
                                -46.6781
                            ]
                        },
                        ""text"": ""tiger diamond newbrunswick snowleopard chocolate dog snowleopard turtle cat sapphire peach sapphire vancouver white chocolate horse diamond lion superlongcolourname ruby""
                    }";

            JsonNavigatorTests.VerifyNavigator(input);
        }
Ejemplo n.º 20
0
 public void Store01Test()
 {
     JsonNavigatorTests.VerifyNavigatorWithCurratedDoc("store01C.json");
 }
Ejemplo n.º 21
0
        public void NumberWithScientificNotationTest()
        {
            string input = "6.02252e23";

            JsonNavigatorTests.VerifyNavigator(input);
        }
Ejemplo n.º 22
0
        public void NegativeNumberTest()
        {
            string input = "-1337.0";

            JsonNavigatorTests.VerifyNavigator(input);
        }
Ejemplo n.º 23
0
        public void DoubleTest()
        {
            string input = "1337.0";

            JsonNavigatorTests.VerifyNavigator(input);
        }
Ejemplo n.º 24
0
        public void NullTest()
        {
            string input = "null";

            JsonNavigatorTests.VerifyNavigator(input);
        }
Ejemplo n.º 25
0
        public void IntegerTest()
        {
            string input = "1337";

            JsonNavigatorTests.VerifyNavigator(input);
        }
Ejemplo n.º 26
0
 public void StatesLegislatorsTest()
 {
     JsonNavigatorTests.VerifyNavigatorWithCurratedDoc("states_legislators");
 }
Ejemplo n.º 27
0
 public void TicinoErrorBucketsTest()
 {
     JsonNavigatorTests.VerifyNavigatorWithCurratedDoc("TicinoErrorBuckets");
 }
Ejemplo n.º 28
0
 public void XpertEventsTest()
 {
     JsonNavigatorTests.VerifyNavigatorWithCurratedDoc("XpertEvents");
 }
Ejemplo n.º 29
0
 public void Ups1Test()
 {
     JsonNavigatorTests.VerifyNavigatorWithCurratedDoc("ups1");
 }
Ejemplo n.º 30
0
 public void TwitterDataTest()
 {
     JsonNavigatorTests.VerifyNavigatorWithCurratedDoc("twitter_data");
 }