コード例 #1
0
        public void JsonExpressionWriter_NotSupported()
        {
            AssertNotSupported(w => w.WriteValue(Array.Empty <byte>()));
            AssertNotSupported(w => w.WriteComment("bar"));
            AssertNotSupported(w => w.WriteStartConstructor("bar"));
            AssertNotSupported(w => w.WriteEndConstructor());
            AssertNotSupported(w => w.WriteUndefined());

            Assert.ThrowsException <NotSupportedException>(() =>
            {
                var w = new JsonExpressionWriter()
                {
                    DateFormatHandling = DateFormatHandling.MicrosoftDateFormat
                };
                w.WriteValue(DateTime.Now);
            });

            Assert.ThrowsException <NotSupportedException>(() =>
            {
                var w = new JsonExpressionWriter()
                {
                    DateFormatHandling = DateFormatHandling.MicrosoftDateFormat
                };
                w.WriteValue(DateTimeOffset.Now);
            });
        }
コード例 #2
0
        public void JsonExpressionWriter_DateTimeOffset()
        {
            Roundtrip(
                new DateTimeOffset(1983, 2, 11, 3, 14, 15, TimeSpan.FromHours(0)),
                new DateTimeOffset(1983, 2, 11, 3, 14, 15, TimeSpan.FromHours(1)),
                new DateTimeOffset(1983, 2, 11, 3, 14, 15, TimeSpan.FromHours(-1)),
                new DateTimeOffset(1983, 2, 11, 3, 14, 15, 9, new TimeSpan(2, 30, 0)),
                new DateTimeOffset(1983, 2, 11, 3, 14, 15, 98, new TimeSpan(-8, 30, 0)),
                new DateTimeOffset(1983, 2, 11, 3, 14, 15, 987, TimeSpan.FromHours(5)),
                new DateTimeOffset(1983, 2, 11, 3, 14, 15, TimeSpan.FromHours(1)).AddTicks(1),
                new DateTimeOffset(1983, 2, 11, 3, 14, 15, TimeSpan.FromHours(-1)).AddTicks(12),
                new DateTimeOffset(1983, 2, 11, 3, 14, 15, TimeSpan.FromHours(2)).AddTicks(123),
                new DateTimeOffset(1983, 2, 11, 3, 14, 15, TimeSpan.FromHours(-2)).AddTicks(1234),
                new DateTimeOffset(1983, 2, 11, 3, 14, 15, TimeSpan.FromHours(9)).AddTicks(12345),
                new DateTimeOffset(1983, 2, 11, 3, 14, 15, TimeSpan.FromHours(-10)).AddTicks(123456),
                new DateTimeOffset(1983, 2, 11, 3, 14, 15, TimeSpan.FromHours(11)).AddTicks(1234567)
                );

            var w = new JsonExpressionWriter()
            {
                DateFormatString = "dd/MM/yyyy"
            };

            w.WriteValue(new DateTimeOffset(1983, 2, 11, 3, 14, 15, TimeSpan.Zero));
            w.Flush();
            Assert.AreEqual("\"11/02/1983\"", w.Expression.ToString());
        }
コード例 #3
0
        public void JsonExpressionWriter_DateTime()
        {
            Roundtrip(
                new DateTime(1983, 2, 11, 3, 14, 15, DateTimeKind.Unspecified),
                new DateTime(1983, 2, 11, 3, 14, 15, DateTimeKind.Local),
                new DateTime(1983, 2, 11, 3, 14, 15, DateTimeKind.Utc),
                new DateTime(1983, 2, 11, 3, 14, 15, 9, DateTimeKind.Utc),
                new DateTime(1983, 2, 11, 3, 14, 15, 98, DateTimeKind.Utc),
                new DateTime(1983, 2, 11, 3, 14, 15, 987, DateTimeKind.Utc),
                new DateTime(1983, 2, 11, 3, 14, 15, DateTimeKind.Utc).AddTicks(1),
                new DateTime(1983, 2, 11, 3, 14, 15, DateTimeKind.Utc).AddTicks(12),
                new DateTime(1983, 2, 11, 3, 14, 15, DateTimeKind.Utc).AddTicks(123),
                new DateTime(1983, 2, 11, 3, 14, 15, DateTimeKind.Utc).AddTicks(1234),
                new DateTime(1983, 2, 11, 3, 14, 15, DateTimeKind.Utc).AddTicks(12345),
                new DateTime(1983, 2, 11, 3, 14, 15, DateTimeKind.Utc).AddTicks(123456),
                new DateTime(1983, 2, 11, 3, 14, 15, DateTimeKind.Utc).AddTicks(1234567)
                );

            var w = new JsonExpressionWriter()
            {
                DateFormatString = "dd/MM/yyyy"
            };

            w.WriteValue(new DateTime(1983, 2, 11, 3, 14, 15));
            w.Flush();
            Assert.AreEqual("\"11/02/1983\"", w.Expression.ToString());
        }
コード例 #4
0
        public void JsonExpressionWriter_InvalidInput()
        {
            var writer = new JsonExpressionWriter();

            writer.WriteValue(1);
            writer.WriteValue(2);

            Assert.ThrowsException <InvalidOperationException>(() => writer.Expression);
        }
コード例 #5
0
        private static Expression Serialize(FloatFormatHandling ffh, object obj)
        {
            var ser = new JsonSerializer();

            using var writer = new JsonExpressionWriter()
                  {
                      FloatFormatHandling = ffh
                  };

            ser.Serialize(writer, obj);
            writer.Flush();

            return(writer.Expression);
        }
コード例 #6
0
        public void JsonExpressionWriter_Primitives_NullableDouble()
        {
            Roundtrip(
                new { a = (double?)null },
                new { a = (double?)0.0 },
                new { a = (double?)1.0 }
                );

            var w = new JsonExpressionWriter();

            w.WriteValue((double?)null);
            w.Flush();
            Assert.AreEqual("null", w.Expression.ToString());
        }
コード例 #7
0
        public void JsonExpressionWriter_InvalidOperation_EndObject()
        {
            Assert.ThrowsException <InvalidOperationException>(() =>
            {
                var w = new JsonExpressionWriter();
                w.WriteEndObject();
            });

            Assert.ThrowsException <InvalidOperationException>(() =>
            {
                var w = new JsonExpressionWriter();
                w.WriteValue(1);
                w.WriteEndObject();
            });
        }
コード例 #8
0
 public void JsonExpressionWriter_InvalidOperation_EndArray_NoValidElement()
 {
     foreach (var write in new Action <JsonExpressionWriter>[]
     {
         w => w.WritePropertyName("foo"),
         w => w.WriteStartObject(),
     })
     {
         Assert.ThrowsException <InvalidOperationException>(() =>
         {
             var w = new JsonExpressionWriter();
             w.WriteStartArray();
             write(w);
             w.WriteEndArray();
         });
     }
 }
コード例 #9
0
        public void JsonExpressionWriter_String()
        {
            Roundtrip(new object[] {
                null,
                " ",
                "bar",
                "I am Bart",
                "Hello \"Bart\"!",
                "Some\tweird\r\ncharacters..."
            });

            var w = new JsonExpressionWriter();

            w.WriteValue((string)null);
            w.Flush();
            Assert.AreEqual("null", w.Expression.ToString());
        }
コード例 #10
0
        public void JsonExpressionWriter_Basics()
        {
            var writer = new JsonExpressionWriter();

            writer.WriteValue(42);

            writer.Flush();

            var expr     = writer.Expression;
            var constant = expr as ConstantExpression;

            Assert.IsNotNull(constant);
            Assert.AreEqual(ExpressionType.Number, constant.NodeType);
            Assert.AreEqual("42", (string)constant.Value);

            ((IDisposable)writer).Dispose();

            Assert.ThrowsException <ObjectDisposedException>(() => writer.WriteValue(43));
            Assert.ThrowsException <ObjectDisposedException>(() => writer.Expression);
        }