Skip to content

Commit

Permalink
😿 Reverted System.Text.Json -> Newtonsoft.Json (#35)
Browse files Browse the repository at this point in the history
* 😿 Reverted System.Text.Json -> Newtonsoft.Json
System.Text.Json doesn't work for Polymorphic types. Crazy :(

Also a deal breaker.

So reverting back to the older and slower Newtonsoft.Json.

* Ignore launchSettings.json for the test project.
Just ignore everytime the app keeps recreating the launchSettings.json in this test project. It's a weird sideeffect of having a test project but also a webproject in it.
  • Loading branch information
PureKrome authored Oct 16, 2020
1 parent 28e20b2 commit 74f8868
Show file tree
Hide file tree
Showing 13 changed files with 64 additions and 117 deletions.
39 changes: 0 additions & 39 deletions src/Homely.AspNetCore.Mvc.Helpers/DateTimeConverter.cs

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
using Homely.AspNetCore.Mvc.Helpers.Controllers;
using Homely.AspNetCore.Mvc.Helpers.Models;
using Microsoft.Extensions.DependencyInjection;
using Newtonsoft.Json.Converters;
using Newtonsoft.Json.Serialization;
using System;
using System.Reflection;
using System.Text.Json;
using System.Text.Json.Serialization;

namespace Homely.AspNetCore.Mvc.Helpers.Extensions
{
Expand Down Expand Up @@ -54,17 +54,24 @@ public static IMvcBuilder AddDefaultJsonOptions(this IMvcBuilder builder,
bool isIndented = false,
string dateTimeFormat = null)
{
return builder.AddJsonOptions(options =>
return builder.AddNewtonsoftJson(options =>
{
options.JsonSerializerOptions.IgnoreNullValues = true;
options.JsonSerializerOptions.WriteIndented = isIndented;
options.JsonSerializerOptions.PropertyNamingPolicy = JsonNamingPolicy.CamelCase;
options.JsonSerializerOptions.Converters.Add(new JsonStringEnumConverter(JsonNamingPolicy.CamelCase));
var contractResolver = new DefaultContractResolver
{
NamingStrategy = new CamelCaseNamingStrategy()
};

options.SerializerSettings.NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore;
options.SerializerSettings.Formatting = isIndented
? Newtonsoft.Json.Formatting.Indented
: Newtonsoft.Json.Formatting.None;
options.SerializerSettings.ContractResolver = contractResolver;
options.SerializerSettings.Converters.Add(new StringEnumConverter(new CamelCaseNamingStrategy()));

// If we have specified a custom date-time format, then use the specific custom converter.
if (!string.IsNullOrWhiteSpace(dateTimeFormat))
{
options.JsonSerializerOptions.Converters.Add(new DateTimeConverter(dateTimeFormat));
options.SerializerSettings.Converters.Add(new IsoDateTimeConverter { DateTimeFormat = dateTimeFormat });
}
});
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
Expand All @@ -21,6 +21,7 @@

<ItemGroup>
<PackageReference Include="Hellang.Middleware.ProblemDetails" Version="5.1.1" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="3.1.9" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="3.1.8" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="3.1.8" />
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.0.0" PrivateAssets="All" />
Expand Down
1 change: 1 addition & 0 deletions tests/Homely.AspNetCore.Mvc.Helpers.Tests/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Properties/launchSettings.json

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Shouldly;
using System;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,16 @@ public async Task GivenSomeModel_Get_ReturnsASyntaxCorrectJsonText(int id,
? $"{{{Environment.NewLine} \""
: $"{{\"id\"";
responseBody.ShouldStartWith(startingJson); // WriteIndented check.

if (id == 1)
{
responseBody.ShouldContain("someBaseClass", Case.Sensitive); // Base class serializes.
responseBody.ShouldContain("aaa", Case.Sensitive); // Abstract class property serializes.
responseBody.ShouldContain("bbb", Case.Sensitive); // Base class property serializes.

responseBody.ShouldContain("someDerivedClass", Case.Sensitive); // Base class serializes.
responseBody.ShouldContain("ccc", Case.Sensitive); // Derived class property serializes.
}
}
}
}
7 changes: 7 additions & 0 deletions tests/TestWebApplication/Models/AbstractClass.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace TestWebApplication.Models
{
public abstract class AbstractClass
{
public string SomeAbstractClassProperty { get; set; }
}
}
7 changes: 7 additions & 0 deletions tests/TestWebApplication/Models/BaseClass.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace TestWebApplication.Models
{
public class BaseClass : AbstractClass
{
public string SomeBaseClassProperty { get; set; }
}
}
7 changes: 7 additions & 0 deletions tests/TestWebApplication/Models/DerivedClass.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace TestWebApplication.Models
{
public class DerivedClass : BaseClass
{
public string SomeDerivedClassProperty { get; set; }
}
}
2 changes: 2 additions & 0 deletions tests/TestWebApplication/Models/FakeVehicle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,7 @@ public class FakeVehicle
/// <remarks>Optional - might not always have this data.</remarks>
public string VIN { get; set; }
public DateTime CreatedOn { get; set; } = DateTime.UtcNow;
public BaseClass SomeBaseClass { get; set; }
public DerivedClass SomeDerivedClass { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,18 @@ public static FakeVehicleRepository CreateAFakeVehicleRepository()
Name = "Name1",
RegistrationNumber = "RegistrationNumber1",
Colour = ColourType.Grey,
CreatedOn = new System.DateTime(2000, 1, 2, 3, 4, 5, 0) // Testing ZERO milliseconds.
CreatedOn = new System.DateTime(2000, 1, 2, 3, 4, 5, 0), // Testing ZERO milliseconds.
SomeBaseClass = new BaseClass
{
SomeAbstractClassProperty = "aaa",
SomeBaseClassProperty = "bbb"
},
SomeDerivedClass = new DerivedClass
{
SomeAbstractClassProperty = "aaa",
SomeBaseClassProperty = "bbb",
SomeDerivedClassProperty = "ccc"
}
});

stubbedFakeVehicleRepository.Add(new FakeVehicle
Expand Down

0 comments on commit 74f8868

Please sign in to comment.