-Code reformat
This commit is contained in:
@@ -47,208 +47,209 @@ using System.Globalization;
|
||||
|
||||
namespace Newtonsoft.Json.Tests.Serialization
|
||||
{
|
||||
public class DynamicContractResolver : DefaultContractResolver
|
||||
{
|
||||
private readonly char _startingWithChar;
|
||||
public DynamicContractResolver(char startingWithChar)
|
||||
: base(false)
|
||||
public class DynamicContractResolver : DefaultContractResolver
|
||||
{
|
||||
_startingWithChar = startingWithChar;
|
||||
private readonly char _startingWithChar;
|
||||
|
||||
public DynamicContractResolver(char startingWithChar)
|
||||
: base(false)
|
||||
{
|
||||
_startingWithChar = startingWithChar;
|
||||
}
|
||||
|
||||
protected override IList<JsonProperty> CreateProperties(Type type, MemberSerialization memberSerialization)
|
||||
{
|
||||
IList<JsonProperty> properties = base.CreateProperties(type, memberSerialization);
|
||||
|
||||
// only serializer properties that start with the specified character
|
||||
properties =
|
||||
properties.Where(p => p.PropertyName.StartsWith(_startingWithChar.ToString())).ToList();
|
||||
|
||||
return properties;
|
||||
}
|
||||
}
|
||||
|
||||
protected override IList<JsonProperty> CreateProperties(Type type, MemberSerialization memberSerialization)
|
||||
public class EscapedPropertiesContractResolver : DefaultContractResolver
|
||||
{
|
||||
IList<JsonProperty> properties = base.CreateProperties(type, memberSerialization);
|
||||
|
||||
// only serializer properties that start with the specified character
|
||||
properties =
|
||||
properties.Where(p => p.PropertyName.StartsWith(_startingWithChar.ToString())).ToList();
|
||||
|
||||
return properties;
|
||||
protected internal override string ResolvePropertyName(string propertyName)
|
||||
{
|
||||
return base.ResolvePropertyName(propertyName + @"-'-""-");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public class EscapedPropertiesContractResolver : DefaultContractResolver
|
||||
{
|
||||
protected internal override string ResolvePropertyName(string propertyName)
|
||||
public class Book
|
||||
{
|
||||
return base.ResolvePropertyName(propertyName + @"-'-""-");
|
||||
public string BookName { get; set; }
|
||||
public decimal BookPrice { get; set; }
|
||||
public string AuthorName { get; set; }
|
||||
public int AuthorAge { get; set; }
|
||||
public string AuthorCountry { get; set; }
|
||||
}
|
||||
}
|
||||
|
||||
public class Book
|
||||
{
|
||||
public string BookName { get; set; }
|
||||
public decimal BookPrice { get; set; }
|
||||
public string AuthorName { get; set; }
|
||||
public int AuthorAge { get; set; }
|
||||
public string AuthorCountry { get; set; }
|
||||
}
|
||||
|
||||
public class IPersonContractResolver : DefaultContractResolver
|
||||
{
|
||||
protected override JsonContract CreateContract(Type objectType)
|
||||
public class IPersonContractResolver : DefaultContractResolver
|
||||
{
|
||||
if (objectType == typeof(Employee))
|
||||
objectType = typeof(IPerson);
|
||||
protected override JsonContract CreateContract(Type objectType)
|
||||
{
|
||||
if (objectType == typeof(Employee))
|
||||
objectType = typeof(IPerson);
|
||||
|
||||
return base.CreateContract(objectType);
|
||||
return base.CreateContract(objectType);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public class AddressWithDataMember
|
||||
{
|
||||
public class AddressWithDataMember
|
||||
{
|
||||
#if !NET20
|
||||
[DataMember(Name = "CustomerAddress1")]
|
||||
[DataMember(Name = "CustomerAddress1")]
|
||||
#endif
|
||||
public string AddressLine1 { get; set; }
|
||||
}
|
||||
|
||||
[TestFixture]
|
||||
public class ContractResolverTests : TestFixtureBase
|
||||
{
|
||||
[Test]
|
||||
public void SerializeWithEscapedPropertyName()
|
||||
{
|
||||
string json = JsonConvert.SerializeObject(
|
||||
new AddressWithDataMember
|
||||
{
|
||||
AddressLine1 = "value!"
|
||||
},
|
||||
new JsonSerializerSettings
|
||||
{
|
||||
ContractResolver = new EscapedPropertiesContractResolver()
|
||||
});
|
||||
|
||||
Assert.AreEqual(@"{""AddressLine1-'-\""-"":""value!""}", json);
|
||||
|
||||
JsonTextReader reader = new JsonTextReader(new StringReader(json));
|
||||
reader.Read();
|
||||
reader.Read();
|
||||
|
||||
Assert.AreEqual(@"AddressLine1-'-""-", reader.Value);
|
||||
public string AddressLine1 { get; set; }
|
||||
}
|
||||
|
||||
[TestFixture]
|
||||
public class ContractResolverTests : TestFixtureBase
|
||||
{
|
||||
[Test]
|
||||
public void SerializeWithEscapedPropertyName()
|
||||
{
|
||||
string json = JsonConvert.SerializeObject(
|
||||
new AddressWithDataMember
|
||||
{
|
||||
AddressLine1 = "value!"
|
||||
},
|
||||
new JsonSerializerSettings
|
||||
{
|
||||
ContractResolver = new EscapedPropertiesContractResolver()
|
||||
});
|
||||
|
||||
Assert.AreEqual(@"{""AddressLine1-'-\""-"":""value!""}", json);
|
||||
|
||||
JsonTextReader reader = new JsonTextReader(new StringReader(json));
|
||||
reader.Read();
|
||||
reader.Read();
|
||||
|
||||
Assert.AreEqual(@"AddressLine1-'-""-", reader.Value);
|
||||
}
|
||||
|
||||
#if !NET20
|
||||
[Test]
|
||||
public void DeserializeDataMemberClassWithNoDataContract()
|
||||
{
|
||||
var resolver = new DefaultContractResolver();
|
||||
var contract = (JsonObjectContract)resolver.ResolveContract(typeof(AddressWithDataMember));
|
||||
[Test]
|
||||
public void DeserializeDataMemberClassWithNoDataContract()
|
||||
{
|
||||
var resolver = new DefaultContractResolver();
|
||||
var contract = (JsonObjectContract)resolver.ResolveContract(typeof(AddressWithDataMember));
|
||||
|
||||
Assert.AreEqual("AddressLine1", contract.Properties[0].PropertyName);
|
||||
}
|
||||
Assert.AreEqual("AddressLine1", contract.Properties[0].PropertyName);
|
||||
}
|
||||
#endif
|
||||
|
||||
[Test]
|
||||
public void ResolveProperties_IgnoreStatic()
|
||||
{
|
||||
var resolver = new DefaultContractResolver();
|
||||
var contract = (JsonObjectContract)resolver.ResolveContract(typeof(NumberFormatInfo));
|
||||
[Test]
|
||||
public void ResolveProperties_IgnoreStatic()
|
||||
{
|
||||
var resolver = new DefaultContractResolver();
|
||||
var contract = (JsonObjectContract)resolver.ResolveContract(typeof(NumberFormatInfo));
|
||||
|
||||
Assert.IsFalse(contract.Properties.Any(c => c.PropertyName == "InvariantInfo"));
|
||||
}
|
||||
Assert.IsFalse(contract.Properties.Any(c => c.PropertyName == "InvariantInfo"));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void SerializeInterface()
|
||||
{
|
||||
Employee employee = new Employee
|
||||
{
|
||||
BirthDate = new DateTime(1977, 12, 30, 1, 1, 1, DateTimeKind.Utc),
|
||||
FirstName = "Maurice",
|
||||
LastName = "Moss",
|
||||
Department = "IT",
|
||||
JobTitle = "Support"
|
||||
};
|
||||
[Test]
|
||||
public void SerializeInterface()
|
||||
{
|
||||
Employee employee = new Employee
|
||||
{
|
||||
BirthDate = new DateTime(1977, 12, 30, 1, 1, 1, DateTimeKind.Utc),
|
||||
FirstName = "Maurice",
|
||||
LastName = "Moss",
|
||||
Department = "IT",
|
||||
JobTitle = "Support"
|
||||
};
|
||||
|
||||
string iPersonJson = JsonConvert.SerializeObject(employee, Formatting.Indented,
|
||||
new JsonSerializerSettings { ContractResolver = new IPersonContractResolver() });
|
||||
string iPersonJson = JsonConvert.SerializeObject(employee, Formatting.Indented,
|
||||
new JsonSerializerSettings { ContractResolver = new IPersonContractResolver() });
|
||||
|
||||
Assert.AreEqual(@"{
|
||||
Assert.AreEqual(@"{
|
||||
""FirstName"": ""Maurice"",
|
||||
""LastName"": ""Moss"",
|
||||
""BirthDate"": ""1977-12-30T01:01:01Z""
|
||||
}", iPersonJson);
|
||||
}
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void SingleTypeWithMultipleContractResolvers()
|
||||
{
|
||||
Book book = new Book
|
||||
{
|
||||
BookName = "The Gathering Storm",
|
||||
BookPrice = 16.19m,
|
||||
AuthorName = "Brandon Sanderson",
|
||||
AuthorAge = 34,
|
||||
AuthorCountry = "United States of America"
|
||||
};
|
||||
[Test]
|
||||
public void SingleTypeWithMultipleContractResolvers()
|
||||
{
|
||||
Book book = new Book
|
||||
{
|
||||
BookName = "The Gathering Storm",
|
||||
BookPrice = 16.19m,
|
||||
AuthorName = "Brandon Sanderson",
|
||||
AuthorAge = 34,
|
||||
AuthorCountry = "United States of America"
|
||||
};
|
||||
|
||||
string startingWithA = JsonConvert.SerializeObject(book, Formatting.Indented,
|
||||
new JsonSerializerSettings { ContractResolver = new DynamicContractResolver('A') });
|
||||
string startingWithA = JsonConvert.SerializeObject(book, Formatting.Indented,
|
||||
new JsonSerializerSettings { ContractResolver = new DynamicContractResolver('A') });
|
||||
|
||||
// {
|
||||
// "AuthorName": "Brandon Sanderson",
|
||||
// "AuthorAge": 34,
|
||||
// "AuthorCountry": "United States of America"
|
||||
// }
|
||||
// {
|
||||
// "AuthorName": "Brandon Sanderson",
|
||||
// "AuthorAge": 34,
|
||||
// "AuthorCountry": "United States of America"
|
||||
// }
|
||||
|
||||
string startingWithB = JsonConvert.SerializeObject(book, Formatting.Indented,
|
||||
new JsonSerializerSettings { ContractResolver = new DynamicContractResolver('B') });
|
||||
string startingWithB = JsonConvert.SerializeObject(book, Formatting.Indented,
|
||||
new JsonSerializerSettings { ContractResolver = new DynamicContractResolver('B') });
|
||||
|
||||
// {
|
||||
// "BookName": "The Gathering Storm",
|
||||
// "BookPrice": 16.19
|
||||
// }
|
||||
// {
|
||||
// "BookName": "The Gathering Storm",
|
||||
// "BookPrice": 16.19
|
||||
// }
|
||||
|
||||
Assert.AreEqual(@"{
|
||||
Assert.AreEqual(@"{
|
||||
""AuthorName"": ""Brandon Sanderson"",
|
||||
""AuthorAge"": 34,
|
||||
""AuthorCountry"": ""United States of America""
|
||||
}", startingWithA);
|
||||
|
||||
Assert.AreEqual(@"{
|
||||
Assert.AreEqual(@"{
|
||||
""BookName"": ""The Gathering Storm"",
|
||||
""BookPrice"": 16.19
|
||||
}", startingWithB);
|
||||
}
|
||||
}
|
||||
|
||||
#if !(NETFX_CORE || PORTABLE || PORTABLE40)
|
||||
[Test]
|
||||
public void SerializeCompilerGeneratedMembers()
|
||||
{
|
||||
StructTest structTest = new StructTest
|
||||
[Test]
|
||||
public void SerializeCompilerGeneratedMembers()
|
||||
{
|
||||
IntField = 1,
|
||||
IntProperty = 2,
|
||||
StringField = "Field",
|
||||
StringProperty = "Property"
|
||||
};
|
||||
StructTest structTest = new StructTest
|
||||
{
|
||||
IntField = 1,
|
||||
IntProperty = 2,
|
||||
StringField = "Field",
|
||||
StringProperty = "Property"
|
||||
};
|
||||
|
||||
DefaultContractResolver skipCompilerGeneratedResolver = new DefaultContractResolver
|
||||
{
|
||||
DefaultMembersSearchFlags = BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public
|
||||
};
|
||||
DefaultContractResolver skipCompilerGeneratedResolver = new DefaultContractResolver
|
||||
{
|
||||
DefaultMembersSearchFlags = BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public
|
||||
};
|
||||
|
||||
string skipCompilerGeneratedJson = JsonConvert.SerializeObject(structTest, Formatting.Indented,
|
||||
new JsonSerializerSettings { ContractResolver = skipCompilerGeneratedResolver });
|
||||
string skipCompilerGeneratedJson = JsonConvert.SerializeObject(structTest, Formatting.Indented,
|
||||
new JsonSerializerSettings { ContractResolver = skipCompilerGeneratedResolver });
|
||||
|
||||
Assert.AreEqual(@"{
|
||||
Assert.AreEqual(@"{
|
||||
""StringField"": ""Field"",
|
||||
""IntField"": 1,
|
||||
""StringProperty"": ""Property"",
|
||||
""IntProperty"": 2
|
||||
}", skipCompilerGeneratedJson);
|
||||
|
||||
DefaultContractResolver includeCompilerGeneratedResolver = new DefaultContractResolver
|
||||
{
|
||||
DefaultMembersSearchFlags = BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public,
|
||||
SerializeCompilerGeneratedMembers = true
|
||||
};
|
||||
DefaultContractResolver includeCompilerGeneratedResolver = new DefaultContractResolver
|
||||
{
|
||||
DefaultMembersSearchFlags = BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public,
|
||||
SerializeCompilerGeneratedMembers = true
|
||||
};
|
||||
|
||||
string includeCompilerGeneratedJson = JsonConvert.SerializeObject(structTest, Formatting.Indented,
|
||||
new JsonSerializerSettings { ContractResolver = includeCompilerGeneratedResolver });
|
||||
string includeCompilerGeneratedJson = JsonConvert.SerializeObject(structTest, Formatting.Indented,
|
||||
new JsonSerializerSettings { ContractResolver = includeCompilerGeneratedResolver });
|
||||
|
||||
Assert.AreEqual(@"{
|
||||
Assert.AreEqual(@"{
|
||||
""StringField"": ""Field"",
|
||||
""IntField"": 1,
|
||||
""<StringProperty>k__BackingField"": ""Property"",
|
||||
@@ -256,7 +257,7 @@ namespace Newtonsoft.Json.Tests.Serialization
|
||||
""StringProperty"": ""Property"",
|
||||
""IntProperty"": 2
|
||||
}", includeCompilerGeneratedJson);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user