public void Use_DataMember_names_when_annotated() { var instance = new FooAnnotated { MyInt = 75, MyString = "secret" }; Helpers.SetJsonSerializerResolver(); var bytes = JsonSerializer.Serialize(instance); var json = Encoding.UTF8.GetString(bytes); json.ShouldContain("MyCustomInt"); json.ShouldContain("MyCustomString"); }
public void Use_DataMember_names_when_annotated() { var instance = new FooAnnotated { MyInt = 75, MyString = "secret" }; var resolver = Helpers.GetEncryptedResolver(StandardResolver.Default); var bytes = JsonSerializer.Serialize(instance, resolver); var json = Encoding.UTF8.GetString(bytes); json.ShouldContain("MyCustomInt"); json.ShouldContain("MyCustomString"); }
public void Use_annotated_constructor_if_exists() { var instance = new FooAnnotated("something secret", 75); var resolver = Helpers.GetEncryptedResolver(StandardResolver.Default); var bytes = JsonSerializer.Serialize(instance, resolver); var json = Encoding.UTF8.GetString(bytes); json.ShouldNotContain(Helpers.SerializedValueOf(instance.MyInt, StandardResolver.AllowPrivate, nameof(instance.MyInt))); json.ShouldNotContain(Helpers.SerializedValueOf(instance.MyString, StandardResolver.AllowPrivate, nameof(instance.MyString))); var deserialized = JsonSerializer.Deserialize <FooAnnotated>(json, resolver); // 2 * 75 deserialized.MyInt.ShouldBe(150); // the value(s) that the constructor doesn't set (by name according to constructor params) will still be set after the constructor is used deserialized.MyString.ShouldBe("something secret"); }