Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow JsonProperty(PropertyName='blah') to be used during Serialization #99

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/ArangoDB.Client.Test/ArangoDB.Client.Test.Net45.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
<Compile Include="Model\Product.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Serialization\NamingConvention.cs" />
<Compile Include="Serialization\PropertyNames.cs" />
<Compile Include="Serialization\SimpleDocumentParser.cs" />
<Compile Include="Setting\SerializationSetting.cs" />
<Compile Include="Utility\ObjectUtility.cs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
<Compile Include="Model\Product.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Serialization\NamingConvention.cs" />
<Compile Include="Serialization\PropertyNames.cs" />
<Compile Include="Serialization\SimpleDocumentParser.cs" />
<Compile Include="Setting\SerializationSetting.cs" />
<Compile Include="Utility\ObjectUtility.cs" />
Expand Down
71 changes: 71 additions & 0 deletions src/ArangoDB.Client.Test/Serialization/PropertyNames.cs
Original file line number Diff line number Diff line change
@@ -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; }
}
}
}
3 changes: 3 additions & 0 deletions src/ArangoDB.Client/DatabaseSharedSetting.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,12 @@ public DatabaseSerializationSharedSetting()
SerializeEnumAsInteger = true;
MetadataPropertyHandling = Newtonsoft.Json.MetadataPropertyHandling.Default;
Converters = new List<JsonConverter>();
UseUnderlyingPropertyName = false;
}

public MetadataPropertyHandling MetadataPropertyHandling { get; set; }

public bool UseUnderlyingPropertyName { get; set; }
}

public class DatabaseLogSharedSetting
Expand Down
5 changes: 5 additions & 0 deletions src/ArangoDB.Client/Serialization/DocumentContractResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@ protected override IList<JsonProperty> 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);
Expand Down