diff --git a/src/ArangoDB.Client.Test/ArangoDB.Client.Test.Net45.csproj b/src/ArangoDB.Client.Test/ArangoDB.Client.Test.Net45.csproj index 27d4d06..b0b09fb 100644 --- a/src/ArangoDB.Client.Test/ArangoDB.Client.Test.Net45.csproj +++ b/src/ArangoDB.Client.Test/ArangoDB.Client.Test.Net45.csproj @@ -61,6 +61,7 @@ + diff --git a/src/ArangoDB.Client.Test/ArangoDB.Client.Test.Portable.csproj b/src/ArangoDB.Client.Test/ArangoDB.Client.Test.Portable.csproj index 19f8291..5f8e4d3 100644 --- a/src/ArangoDB.Client.Test/ArangoDB.Client.Test.Portable.csproj +++ b/src/ArangoDB.Client.Test/ArangoDB.Client.Test.Portable.csproj @@ -62,6 +62,7 @@ + diff --git a/src/ArangoDB.Client.Test/Serialization/PropertyNames.cs b/src/ArangoDB.Client.Test/Serialization/PropertyNames.cs new file mode 100644 index 0000000..5122de8 --- /dev/null +++ b/src/ArangoDB.Client.Test/Serialization/PropertyNames.cs @@ -0,0 +1,71 @@ +using System.IO; +using Xunit; +using Newtonsoft.Json; +using Newtonsoft.Json.Linq; +using ArangoDB.Client.Serialization; + +namespace ArangoDB.Client.Test.Serialization { + public class PropertyNames { + + private IArangoDatabase _db; + private ObjectToSerialize _toTest; + private DocumentSerializer _docSerializer; + private JsonSerializer _jsonSerializer; + + public PropertyNames(){ + _toTest = new ObjectToSerialize(); + _db = new ArangoDatabase(); + _docSerializer = new DocumentSerializer(_db); + _jsonSerializer = _docSerializer.CreateJsonSerializer(); + } + + [Fact] + public void UseUnderlyingNames() + { + + _db.SharedSetting.Serialization.UseUnderlyingPropertyName = true; + + string testData = SerializeTestObject(); + JObject jo = JObject.Parse(testData); + + Assert.NotNull(jo["TestProp02"]); + } + + [Fact] + public void UseJsonPropertyNames() + { + _db.SharedSetting.Serialization.UseUnderlyingPropertyName = false; + + string testData = SerializeTestObject(); + JObject jo = JObject.Parse(testData); + + Assert.NotNull(jo["MyTestProp"]); + } + + private string SerializeTestObject(){ + string retVal = null; + + using (MemoryStream ms = new MemoryStream(1024)) { + using (StreamWriter sw = new StreamWriter(ms)) { + using (JsonWriter jw = new JsonTextWriter(sw)) { + _jsonSerializer.Serialize(jw, _toTest); + sw.Flush(); + ms.Seek(0, SeekOrigin.Begin); + + using (StreamReader sr = new StreamReader(ms)) { + retVal = sr.ReadToEnd(); + } + } + } + } + return retVal; + } + + private class ObjectToSerialize { + public bool TestProp01 { get; set; } + + [JsonProperty(PropertyName = "MyTestProp")] + public bool TestProp02 { get; set; } + } + } +} diff --git a/src/ArangoDB.Client/DatabaseSharedSetting.cs b/src/ArangoDB.Client/DatabaseSharedSetting.cs index a3c694d..bb86928 100644 --- a/src/ArangoDB.Client/DatabaseSharedSetting.cs +++ b/src/ArangoDB.Client/DatabaseSharedSetting.cs @@ -96,9 +96,12 @@ public DatabaseSerializationSharedSetting() SerializeEnumAsInteger = true; MetadataPropertyHandling = Newtonsoft.Json.MetadataPropertyHandling.Default; Converters = new List(); + UseUnderlyingPropertyName = false; } public MetadataPropertyHandling MetadataPropertyHandling { get; set; } + + public bool UseUnderlyingPropertyName { get; set; } } public class DatabaseLogSharedSetting diff --git a/src/ArangoDB.Client/Serialization/DocumentContractResolver.cs b/src/ArangoDB.Client/Serialization/DocumentContractResolver.cs index 85b3c33..076b61f 100644 --- a/src/ArangoDB.Client/Serialization/DocumentContractResolver.cs +++ b/src/ArangoDB.Client/Serialization/DocumentContractResolver.cs @@ -46,6 +46,11 @@ protected override IList CreateProperties(Type type, MemberSeriali { if (documentProperty.IgnoreProperty) continue; + + // this change allows the JsonProperty.PropertyName to be used instead of the Object.PropertyName + if (!sharedSetting.Serialization.UseUnderlyingPropertyName && p.PropertyName != p.UnderlyingName) { + documentProperty.PropertyName = p.PropertyName; + } } p.PropertyName = sharedSetting.Collection.ResolvePropertyName(type, p.UnderlyingName);