Пример #1
0
        public void DeserializeAndConvertNullValue()
        {
            string json = @"{
  ""Id"": ""00000001-0002-0003-0405-060708090a0b"",
  ""Year"": 2010,
  ""Company"": ""Company!"",
  ""DecimalRange"": {
    ""First"": 0,
    ""Last"": 1
  },
  ""IntRange"": {
    ""First"": -2147483648,
    ""Last"": 2147483647
  },
  ""NullDecimalRange"": null
}";

            JsonSerializer.Serialize(json, new JsonSerializerOptions {
                WriteIndented = true
            });

            NullInterfaceTestClass deserialized = JsonSerializer.Deserialize <NullInterfaceTestClass>(json);

            Assert.Equal("Company!", deserialized.Company);
            Assert.Equal(new Guid(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11), deserialized.Id);
            Assert.Equal(0, deserialized.DecimalRange.First);
            Assert.Equal(1, deserialized.DecimalRange.Last);
            Assert.Equal(int.MinValue, deserialized.IntRange.First);
            Assert.Equal(int.MaxValue, deserialized.IntRange.Last);
            Assert.Equal(null, deserialized.NullDecimalRange);
            Assert.Equal(2010, deserialized.Year);
        }
Пример #2
0
        public void SerializeAndConvertNullValue()
        {
            NullInterfaceTestClass initial = new NullInterfaceTestClass
            {
                Company      = "Company!",
                DecimalRange = new Range <decimal> {
                    First = 0, Last = 1
                },
                Id       = new Guid(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11),
                IntRange = new Range <int> {
                    First = int.MinValue, Last = int.MaxValue
                },
                Year             = 2010,
                NullDecimalRange = null
            };

            string json = JsonSerializer.Serialize(initial, new JsonSerializerOptions {
                WriteIndented = true
            });

            Assert.Equal(@"{
  ""Id"": ""00000001-0002-0003-0405-060708090a0b"",
  ""Year"": 2010,
  ""Company"": ""Company!"",
  ""DecimalRange"": {
    ""First"": 0,
    ""Last"": 1
  },
  ""IntRange"": {
    ""First"": -2147483648,
    ""Last"": 2147483647
  },
  ""NullDecimalRange"": null
}", json);
        }