Exemplo n.º 1
0
        public void JsonSerializeBytesRelaxed()
        {
            var before =
                new JsonTestPerson()
            {
                Name = "Jeff",
                Age  = 56
            };

            var jsonBytes = NeonHelper.JsonSerializeToBytes(before);

            Assert.StartsWith("{", Encoding.UTF8.GetString(jsonBytes));

            var after = NeonHelper.JsonDeserialize <JsonTestPerson>(jsonBytes);

            Assert.Equal("Jeff", after.Name);
            Assert.Equal(56, after.Age);

            // Verify that we don't see exceptions for a source property
            // that's not defined in the type.

            const string unmatchedJson =
                @"
{
    ""Name"": ""jeff"",
    ""Age"": 66,
    ""Unmatched"": ""Hello""
}
";
            var unmatchedJsonBytes = Encoding.UTF8.GetBytes(unmatchedJson);

            NeonHelper.JsonDeserialize <JsonTestPerson>(unmatchedJsonBytes, strict: false);
        }
Exemplo n.º 2
0
        public void JsonClone()
        {
            var value =
                new JsonTestPerson()
            {
                Name = "Jeff",
                Age  = 56
            };

            var clone = NeonHelper.JsonClone <JsonTestPerson>(value);

            Assert.NotSame(value, clone);
            Assert.Equal(value.Name, clone.Name);
            Assert.Equal(value.Age, clone.Age);
        }
Exemplo n.º 3
0
        public void JsonNotYaml()
        {
            // Verify that we can identify and parse JSON (over YAML).

            var before =
                new JsonTestPerson()
            {
                Name = "Jeff",
                Age  = 56
            };

            var json = NeonHelper.JsonSerialize(before);

            Assert.StartsWith("{", json);

            var after = NeonHelper.JsonOrYamlDeserialize <JsonTestPerson>(json);

            Assert.Equal("Jeff", after.Name);
            Assert.Equal(56, after.Age);
        }
Exemplo n.º 4
0
        public void JsonSerializeStrict()
        {
            var before =
                new JsonTestPerson()
            {
                Name = "Jeff",
                Age  = 56
            };

            var json = NeonHelper.JsonSerialize(before);

            Assert.StartsWith("{", json);

            var after = NeonHelper.JsonDeserialize <JsonTestPerson>(json);

            Assert.Equal("Jeff", after.Name);
            Assert.Equal(56, after.Age);

            const string unmatchedJson =
                @"
{
    ""Name"": ""jeff"",
    ""Age"": 66,
    ""Unmatched"": ""Hello""
}
";

            // Verify that we don't see exceptions for a source property
            // that's not defined in the type by default (when [strict=false])

            NeonHelper.JsonDeserialize <JsonTestPerson>(unmatchedJson);

            // Verify that we see exceptions for a source property
            // that's not defined in the type when [strict=true]

            Assert.Throws <JsonSerializationException>(() => NeonHelper.JsonDeserialize <JsonTestPerson>(unmatchedJson, strict: true));
        }