-
Notifications
You must be signed in to change notification settings - Fork 72
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
261ff80
commit e1f0b85
Showing
10 changed files
with
367 additions
and
50 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
42 changes: 42 additions & 0 deletions
42
csharp/Squidex.ClientLibrary/Squidex.ClientLibrary/JsonNull.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,42 @@ | ||
// ========================================================================== | ||
// Squidex Headless CMS | ||
// ========================================================================== | ||
// Copyright (c) Squidex UG (haftungsbeschraenkt) | ||
// All rights reserved. Licensed under the MIT license. | ||
// ========================================================================== | ||
|
||
#pragma warning disable SA1313 // Parameter names should begin with lower-case letter | ||
|
||
namespace Squidex.ClientLibrary | ||
{ | ||
/// <summary> | ||
/// A value that can be null. | ||
/// </summary> | ||
/// <typeparam name="T">The actual value.</typeparam> | ||
public record struct JsonNull<T>(T Value) | ||
{ | ||
/// <summary> | ||
/// Operator to compare the value to the wrapper. | ||
/// </summary> | ||
/// <param name="value">The value to convert.</param> | ||
public static implicit operator JsonNull<T>(T value) | ||
{ | ||
return new JsonNull<T>(value); | ||
} | ||
|
||
/// <summary> | ||
/// Operator to compare the wrapper to the actual value. | ||
/// </summary> | ||
/// <param name="wrapper">The wrapper to convert.</param> | ||
public static implicit operator T(JsonNull<T> wrapper) | ||
{ | ||
return wrapper.Value; | ||
} | ||
|
||
/// <inheritdoc/> | ||
public override readonly string ToString() | ||
{ | ||
return Value?.ToString() ?? string.Empty; | ||
} | ||
} | ||
} |
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
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
74 changes: 74 additions & 0 deletions
74
csharp/Squidex.ClientLibrary/Squidex.ClientLibrary/Utils/JsonNullContractResolver.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,74 @@ | ||
// ========================================================================== | ||
// Squidex Headless CMS | ||
// ========================================================================== | ||
// Copyright (c) Squidex UG (haftungsbeschraenkt) | ||
// All rights reserved. Licensed under the MIT license. | ||
// ========================================================================== | ||
|
||
using Newtonsoft.Json; | ||
using Newtonsoft.Json.Serialization; | ||
using System.Reflection; | ||
|
||
namespace Squidex.ClientLibrary.Utils | ||
{ | ||
internal class JsonNullContractResolver : CamelCasePropertyNamesContractResolver | ||
{ | ||
/* | ||
protected override JsonContract CreateContract(Type objectType) | ||
{ | ||
var contract = base.CreateContract(objectType); | ||
if (contract.UnderlyingType.IsGenericType && contract.UnderlyingType.GetGenericTypeDefinition() == typeof(JsonNull<>)) | ||
{ | ||
var converterType = typeof(JsonNullConverter<>).MakeGenericType(contract.UnderlyingType.GetGenericArguments()[0]); | ||
var converterObj = Activator.CreateInstance(converterType) as JsonConverter; | ||
contract.Converter = converterObj; | ||
} | ||
return contract; | ||
} | ||
*/ | ||
|
||
protected override JsonProperty CreateProperty(MemberInfo member, MemberSerialization memberSerialization) | ||
{ | ||
var property = base.CreateProperty(member, memberSerialization); | ||
|
||
var objectType = (member as PropertyInfo)?.PropertyType; | ||
|
||
if (objectType?.IsGenericType == true && objectType.GetGenericTypeDefinition() == typeof(JsonNull<>)) | ||
{ | ||
var baseType = typeof(JsonNullConverter<>); | ||
|
||
if (property.Converter is InvariantConverter) | ||
{ | ||
baseType = typeof(JsonNullInvariantConverter<>); | ||
} | ||
else if (property.Converter is InvariantWriteConverter) | ||
{ | ||
baseType = typeof(JsonNullInvariantWriteConverter<>); | ||
} | ||
|
||
var converterType = baseType.MakeGenericType(objectType.GetGenericArguments()[0]); | ||
var converterObj = Activator.CreateInstance(converterType) as JsonConverter; | ||
|
||
property.Converter = converterObj; | ||
} | ||
|
||
return property; | ||
} | ||
|
||
protected override JsonConverter? ResolveContractConverter(Type objectType) | ||
{ | ||
if (objectType.IsGenericType && objectType.GetGenericTypeDefinition() == typeof(JsonNull<>)) | ||
{ | ||
var converterType = typeof(JsonNullConverter<>).MakeGenericType(objectType.GetGenericArguments()[0]); | ||
var converterObj = Activator.CreateInstance(converterType) as JsonConverter; | ||
|
||
return converterObj; | ||
} | ||
|
||
return base.ResolveContractConverter(objectType); | ||
} | ||
} | ||
} |
Oops, something went wrong.