Skip to content

Add SerializerSettings to JsonApiOptions #187

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

Merged
merged 3 commits into from
Nov 11, 2017
Merged
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
15 changes: 13 additions & 2 deletions src/JsonApiDotNetCore/Configuration/JsonApiOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using JsonApiDotNetCore.Internal;
using JsonApiDotNetCore.Serialization;
using Microsoft.EntityFrameworkCore;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;

namespace JsonApiDotNetCore.Configuration
Expand All @@ -21,10 +22,20 @@ public class JsonApiOptions
/// of the v1 spec. However, we have decided that this is a real
/// requirement for users of this library and a gap in the specification.
/// It will likely be removed when the spec is updated to support this
/// requirement.
/// requirement.
/// </summary>
public bool AllowCustomQueryParameters { get; set; }
public IContractResolver JsonContractResolver { get; set; } = new DasherizedResolver();
[Obsolete("JsonContract resolver can now be set on SerializerSettings.")]
public IContractResolver JsonContractResolver
{
get => SerializerSettings.ContractResolver;
set => SerializerSettings.ContractResolver = value;
}
public JsonSerializerSettings SerializerSettings { get; } = new JsonSerializerSettings()
{
NullValueHandling = NullValueHandling.Ignore,
ContractResolver = new DasherizedResolver()
};
internal IContextGraphBuilder ContextGraphBuilder { get; } = new ContextGraphBuilder();

public void BuildContextGraph<TContext>(Action<IContextGraphBuilder> builder)
Expand Down
9 changes: 2 additions & 7 deletions src/JsonApiDotNetCore/Serialization/JsonApiDeSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -135,12 +135,7 @@ private object ConvertAttrValue(object newValue, Type targetType)

private object DeserializeComplexType(JContainer obj, Type targetType)
{
var serializerSettings = new JsonSerializerSettings
{
ContractResolver = _jsonApiContext.Options.JsonContractResolver
};

return obj.ToObject(targetType, JsonSerializer.Create(serializerSettings));
return obj.ToObject(targetType, JsonSerializer.Create(_jsonApiContext.Options.SerializerSettings));
}

private object SetRelationships(
Expand Down Expand Up @@ -223,4 +218,4 @@ private object SetHasManyRelationship(object entity,
return entity;
}
}
}
}
13 changes: 5 additions & 8 deletions src/JsonApiDotNetCore/Serialization/JsonApiSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ public JsonApiSerializer(
IDocumentBuilder documentBuilder)
{
_jsonApiContext = jsonApiContext;
_documentBuilder = documentBuilder;
_documentBuilder = documentBuilder;
}

public JsonApiSerializer(
IJsonApiContext jsonApiContext,
IDocumentBuilder documentBuilder,
IDocumentBuilder documentBuilder,
ILoggerFactory loggerFactory)
{
_jsonApiContext = jsonApiContext;
Expand All @@ -43,7 +43,7 @@ public string Serialize(object entity)
if (entity is IEnumerable<IIdentifiable>)
return SerializeDocuments(entity);

return SerializeDocument(entity);
return SerializeDocument(entity);
}

private string GetNullDataResponse()
Expand Down Expand Up @@ -83,10 +83,7 @@ private string SerializeDocument(object entity)

private string _serialize(object obj)
{
return JsonConvert.SerializeObject(obj, new JsonSerializerSettings {
NullValueHandling = NullValueHandling.Ignore,
ContractResolver = _jsonApiContext.Options.JsonContractResolver
});
return JsonConvert.SerializeObject(obj, _jsonApiContext.Options.SerializerSettings);
}
}
}
}
37 changes: 17 additions & 20 deletions test/UnitTests/Serialization/JsonApiDeSerializerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ public void Can_Deserialize_Complex_Types()
jsonApiContextMock.SetupAllProperties();
jsonApiContextMock.Setup(m => m.ContextGraph).Returns(contextGraph);
jsonApiContextMock.Setup(m => m.AttributesToUpdate).Returns(new Dictionary<AttrAttribute, object>());
jsonApiContextMock.Setup(m => m.Options).Returns(new JsonApiOptions
{
JsonContractResolver = new CamelCasePropertyNamesContractResolver()
});

var jsonApiOptions = new JsonApiOptions();
jsonApiOptions.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
jsonApiContextMock.Setup(m => m.Options).Returns(jsonApiOptions);

var genericProcessorFactoryMock = new Mock<IGenericProcessorFactory>();

Expand Down Expand Up @@ -69,10 +69,9 @@ public void Can_Deserialize_Complex_List_Types()
jsonApiContextMock.SetupAllProperties();
jsonApiContextMock.Setup(m => m.ContextGraph).Returns(contextGraph);
jsonApiContextMock.Setup(m => m.AttributesToUpdate).Returns(new Dictionary<AttrAttribute, object>());
jsonApiContextMock.Setup(m => m.Options).Returns(new JsonApiOptions
{
JsonContractResolver = new CamelCasePropertyNamesContractResolver()
});
var jsonApiOptions = new JsonApiOptions();
jsonApiOptions.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
jsonApiContextMock.Setup(m => m.Options).Returns(jsonApiOptions);

var genericProcessorFactoryMock = new Mock<IGenericProcessorFactory>();

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

jsonApiContextMock.Setup(m => m.Options).Returns(new JsonApiOptions
{
JsonContractResolver = new DasherizedResolver() // <---
});
var jsonApiOptions = new JsonApiOptions();
jsonApiOptions.SerializerSettings.ContractResolver = new DasherizedResolver(); // <--
jsonApiContextMock.Setup(m => m.Options).Returns(jsonApiOptions);

var genericProcessorFactoryMock = new Mock<IGenericProcessorFactory>();

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

jsonApiContextMock.Setup(m => m.Options).Returns(new JsonApiOptions
{
JsonContractResolver = new DasherizedResolver()
});
var jsonApiOptions = new JsonApiOptions();
jsonApiOptions.SerializerSettings.ContractResolver = new DasherizedResolver();
jsonApiContextMock.Setup(m => m.Options).Returns(jsonApiOptions);

var genericProcessorFactoryMock = new Mock<IGenericProcessorFactory>();

Expand All @@ -178,8 +175,8 @@ public void Immutable_Attrs_Are_Not_Included_In_AttributesToUpdate()
Type = "test-resource",
Id = "1",
Attributes = new Dictionary<string, object> {
{ "complex-member", new Dictionary<string, string> {
{ "compound-name", "testName" } }
{ "complex-member", new Dictionary<string, string> {
{ "compound-name", "testName" } }
},
{ "immutable", "value"}
}
Expand All @@ -194,8 +191,8 @@ public void Immutable_Attrs_Are_Not_Included_In_AttributesToUpdate()
// assert
Assert.NotNull(result.ComplexMember);
Assert.Equal(1, attributesToUpdate.Count);
foreach(var attr in attributesToUpdate)

foreach (var attr in attributesToUpdate)
Assert.False(attr.Key.IsImmutable);
}

Expand Down
10 changes: 5 additions & 5 deletions test/UnitTests/Serialization/JsonApiSerializerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,18 @@ public void Can_Serialize_Complex_Types()
var jsonApiContextMock = new Mock<IJsonApiContext>();
jsonApiContextMock.SetupAllProperties();
jsonApiContextMock.Setup(m => m.ContextGraph).Returns(contextGraph);
jsonApiContextMock.Setup(m => m.Options).Returns(new JsonApiOptions {
JsonContractResolver = new DasherizedResolver()
});
jsonApiContextMock.Setup(m => m.Options).Returns(new JsonApiOptions());
jsonApiContextMock.Setup(m => m.RequestEntity)
.Returns(contextGraph.GetContextEntity("test-resource"));
jsonApiContextMock.Setup(m => m.MetaBuilder).Returns(new MetaBuilder());
jsonApiContextMock.Setup(m => m.PageManager).Returns(new PageManager());

var documentBuilder = new DocumentBuilder(jsonApiContextMock.Object);
var serializer = new JsonApiSerializer(jsonApiContextMock.Object, documentBuilder);
var resource = new TestResource {
ComplexMember = new ComplexType {
var resource = new TestResource
{
ComplexMember = new ComplexType
{
CompoundName = "testname"
}
};
Expand Down