Skip to content

Commit e130428

Browse files
Flurl JSON serialization based on CouchSettings' PropertyCaseType.
1 parent b2d73c5 commit e130428

File tree

7 files changed

+72
-24
lines changed

7 files changed

+72
-24
lines changed

CHANGELOG.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1-
# 1.0.1-beta.4 (2019-04-25)
1+
# 1.0.1 (2019-04-27)
2+
3+
## Bug Fixes
4+
* **Everywhere:** Flurl JSON serialization based on CouchSettings' PropertyCaseType.
5+
6+
# 1.0.1-beta.4 (2019-04-25)
27

38
## Features
49
* **CouchClient:** add FindManyAsync(ids) ([#PR33](https://github.com/matteobortolazzo/couchdb-net/pull/33)).

LATEST_CHANGE.md

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,2 @@
1-
## Features
2-
* **CouchClient:** add FindManyAsync(ids) ([#PR33](https://github.com/matteobortolazzo/couchdb-net/pull/33)).
3-
* **CouchClient:** add QueryAsync(someMangoJson) ([#PR32](https://github.com/matteobortolazzo/couchdb-net/pull/32)).
4-
* **CouchClient:** add QueryAsync(someMangoObject) ([b4dd1b2](https://github.com/matteobortolazzo/couchdb-net/commit/b4dd1b2)).
5-
6-
## Bug Fixes
7-
* **_find:** removed T : IComparable from In() and Contains() methods ([#PR31](https://github.com/matteobortolazzo/couchdb-net/pull/31)).
8-
* **_find:** fix single element array queries. ([#PR34](https://github.com/matteobortolazzo/couchdb-net/pull/34)).
1+
## Bug Fixes
2+
* **Everywhere:** Flurl JSON serialization based on CouchSettings' PropertyCaseType.

src/CouchDB.Driver/CouchClient.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
using CouchDB.Driver.Settings;
1313
using CouchDB.Driver.DTOs;
1414
using CouchDB.Driver.Exceptions;
15+
using Newtonsoft.Json;
1516

1617
namespace CouchDB.Driver
1718
{
@@ -46,6 +47,10 @@ public CouchClient(string connectionString, Action<CouchSettings> couchSettingsF
4647
ConnectionString = connectionString;
4748
_flurlClient = new FlurlClient(connectionString).Configure(s =>
4849
{
50+
s.JsonSerializer = new NewtonsoftJsonSerializer(new JsonSerializerSettings
51+
{
52+
ContractResolver = new CouchContractResolver(_settings.PropertiesCase)
53+
});
4954
s.BeforeCall = OnBeforeCall;
5055
if (_settings.ServerCertificateCustomValidationCallback != null)
5156
{
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Reflection;
4+
using System.Text;
5+
using CouchDB.Driver.Settings;
6+
using Newtonsoft.Json;
7+
8+
namespace CouchDB.Driver.Extensions
9+
{
10+
internal static class MemberInfoExtensions
11+
{
12+
public static string GetCouchPropertyName(this MemberInfo memberInfo, PropertyCaseType propertyCaseType)
13+
{
14+
var jsonPropertyAttributes = memberInfo.GetCustomAttributes(typeof(JsonPropertyAttribute), true);
15+
JsonPropertyAttribute jsonProperty = jsonPropertyAttributes.Length > 0 ?
16+
jsonPropertyAttributes[0] as JsonPropertyAttribute : null;
17+
18+
if (jsonProperty != null)
19+
{
20+
return jsonProperty.PropertyName;
21+
}
22+
return propertyCaseType.Convert(memberInfo.Name);
23+
}
24+
}
25+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
using System.Reflection;
2+
using CouchDB.Driver.Extensions;
3+
using CouchDB.Driver.Settings;
4+
using Newtonsoft.Json;
5+
using Newtonsoft.Json.Serialization;
6+
7+
namespace CouchDB.Driver.Helpers
8+
{
9+
public class CouchContractResolver : DefaultContractResolver
10+
{
11+
private readonly PropertyCaseType _propertyCaseType;
12+
13+
public CouchContractResolver(PropertyCaseType propertyCaseType)
14+
{
15+
_propertyCaseType = propertyCaseType;
16+
}
17+
protected override JsonProperty CreateProperty(MemberInfo member, MemberSerialization memberSerialization)
18+
{
19+
JsonProperty property = base.CreateProperty(member, memberSerialization);
20+
if (property != null && !property.Ignored)
21+
{
22+
property.PropertyName = member.GetCouchPropertyName(_propertyCaseType);
23+
}
24+
return property;
25+
}
26+
}
27+
}

src/CouchDB.Driver/Settings/CouchSettings.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ internal CouchSettings()
2929
{
3030
AuthenticationType = AuthenticationType.None;
3131
PluralizeEntitis = true;
32-
DocumentsCaseType = (DocumentCaseType)DocumentCaseType.UnderscoreCase;
32+
DocumentsCaseType = DocumentCaseType.UnderscoreCase;
3333
PropertiesCase = PropertyCaseType.CamelCase;
3434
}
3535

src/CouchDB.Driver/Translators/MemberExpressionTranslator.cs

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
using CouchDB.Driver.Types;
1+
using CouchDB.Driver.Extensions;
2+
using CouchDB.Driver.Settings;
3+
using CouchDB.Driver.Types;
24
using Humanizer;
35
using Newtonsoft.Json;
46
using System.Collections.Generic;
@@ -11,25 +13,15 @@ internal partial class QueryTranslator
1113
{
1214
protected override Expression VisitMember(MemberExpression m)
1315
{
14-
string GetPropertyName(MemberInfo memberInfo)
15-
{
16-
var jsonPropertyAttributes = memberInfo.GetCustomAttributes(typeof(JsonPropertyAttribute), true);
17-
var jsonProperty = jsonPropertyAttributes.Length > 0 ? jsonPropertyAttributes[0] as JsonPropertyAttribute : null;
18-
19-
if (jsonProperty != null)
20-
{
21-
return jsonProperty.PropertyName;
22-
}
23-
return _settings.PropertiesCase.Convert(memberInfo.Name);
24-
}
16+
PropertyCaseType caseType = _settings.PropertiesCase;
2517

26-
var members = new List<string> { GetPropertyName(m.Member) };
18+
var members = new List<string> { m.Member.GetCouchPropertyName(caseType) };
2719

2820
var currentExpression = m.Expression;
2921

3022
while (currentExpression is MemberExpression cm)
3123
{
32-
members.Add(GetPropertyName(cm.Member));
24+
members.Add(cm.Member.GetCouchPropertyName(caseType));
3325
currentExpression = cm.Expression;
3426
}
3527

0 commit comments

Comments
 (0)