Skip to content

Commit f477461

Browse files
committed
Added Newtonsoft.Json nuget package, and some classes and helpers for adapting the serialization of objects to json.
1 parent 8c718e4 commit f477461

9 files changed

+147
-0
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
using System;
2+
using Newtonsoft.Json.Serialization;
3+
4+
namespace FooBar.Web.Core.Json
5+
{
6+
public class CamelCaseExceptDictionaryKeysResolver : CamelCasePropertyNamesContractResolver
7+
{
8+
protected override JsonDictionaryContract CreateDictionaryContract(Type objectType)
9+
{
10+
JsonDictionaryContract contract = base.CreateDictionaryContract(objectType);
11+
contract.DictionaryKeyResolver = propertyName => propertyName;
12+
return contract;
13+
}
14+
}
15+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
namespace FooBar.Web.Core.Json
2+
{
3+
public class FooBarContractResolver : NullToEmptyListResolver
4+
{
5+
//Marker class
6+
//As Json.net don't accept multiple contract resolvers we need to use inheritance.
7+
//For additional needs of custom contract resolvers, add your new one to the inheritance chain by changing the inheritance of this class
8+
//and let your new resolver inherit from the one currently inherited here.
9+
}
10+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using System.Globalization;
2+
using Newtonsoft.Json.Converters;
3+
4+
namespace FooBar.Web.Core.Json
5+
{
6+
public class JsonDateConverter : IsoDateTimeConverter
7+
{
8+
public JsonDateConverter()
9+
{
10+
Culture = new CultureInfo("sv-SE"); //Force ISO 8601
11+
DateTimeFormat = "d";
12+
}
13+
}
14+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using System.Globalization;
2+
using Newtonsoft.Json.Converters;
3+
4+
namespace FooBar.Web.Core.Json
5+
{
6+
public class JsonDateTimeConverter : IsoDateTimeConverter
7+
{
8+
public JsonDateTimeConverter()
9+
{
10+
Culture = new CultureInfo("sv-SE"); //Force ISO 8601
11+
DateTimeFormat = "yyyy'-'MM'-'dd' 'HH':'mm";
12+
}
13+
}
14+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
using Newtonsoft.Json;
2+
3+
namespace FooBar.Web.Core.Json
4+
{
5+
public static class JsonHelpers
6+
{
7+
public static JsonSerializerSettings CreateJsonSerializerSettings()
8+
{
9+
var settings = new JsonSerializerSettings { ContractResolver = new FooBarContractResolver() };
10+
settings.Converters.Add(new JsonDateConverter());
11+
return settings;
12+
}
13+
14+
}
15+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
using Newtonsoft.Json;
2+
3+
namespace FooBar.Web.Core.Json
4+
{
5+
public static class JsonObjectExtensions
6+
{
7+
public static string ToJsonCamelCase(this object obj)
8+
{
9+
JsonSerializerSettings settings = JsonHelpers.CreateJsonSerializerSettings();
10+
return JsonConvert.SerializeObject(obj, Formatting.None, settings);
11+
}
12+
}
13+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using System.Globalization;
2+
using Newtonsoft.Json.Converters;
3+
4+
namespace FooBar.Web.Core.Json
5+
{
6+
public class JsonTimeConverter : IsoDateTimeConverter
7+
{
8+
public JsonTimeConverter()
9+
{
10+
Culture = new CultureInfo("sv-SE"); //Force ISO 8601
11+
DateTimeFormat = "t";
12+
}
13+
}
14+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using Newtonsoft.Json.Serialization;
4+
using System.Reflection;
5+
6+
namespace FooBar.Web.Core.Json
7+
{
8+
//Inspired from http://stackoverflow.com/a/25150302/226589
9+
public class NullToEmptyListResolver : CamelCaseExceptDictionaryKeysResolver
10+
{
11+
protected override IValueProvider CreateMemberValueProvider(MemberInfo member)
12+
{
13+
IValueProvider provider = base.CreateMemberValueProvider(member);
14+
15+
if (member.MemberType == MemberTypes.Property)
16+
{
17+
Type propType = ((PropertyInfo)member).PropertyType;
18+
TypeInfo propTypeInfo = propType.GetTypeInfo();
19+
if (propTypeInfo.IsGenericType &&
20+
propType.GetGenericTypeDefinition() == typeof(List<>))
21+
{
22+
return new EmptyListValueProvider(provider, propType);
23+
}
24+
}
25+
26+
return provider;
27+
}
28+
29+
class EmptyListValueProvider : IValueProvider
30+
{
31+
private IValueProvider innerProvider;
32+
private object defaultValue;
33+
34+
public EmptyListValueProvider(IValueProvider innerProvider, Type listType)
35+
{
36+
this.innerProvider = innerProvider;
37+
defaultValue = Activator.CreateInstance(listType);
38+
}
39+
40+
public void SetValue(object target, object value)
41+
{
42+
innerProvider.SetValue(target, value ?? defaultValue);
43+
}
44+
45+
public object GetValue(object target)
46+
{
47+
return innerProvider.GetValue(target) ?? defaultValue;
48+
}
49+
}
50+
}
51+
}

src/FooBar.Web/FooBar.Web.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
<PackageReference Include="Microsoft.AspNetCore.StaticFiles" Version="1.0.1" />
4242
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="1.0.1" />
4343
<PackageReference Include="Microsoft.VisualStudio.Web.BrowserLink" Version="1.0.1" />
44+
<PackageReference Include="Newtonsoft.Json" Version="9.0.1" />
4445
</ItemGroup>
4546
<ItemGroup>
4647
<DotNetCliToolReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Tools" Version="1.0.0-msbuild3-final" />

0 commit comments

Comments
 (0)