コード例 #1
0
        public void ReadWithSchemaFirst()
        {
            string json             = @"{
  'StringProperty': 'Text',
  'IntProperty': 42,
  'DoubleProperty': 10.2,
  'NullableIntProperty': null,
  'BoolProperty': true,
  'DateProperty': '2020-12-26',
  'StringArray':['a1','a2'],
  'IntArray':[1,2],
  '$metadata.schema.compact': [
    'StringProperty@type=string',
    'IntProperty@type=int',
    'DoubleProperty@type=double',
    'NullableIntProperty@type=int?',
    'BoolProperty @type = bool',
    'DateProperty@type=LocalDate',
    'StringArray@type=string[]',
    'IntArray@type=int[]'
  ]
}";
            var    initialContainer = CreateTestContainer();

            var        restoredContainer = json.DeserializeWithNewtonsoftJson <IPropertyContainer>(configureSerialization: options => options.ReadSchemaFirst = true);
            ObjectDiff objectDiff        = MetadataComparer.GetDiff(restoredContainer, initialContainer);

            objectDiff.Diffs.Should().BeEmpty();

            // container will lose types
            var        restoredContainer2 = json.DeserializeWithNewtonsoftJson <IPropertyContainer>(configureSerialization: options => options.ReadSchemaFirst = false);
            ObjectDiff objectDiff2        = MetadataComparer.GetDiff(restoredContainer2, initialContainer);

            objectDiff2.Diffs.Should().NotBeNullOrEmpty();
        }
コード例 #2
0
        public void SerializeDeserializePropertyContainer()
        {
            var initialContainer = CreateTestContainer();

            var propertyContainerContract = initialContainer.ToContract(new DefaultMapperSettings());

            propertyContainerContract.Should().NotBeNull();

            var contractJson = propertyContainerContract.ToJsonWithSystemTextJson();

            var json1       = initialContainer.ToJsonWithSystemTextJson();
            var jsonOptions = new JsonSerializerOptions().ConfigureJsonOptions();

            DeserializeAndCompare <IPropertyContainer>();
            DeserializeAndCompare <PropertyContainer>();
            DeserializeAndCompare <IMutablePropertyContainer>();
            DeserializeAndCompare <MutablePropertyContainer>();
            DeserializeAndCompare <PropertyContainer <TestMeta> >();

            var        json2      = initialContainer.ToJsonWithNewtonsoftJson();
            var        container2 = json2.DeserializeWithNewtonsoftJson <IPropertyContainer>();
            ObjectDiff objectDiff = MetadataComparer.GetDiff(initialContainer, container2);

            objectDiff.Diffs.Should().BeEmpty();

            void DeserializeAndCompare <TContainer>() where TContainer : IPropertyContainer
            {
                TContainer?restored = JsonSerializer.Deserialize <TContainer>(json1, jsonOptions);

                ObjectDiff objectDiff = MetadataComparer.GetDiff(restored, initialContainer);

                objectDiff.Diffs.Should().BeEmpty();
            }
        }