Esempio n. 1
0
        public void TypeIs_WhenAppliedToJsonObjectTypeNull_Always_ReturnsTrue()
        {
            // Arrange
            IJsonObject json = JsonObject.OfNull();

            // Act
            var r = json.TypeIs(JsonObjectType.Null);

            // Assert
            Assert.That(r, Is.True);
        }
        public void TypeIs_WhenAppliedToJsonObjectTypeNull_ReturnsFalse()
        {
            // Arrange
            IJsonObject json = JsonObject.Of(true);

            // Act
            var r = json.TypeIs(JsonObjectType.Null);

            // Assert
            Assert.That(r, Is.False);
        }
Esempio n. 3
0
        public void TypeIs_WhenAppliedToJsonObjectTypeObject_Always_ReturnsFalse()
        {
            // Arrange
            IJsonObject json = JsonObject.OfNull();

            // Act
            var r = json.TypeIs(JsonObjectType.Object);

            // Assert
            Assert.That(r, Is.False);
        }
        public void TypeIs_WhenAppliedToJsonObjectTypeObject_ReturnsFalse()
        {
            // Arrange
            IJsonObject json = JsonObject.Of(new string[0]);

            // Act
            var r = json.TypeIs(JsonObjectType.Object);

            // Assert
            Assert.That(r, Is.False);
        }
        public void TypeIs_WhenAppliedToJsonObjectTypeObject_ReturnsTrue()
        {
            // Arrange
            IJsonObject json0 = JsonObject.FromString("{}");
            IJsonObject json1 = JsonObject.FromString("{a:'abc',b:123,c:true,d:null,e:[],f:{}}");

            // Act
            var r0 = json0.TypeIs(JsonObjectType.Object);
            var r1 = json1.TypeIs(JsonObjectType.Object);

            // Assert
            Assert.That(r0, Is.True);
            Assert.That(r1, Is.True);
        }
Esempio n. 6
0
        public void FromString_WhenApplyToValidJsonLiteral_ReturnsSuccessfuly()
        {
            // Arrange
            // Act
            IJsonObject r0 = JsonObject.FromString("{}");
            IJsonObject r1 = JsonObject.FromString("[]");
            IJsonObject r2 = JsonObject.FromString("true");
            IJsonObject r3 = JsonObject.FromString("null");
            IJsonObject r4 = JsonObject.FromString("''");
            IJsonObject r5 = JsonObject.FromString("\"\"");
            IJsonObject r6 = JsonObject.FromString("0.0");

            // Assert
            Assert.That(r0.TypeIs(JsonObjectType.Object), Is.True);
            Assert.That(r1.TypeIs(JsonObjectType.Array), Is.True);
            Assert.That(r2.TypeIs(JsonObjectType.Boolean), Is.True);
            Assert.That(r3.TypeIs(JsonObjectType.Null), Is.True);
            Assert.That(r4.TypeIs(JsonObjectType.String), Is.True);
            Assert.That(r5.TypeIs(JsonObjectType.String), Is.True);
            Assert.That(r6.TypeIs(JsonObjectType.Number), Is.True);
        }
Esempio n. 7
0
        private static void FormatHelper(IJsonObject json,
                                         IJsonFormatOptions opts,
                                         StringBuilder buff,
                                         int depth)
        {
            if (json.Type != JsonObjectType.Array && !json.TypeIs(JsonObjectType.Object))
            {
                buff.Append(json.ToString());
                return;
            }

            if (json.Type == JsonObjectType.Array)
            {
                buff.Append('[');
                bool empty = true;
                foreach (IJsonObject item in json.AsArray())
                {
                    if (empty)
                    {
                        empty = false;
                    }
                    else
                    {
                        buff.Append(',');
                    }
                    FormatHelperNewLine(opts, buff);
                    FormatHelperIndent(opts, buff, depth + 1);
                    FormatHelper(item, opts, buff, depth + 1);
                }
                if (!empty)
                {
                    FormatHelperNewLine(opts, buff);
                    FormatHelperIndent(opts, buff, depth);
                }
                buff.Append(']');
            }
            else if (json.Type == JsonObjectType.Object)
            {
                buff.Append('{');
                bool empty = true;
                foreach (IJsonProperty p in json.Properties)
                {
                    if (empty)
                    {
                        empty = false;
                    }
                    else
                    {
                        buff.Append(',');
                    }
                    FormatHelperNewLine(opts, buff);
                    FormatHelperIndent(opts, buff, depth + 1);
                    buff.Append(Quotes(p.Name)).Append(' ').Append(':').Append(' ');
                    FormatHelper(p.Value, opts, buff, depth + 1);
                }
                if (!empty)
                {
                    FormatHelperNewLine(opts, buff);
                    FormatHelperIndent(opts, buff, depth);
                }
                buff.Append('}');
            }
        }