Permalink
Browse files

Added Newtonsoft.Json nuget package, and some classes and helpers for…

… adapting the serialization of objects to json.
  • Loading branch information...
deap82 committed May 20, 2017
1 parent 8c718e4 commit f477461c41ea73141e54caf596545737ffe4b486
@@ -0,0 +1,15 @@
using System;
using Newtonsoft.Json.Serialization;
namespace FooBar.Web.Core.Json
{
public class CamelCaseExceptDictionaryKeysResolver : CamelCasePropertyNamesContractResolver
{
protected override JsonDictionaryContract CreateDictionaryContract(Type objectType)
{
JsonDictionaryContract contract = base.CreateDictionaryContract(objectType);
contract.DictionaryKeyResolver = propertyName => propertyName;
return contract;
}
}
}
@@ -0,0 +1,10 @@
namespace FooBar.Web.Core.Json
{
public class FooBarContractResolver : NullToEmptyListResolver
{
//Marker class
//As Json.net don't accept multiple contract resolvers we need to use inheritance.
//For additional needs of custom contract resolvers, add your new one to the inheritance chain by changing the inheritance of this class
//and let your new resolver inherit from the one currently inherited here.
}
}
@@ -0,0 +1,14 @@
using System.Globalization;
using Newtonsoft.Json.Converters;
namespace FooBar.Web.Core.Json
{
public class JsonDateConverter : IsoDateTimeConverter
{
public JsonDateConverter()
{
Culture = new CultureInfo("sv-SE"); //Force ISO 8601
DateTimeFormat = "d";
}
}
}
@@ -0,0 +1,14 @@
using System.Globalization;
using Newtonsoft.Json.Converters;
namespace FooBar.Web.Core.Json
{
public class JsonDateTimeConverter : IsoDateTimeConverter
{
public JsonDateTimeConverter()
{
Culture = new CultureInfo("sv-SE"); //Force ISO 8601
DateTimeFormat = "yyyy'-'MM'-'dd' 'HH':'mm";
}
}
}
@@ -0,0 +1,15 @@
using Newtonsoft.Json;
namespace FooBar.Web.Core.Json
{
public static class JsonHelpers
{
public static JsonSerializerSettings CreateJsonSerializerSettings()
{
var settings = new JsonSerializerSettings { ContractResolver = new FooBarContractResolver() };
settings.Converters.Add(new JsonDateConverter());
return settings;
}
}
}
@@ -0,0 +1,13 @@
using Newtonsoft.Json;
namespace FooBar.Web.Core.Json
{
public static class JsonObjectExtensions
{
public static string ToJsonCamelCase(this object obj)
{
JsonSerializerSettings settings = JsonHelpers.CreateJsonSerializerSettings();
return JsonConvert.SerializeObject(obj, Formatting.None, settings);
}
}
}
@@ -0,0 +1,14 @@
using System.Globalization;
using Newtonsoft.Json.Converters;
namespace FooBar.Web.Core.Json
{
public class JsonTimeConverter : IsoDateTimeConverter
{
public JsonTimeConverter()
{
Culture = new CultureInfo("sv-SE"); //Force ISO 8601
DateTimeFormat = "t";
}
}
}
@@ -0,0 +1,51 @@
using System;
using System.Collections.Generic;
using Newtonsoft.Json.Serialization;
using System.Reflection;
namespace FooBar.Web.Core.Json
{
//Inspired from http://stackoverflow.com/a/25150302/226589
public class NullToEmptyListResolver : CamelCaseExceptDictionaryKeysResolver
{
protected override IValueProvider CreateMemberValueProvider(MemberInfo member)
{
IValueProvider provider = base.CreateMemberValueProvider(member);
if (member.MemberType == MemberTypes.Property)
{
Type propType = ((PropertyInfo)member).PropertyType;
TypeInfo propTypeInfo = propType.GetTypeInfo();
if (propTypeInfo.IsGenericType &&
propType.GetGenericTypeDefinition() == typeof(List<>))
{
return new EmptyListValueProvider(provider, propType);
}
}
return provider;
}
class EmptyListValueProvider : IValueProvider
{
private IValueProvider innerProvider;
private object defaultValue;
public EmptyListValueProvider(IValueProvider innerProvider, Type listType)
{
this.innerProvider = innerProvider;
defaultValue = Activator.CreateInstance(listType);
}
public void SetValue(object target, object value)
{
innerProvider.SetValue(target, value ?? defaultValue);
}
public object GetValue(object target)
{
return innerProvider.GetValue(target) ?? defaultValue;
}
}
}
}
@@ -41,6 +41,7 @@
<PackageReference Include="Microsoft.AspNetCore.StaticFiles" Version="1.0.1" />
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="1.0.1" />
<PackageReference Include="Microsoft.VisualStudio.Web.BrowserLink" Version="1.0.1" />
<PackageReference Include="Newtonsoft.Json" Version="9.0.1" />
</ItemGroup>
<ItemGroup>
<DotNetCliToolReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Tools" Version="1.0.0-msbuild3-final" />

0 comments on commit f477461

Please sign in to comment.