Esempio n. 1
0
        public void PlusEqualOperatorShouldExpandToSelfReferencingArrayConcatenation()
        {
            var source1 = @"
a = [ 1, 2 ]
a += 3
a += [4, 5]
";

            var root1 = JsonPlusParser.Parse(source1);

            Assert.True(new[] { 1, 2, 3, 4, 5 }.SequenceEqual(root1.GetInt32List("a")));

            var source = @"
a = [ 1, 2 ]
a += 3
a += ${b}
b = [ 4, 5 ]
";

            JsonPlusRoot root = null;
            var          ex   = Record.Exception(() => root = JsonPlusParser.Parse(source));

            Assert.Null(ex);
            Assert.True(new [] { 1, 2, 3, 4, 5 }.SequenceEqual(root.GetInt32List("a")));
        }
Esempio n. 2
0
        public void CanParseSimpleSubstitutionFile()
        {
            JsonPlusRoot ctx = ResUtility.GetEmbed("SimpleSub.jsonp");
            var          val = ctx.GetString("root.simple-string");

            Assert.Equal("Hello world", val);
        }
Esempio n. 3
0
        public void SelfReferenceOptionalSubstitutionInValueConcatenationShouldBeIgnored()
        {
            var source = "a = ${?a}foo";

            JsonPlusRoot root = null;
            var          ex   = Record.Exception(() => root = JsonPlusParser.Parse(source));

            Assert.Null(ex);

            Assert.Equal("foo", root.GetString("a"));
        }
Esempio n. 4
0
        public void OptionalSubstitutionCycleShouldBeIgnored()
        {
            var source = "foo : ${?foo}";

            JsonPlusRoot root = null;
            var          ex   = Record.Exception(() => root = JsonPlusParser.Parse(source));

            Assert.Null(ex);
            // should not create a field
            Assert.False(root.HasPath("foo"));
        }
Esempio n. 5
0
        public void HiddenSubstitutionShouldNeverBeEvaluated()
        {
            var source = @"
foo : ${does-not-exist}
foo : 42";

            JsonPlusRoot root = null;
            var          ex   = Record.Exception(() => root = JsonPlusParser.Parse(source));

            Assert.Null(ex);
            Assert.Equal(42, root.GetInt32("foo"));
        }
Esempio n. 6
0
        public void SubstitutionToAnotherMemberOfTheSameObjectAreResolvedNormally()
        {
            var source = @"
bar : { foo : 42,
        baz : ${bar.foo}
      }
bar : { foo : 43 }";

            JsonPlusRoot root = null;
            var          ex   = Record.Exception(() => root = JsonPlusParser.Parse(source));

            Assert.Null(ex);

            Assert.Equal(43, root.GetInt32("bar.foo"));
            Assert.Equal(43, root.GetInt32("bar.baz"));
        }
Esempio n. 7
0
        public void MergedSubstitutionShouldAlwaysResolveToOlderValue()
        {
            var source = @"
foo : { a : { c : 1 } }
foo : ${foo.a}
foo : { a : 2 }";

            JsonPlusRoot root = null;
            var          ex   = Record.Exception(() => root = JsonPlusParser.Parse(source));

            Assert.Null(ex);

            Assert.Equal(2, root.GetInt32("foo.a"));
            Assert.Equal(1, root.GetInt32("foo.c"));
            Assert.False(root.HasPath("foo.a.c"));
        }
Esempio n. 8
0
        public void MutuallyReferringObjectsAreResolvedNormally()
        {
            var source = @"
// bar.a should end up as 4
bar : { a : ${foo.d}, b : 1 }
bar.b = 3
// foo.c should end up as 3
foo : { c : ${bar.b}, d : 2 }
foo.d = 4";

            JsonPlusRoot root = null;
            var          ex   = Record.Exception(() => root = JsonPlusParser.Parse(source));

            Assert.Null(ex);

            Assert.Equal(4, root.GetInt32("bar.a"));
            Assert.Equal(3, root.GetInt32("foo.c"));
        }
        private object TransverseJPlusRoot(JsonPlusRoot root, out ErrorRecord error)
        {
            // internal exception catching
            if (root == null)
            {
                throw new ArgumentNullException(nameof(root));
            }

            error = null;

            if (root.IsEmpty)
            {
                return(null);
            }

            if (root.Value.Type != JsonPlusType.Object)
            {
                error = new ErrorRecord(new JsonPlusException(RS.JsonPlusUnsupportedRootNodeType), "UnsupportedRootNodeType", ErrorCategory.ParserError, null);
                return(null);
            }

            return(PopulateJPlusObject(root.Value.GetObject(), out error));
        }