This repository has been archived by the owner on Feb 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10 from ejball/master
Miscellaneous improvements.
- Loading branch information
Showing
23 changed files
with
625 additions
and
271 deletions.
There are no files selected for viewing
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
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 |
---|---|---|
@@ -1,5 +1,3 @@ | ||
#pragma warning disable 1591 | ||
|
||
using System; | ||
using Newtonsoft.Json; | ||
using Newtonsoft.Json.Converters; | ||
|
48 changes: 0 additions & 48 deletions
48
src/Faithlife.Json/Converters/DictionaryKeysAreNotPropertyNamesJsonConverter.cs
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,5 +1,3 @@ | ||
#pragma warning disable 1591 | ||
|
||
using System; | ||
using Faithlife.Utility; | ||
using Newtonsoft.Json; | ||
|
2 changes: 0 additions & 2 deletions
2
src/Faithlife.Json/Converters/GuidLowerNoDashJsonConverter.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 |
---|---|---|
@@ -1,5 +1,3 @@ | ||
#pragma warning disable 1591 | ||
|
||
using System; | ||
using Faithlife.Utility; | ||
using Newtonsoft.Json; | ||
|
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 |
---|---|---|
@@ -1,5 +1,3 @@ | ||
#pragma warning disable 1591 | ||
|
||
using System; | ||
using System.Globalization; | ||
using Newtonsoft.Json; | ||
|
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
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,53 @@ | ||
using System; | ||
using System.Linq; | ||
using System.Reflection; | ||
using Faithlife.Utility; | ||
using Newtonsoft.Json; | ||
|
||
namespace Faithlife.Json.Converters | ||
{ | ||
/// <summary> | ||
/// Supports JSON conversion of Optional{T}. | ||
/// </summary> | ||
public class OptionalJsonConverter : JsonConverter | ||
{ | ||
public override bool CanConvert(Type objectType) | ||
{ | ||
return objectType.IsGenericType() && objectType.GetGenericTypeDefinition() == typeof(Optional<>); | ||
} | ||
|
||
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) | ||
{ | ||
// make sure it has a value; optional instances without a value must be ignored | ||
IOptional optional = (IOptional) value; | ||
if (!optional.HasValue) | ||
{ | ||
string optionalValueTypeName = optional.GetType().GenericTypeArguments.Single().Name; | ||
throw new InvalidOperationException(("Optional<{0}>.HasValue is false. " + | ||
"Optional properties should include these attributes: " + | ||
"[JsonProperty(DefaultValueHandling = DefaultValueHandling.Ignore, NullValueHandling = NullValueHandling.Include), DefaultValueDefault(typeof(Optional<{0}>))]") | ||
.FormatInvariant(optionalValueTypeName)); | ||
} | ||
|
||
// serialize value | ||
serializer.Serialize(writer, optional.Value); | ||
} | ||
|
||
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) | ||
{ | ||
// get T of Optional<T> | ||
Type optionalValueType = objectType.GenericTypeArguments.Single(); | ||
|
||
// deserialize using T | ||
object optionalValue = serializer.Deserialize(reader, optionalValueType); | ||
|
||
// call Optional<T>(T value) constructor | ||
ConstructorInfo constructorInfo = GetConstructor(objectType, new[] { optionalValueType }); | ||
Verify.IsNotNull(constructorInfo); | ||
return constructorInfo.Invoke(new[] { optionalValue }); | ||
} | ||
|
||
private static ConstructorInfo GetConstructor(Type type, Type[] types) | ||
=> type.GetTypeInfo().DeclaredConstructors.FirstOrDefault(x => x.IsPublic && EnumerableUtility.AreEqual(x.GetParameters().Select(p => p.ParameterType), types)); | ||
} | ||
} |
38 changes: 0 additions & 38 deletions
38
src/Faithlife.Json/Converters/ReadOnlyDictionaryJsonConverter.cs
This file was deleted.
Oops, something went wrong.
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,20 @@ | ||
using System; | ||
using System.ComponentModel; | ||
|
||
namespace Faithlife.Json | ||
{ | ||
/// <summary> | ||
/// Sets the <c>DefaultValue</c> to <c>new T()</c> for the specified type. | ||
/// </summary> | ||
public sealed class DefaultValueDefaultAttribute : DefaultValueAttribute | ||
{ | ||
/// <summary> | ||
/// Initializes a new instance of the <see cref="DefaultValueDefaultAttribute"/> class. | ||
/// </summary> | ||
/// <param name="type">The type.</param> | ||
public DefaultValueDefaultAttribute(Type type) | ||
: base(Activator.CreateInstance(type)) | ||
{ | ||
} | ||
} | ||
} |
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
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
Oops, something went wrong.