Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Added Newtonsoft.Json nuget package, and some classes and helpers for…
… adapting the serialization of objects to json.
- Loading branch information
Showing
9 changed files
with
147 additions
and
0 deletions.
There are no files selected for viewing
15 changes: 15 additions & 0 deletions
15
src/FooBar.Web/Core/Json/CamelCaseExceptDictionaryKeysResolver.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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. | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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"; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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"; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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"; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters