Skip to content

Commit 5bf0e52

Browse files
dnperforsjaredcnance
authored andcommitted
feat(JsonApiOptions): Add SerializerSettings to JsonApiOptions
* Add SerializerSettings to JsonApiOptions * Fixed spacing, and added obsolete attribute to JsonContractResolver * Correctly fix spacing.
1 parent f5a011a commit 5bf0e52

File tree

5 files changed

+42
-42
lines changed

5 files changed

+42
-42
lines changed

src/JsonApiDotNetCore/Configuration/JsonApiOptions.cs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using JsonApiDotNetCore.Internal;
44
using JsonApiDotNetCore.Serialization;
55
using Microsoft.EntityFrameworkCore;
6+
using Newtonsoft.Json;
67
using Newtonsoft.Json.Serialization;
78

89
namespace JsonApiDotNetCore.Configuration
@@ -21,10 +22,20 @@ public class JsonApiOptions
2122
/// of the v1 spec. However, we have decided that this is a real
2223
/// requirement for users of this library and a gap in the specification.
2324
/// It will likely be removed when the spec is updated to support this
24-
/// requirement.
25+
/// requirement.
2526
/// </summary>
2627
public bool AllowCustomQueryParameters { get; set; }
27-
public IContractResolver JsonContractResolver { get; set; } = new DasherizedResolver();
28+
[Obsolete("JsonContract resolver can now be set on SerializerSettings.")]
29+
public IContractResolver JsonContractResolver
30+
{
31+
get => SerializerSettings.ContractResolver;
32+
set => SerializerSettings.ContractResolver = value;
33+
}
34+
public JsonSerializerSettings SerializerSettings { get; } = new JsonSerializerSettings()
35+
{
36+
NullValueHandling = NullValueHandling.Ignore,
37+
ContractResolver = new DasherizedResolver()
38+
};
2839
internal IContextGraphBuilder ContextGraphBuilder { get; } = new ContextGraphBuilder();
2940

3041
public void BuildContextGraph<TContext>(Action<IContextGraphBuilder> builder)

src/JsonApiDotNetCore/Serialization/JsonApiDeSerializer.cs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -135,12 +135,7 @@ private object ConvertAttrValue(object newValue, Type targetType)
135135

136136
private object DeserializeComplexType(JContainer obj, Type targetType)
137137
{
138-
var serializerSettings = new JsonSerializerSettings
139-
{
140-
ContractResolver = _jsonApiContext.Options.JsonContractResolver
141-
};
142-
143-
return obj.ToObject(targetType, JsonSerializer.Create(serializerSettings));
138+
return obj.ToObject(targetType, JsonSerializer.Create(_jsonApiContext.Options.SerializerSettings));
144139
}
145140

146141
private object SetRelationships(
@@ -223,4 +218,4 @@ private object SetHasManyRelationship(object entity,
223218
return entity;
224219
}
225220
}
226-
}
221+
}

src/JsonApiDotNetCore/Serialization/JsonApiSerializer.cs

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,12 @@ public JsonApiSerializer(
1919
IDocumentBuilder documentBuilder)
2020
{
2121
_jsonApiContext = jsonApiContext;
22-
_documentBuilder = documentBuilder;
22+
_documentBuilder = documentBuilder;
2323
}
2424

2525
public JsonApiSerializer(
2626
IJsonApiContext jsonApiContext,
27-
IDocumentBuilder documentBuilder,
27+
IDocumentBuilder documentBuilder,
2828
ILoggerFactory loggerFactory)
2929
{
3030
_jsonApiContext = jsonApiContext;
@@ -43,7 +43,7 @@ public string Serialize(object entity)
4343
if (entity is IEnumerable<IIdentifiable>)
4444
return SerializeDocuments(entity);
4545

46-
return SerializeDocument(entity);
46+
return SerializeDocument(entity);
4747
}
4848

4949
private string GetNullDataResponse()
@@ -83,10 +83,7 @@ private string SerializeDocument(object entity)
8383

8484
private string _serialize(object obj)
8585
{
86-
return JsonConvert.SerializeObject(obj, new JsonSerializerSettings {
87-
NullValueHandling = NullValueHandling.Ignore,
88-
ContractResolver = _jsonApiContext.Options.JsonContractResolver
89-
});
86+
return JsonConvert.SerializeObject(obj, _jsonApiContext.Options.SerializerSettings);
9087
}
9188
}
92-
}
89+
}

test/UnitTests/Serialization/JsonApiDeSerializerTests.cs

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,10 @@ public void Can_Deserialize_Complex_Types()
2626
jsonApiContextMock.SetupAllProperties();
2727
jsonApiContextMock.Setup(m => m.ContextGraph).Returns(contextGraph);
2828
jsonApiContextMock.Setup(m => m.AttributesToUpdate).Returns(new Dictionary<AttrAttribute, object>());
29-
jsonApiContextMock.Setup(m => m.Options).Returns(new JsonApiOptions
30-
{
31-
JsonContractResolver = new CamelCasePropertyNamesContractResolver()
32-
});
29+
30+
var jsonApiOptions = new JsonApiOptions();
31+
jsonApiOptions.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
32+
jsonApiContextMock.Setup(m => m.Options).Returns(jsonApiOptions);
3333

3434
var genericProcessorFactoryMock = new Mock<IGenericProcessorFactory>();
3535

@@ -69,10 +69,9 @@ public void Can_Deserialize_Complex_List_Types()
6969
jsonApiContextMock.SetupAllProperties();
7070
jsonApiContextMock.Setup(m => m.ContextGraph).Returns(contextGraph);
7171
jsonApiContextMock.Setup(m => m.AttributesToUpdate).Returns(new Dictionary<AttrAttribute, object>());
72-
jsonApiContextMock.Setup(m => m.Options).Returns(new JsonApiOptions
73-
{
74-
JsonContractResolver = new CamelCasePropertyNamesContractResolver()
75-
});
72+
var jsonApiOptions = new JsonApiOptions();
73+
jsonApiOptions.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
74+
jsonApiContextMock.Setup(m => m.Options).Returns(jsonApiOptions);
7675

7776
var genericProcessorFactoryMock = new Mock<IGenericProcessorFactory>();
7877

@@ -116,10 +115,9 @@ public void Can_Deserialize_Complex_Types_With_Dasherized_Attrs()
116115
jsonApiContextMock.Setup(m => m.ContextGraph).Returns(contextGraph);
117116
jsonApiContextMock.Setup(m => m.AttributesToUpdate).Returns(new Dictionary<AttrAttribute, object>());
118117

119-
jsonApiContextMock.Setup(m => m.Options).Returns(new JsonApiOptions
120-
{
121-
JsonContractResolver = new DasherizedResolver() // <---
122-
});
118+
var jsonApiOptions = new JsonApiOptions();
119+
jsonApiOptions.SerializerSettings.ContractResolver = new DasherizedResolver(); // <--
120+
jsonApiContextMock.Setup(m => m.Options).Returns(jsonApiOptions);
123121

124122
var genericProcessorFactoryMock = new Mock<IGenericProcessorFactory>();
125123

@@ -162,10 +160,9 @@ public void Immutable_Attrs_Are_Not_Included_In_AttributesToUpdate()
162160
jsonApiContextMock.Setup(m => m.ContextGraph).Returns(contextGraph);
163161
jsonApiContextMock.Setup(m => m.AttributesToUpdate).Returns(attributesToUpdate);
164162

165-
jsonApiContextMock.Setup(m => m.Options).Returns(new JsonApiOptions
166-
{
167-
JsonContractResolver = new DasherizedResolver()
168-
});
163+
var jsonApiOptions = new JsonApiOptions();
164+
jsonApiOptions.SerializerSettings.ContractResolver = new DasherizedResolver();
165+
jsonApiContextMock.Setup(m => m.Options).Returns(jsonApiOptions);
169166

170167
var genericProcessorFactoryMock = new Mock<IGenericProcessorFactory>();
171168

@@ -178,8 +175,8 @@ public void Immutable_Attrs_Are_Not_Included_In_AttributesToUpdate()
178175
Type = "test-resource",
179176
Id = "1",
180177
Attributes = new Dictionary<string, object> {
181-
{ "complex-member", new Dictionary<string, string> {
182-
{ "compound-name", "testName" } }
178+
{ "complex-member", new Dictionary<string, string> {
179+
{ "compound-name", "testName" } }
183180
},
184181
{ "immutable", "value"}
185182
}
@@ -194,8 +191,8 @@ public void Immutable_Attrs_Are_Not_Included_In_AttributesToUpdate()
194191
// assert
195192
Assert.NotNull(result.ComplexMember);
196193
Assert.Equal(1, attributesToUpdate.Count);
197-
198-
foreach(var attr in attributesToUpdate)
194+
195+
foreach (var attr in attributesToUpdate)
199196
Assert.False(attr.Key.IsImmutable);
200197
}
201198

test/UnitTests/Serialization/JsonApiSerializerTests.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,18 +26,18 @@ public void Can_Serialize_Complex_Types()
2626
var jsonApiContextMock = new Mock<IJsonApiContext>();
2727
jsonApiContextMock.SetupAllProperties();
2828
jsonApiContextMock.Setup(m => m.ContextGraph).Returns(contextGraph);
29-
jsonApiContextMock.Setup(m => m.Options).Returns(new JsonApiOptions {
30-
JsonContractResolver = new DasherizedResolver()
31-
});
29+
jsonApiContextMock.Setup(m => m.Options).Returns(new JsonApiOptions());
3230
jsonApiContextMock.Setup(m => m.RequestEntity)
3331
.Returns(contextGraph.GetContextEntity("test-resource"));
3432
jsonApiContextMock.Setup(m => m.MetaBuilder).Returns(new MetaBuilder());
3533
jsonApiContextMock.Setup(m => m.PageManager).Returns(new PageManager());
3634

3735
var documentBuilder = new DocumentBuilder(jsonApiContextMock.Object);
3836
var serializer = new JsonApiSerializer(jsonApiContextMock.Object, documentBuilder);
39-
var resource = new TestResource {
40-
ComplexMember = new ComplexType {
37+
var resource = new TestResource
38+
{
39+
ComplexMember = new ComplexType
40+
{
4141
CompoundName = "testname"
4242
}
4343
};

0 commit comments

Comments
 (0)