adding
This commit is contained in:
+25
@@ -0,0 +1,25 @@
|
||||
using System;
|
||||
using System.Globalization;
|
||||
using System.Numerics;
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace LCU.Library.Internal.DocumentModels.Converter;
|
||||
|
||||
public class BigIntegerConverter : JsonConverter<BigInteger>
|
||||
{
|
||||
public override BigInteger Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
|
||||
{
|
||||
if (reader.TokenType != JsonTokenType.Number)
|
||||
throw new JsonException($"Found token {reader.TokenType} but expected token {JsonTokenType.Number}");
|
||||
using var doc = JsonDocument.ParseValue(ref reader);
|
||||
return BigInteger.Parse(doc.RootElement.GetRawText(), NumberFormatInfo.InvariantInfo);
|
||||
}
|
||||
|
||||
public override void Write(Utf8JsonWriter writer, BigInteger value, JsonSerializerOptions options)
|
||||
{
|
||||
var s = value.ToString(NumberFormatInfo.InvariantInfo);
|
||||
using var doc = JsonDocument.Parse(s);
|
||||
doc.WriteTo(writer);
|
||||
}
|
||||
}
|
||||
+33
@@ -0,0 +1,33 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Dynamic;
|
||||
using Newtonsoft.Json;
|
||||
using Newtonsoft.Json.Linq;
|
||||
|
||||
namespace LCU.Library.Internal.DocumentModels.Converter;
|
||||
|
||||
public class DynamicObjectJsonConverter : Newtonsoft.Json.JsonConverter
|
||||
{
|
||||
public override void WriteJson(JsonWriter writer, dynamic value, JsonSerializer serializer)
|
||||
{
|
||||
serializer.Serialize(writer, value);
|
||||
}
|
||||
|
||||
public override dynamic ReadJson(JsonReader reader, Type objectType, dynamic existingValue, JsonSerializer serializer)
|
||||
{
|
||||
if (reader.TokenType != JsonToken.StartObject) return reader.Value;
|
||||
|
||||
var node = JObject.Load(reader);
|
||||
|
||||
var definition = new ExpandoObject();
|
||||
var definitionPropertyBag = definition as IDictionary<string, object>;
|
||||
foreach (var property in node.Properties())
|
||||
{
|
||||
definitionPropertyBag[property.Name] = property.Value.ToObject<object>();
|
||||
}
|
||||
|
||||
return JsonConvert.DeserializeAnonymousType(node.ToString(), definition);
|
||||
}
|
||||
|
||||
public override bool CanConvert(Type _) => true;
|
||||
}
|
||||
+14
@@ -0,0 +1,14 @@
|
||||
using System;
|
||||
|
||||
namespace LCU.Library.Internal.DocumentModels.Converter;
|
||||
|
||||
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Interface)]
|
||||
public class JsonKnownTypesAttribute : Attribute
|
||||
{
|
||||
public Type[] Subclasses { get; }
|
||||
|
||||
public JsonKnownTypesAttribute(params Type[] knownTypes)
|
||||
{
|
||||
Subclasses = knownTypes;
|
||||
}
|
||||
}
|
||||
+48
@@ -0,0 +1,48 @@
|
||||
using System;
|
||||
using System.Text.Json;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.AspNetCore.Mvc.ModelBinding;
|
||||
|
||||
namespace LCU.Library.Internal.DocumentModels.Converter
|
||||
{
|
||||
public class FromJsonQueryAttribute : ModelBinderAttribute
|
||||
{
|
||||
public FromJsonQueryAttribute()
|
||||
{
|
||||
BinderType = typeof(JsonQueryBinder);
|
||||
}
|
||||
}
|
||||
|
||||
public class JsonQueryBinder : IModelBinder
|
||||
{
|
||||
public JsonQueryBinder()
|
||||
{
|
||||
}
|
||||
|
||||
public Task BindModelAsync(ModelBindingContext bindingContext)
|
||||
{
|
||||
var value = bindingContext.ValueProvider.GetValue(bindingContext.FieldName).FirstValue;
|
||||
if (value == null)
|
||||
{
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
var parsed = JsonSerializer.Deserialize(
|
||||
value,
|
||||
bindingContext.ModelType,
|
||||
new JsonSerializerOptions(JsonSerializerDefaults.Web)
|
||||
);
|
||||
bindingContext.Result = ModelBindingResult.Success(parsed);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
bindingContext.Result = ModelBindingResult.Failed();
|
||||
}
|
||||
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
}
|
||||
}
|
||||
+107
@@ -0,0 +1,107 @@
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using System.Text.Json;
|
||||
using Newtonsoft.Json;
|
||||
using Newtonsoft.Json.Linq;
|
||||
using JsonSerializer = System.Text.Json.JsonSerializer;
|
||||
|
||||
namespace LCU.Library.Internal.DocumentModels.Converter;
|
||||
|
||||
internal static class PolymorphicJsonConverter
|
||||
{
|
||||
public const string TypeProperty = "_t";
|
||||
|
||||
public static Exception NotSupported<T>() => new NotSupportedException($"{typeof(T).Name} must specify known deserialization types");
|
||||
|
||||
public static bool IsSerializationSupportedFor<T>(Type actualType) => typeof(T).GetCustomAttribute<JsonKnownTypesAttribute>() is { } knownTypes
|
||||
&& knownTypes.Subclasses.Contains(actualType);
|
||||
|
||||
public static Type DeserializationTypeFor<T>(string actualType) => typeof(T)
|
||||
.GetCustomAttribute<JsonKnownTypesAttribute>()
|
||||
.Subclasses
|
||||
.Single(t => t.Name == actualType);
|
||||
}
|
||||
|
||||
public class SystemPolymorphicJsonConverter<T> : System.Text.Json.Serialization.JsonConverter<T>
|
||||
{
|
||||
public override T Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
|
||||
{
|
||||
var document = JsonDocument.ParseValue(ref reader);
|
||||
var type = document.RootElement.GetProperty(PolymorphicJsonConverter.TypeProperty).GetString();
|
||||
var deserializationType = PolymorphicJsonConverter.DeserializationTypeFor<T>(type);
|
||||
|
||||
return (T)document.Deserialize(deserializationType, options);
|
||||
}
|
||||
|
||||
public override void Write(Utf8JsonWriter writer, T value, JsonSerializerOptions options)
|
||||
{
|
||||
var actualType = value.GetType();
|
||||
if (!PolymorphicJsonConverter.IsSerializationSupportedFor<T>(actualType))
|
||||
{
|
||||
throw PolymorphicJsonConverter.NotSupported<T>();
|
||||
}
|
||||
|
||||
writer.WriteStartObject();
|
||||
writer.WriteString(PolymorphicJsonConverter.TypeProperty, actualType.Name);
|
||||
|
||||
var actualValue = JsonSerializer.Serialize(value, actualType, options);
|
||||
writer.WriteRawValue(actualValue.Substring(1, actualValue.Length - 2), true);
|
||||
|
||||
writer.WriteEndObject();
|
||||
}
|
||||
}
|
||||
|
||||
public class NewtonsoftPolymorphicJsonConverter<T> : Newtonsoft.Json.JsonConverter<T>
|
||||
{
|
||||
public override bool CanRead => _canRead;
|
||||
private bool _canRead = true;
|
||||
|
||||
public override T ReadJson(JsonReader reader, Type objectType, T existingValue, bool hasExistingValue, Newtonsoft.Json.JsonSerializer serializer)
|
||||
{
|
||||
var document = JObject.Load(reader);
|
||||
var type = document.GetValue(PolymorphicJsonConverter.TypeProperty)!.Value<string>();
|
||||
var deserializationType = PolymorphicJsonConverter.DeserializationTypeFor<T>(type);
|
||||
|
||||
T result;
|
||||
lock (this)
|
||||
{
|
||||
_canRead = false;
|
||||
result = (T)JsonConvert.DeserializeObject(document.ToString(), deserializationType);
|
||||
_canRead = true;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
public override bool CanWrite => _canWrite;
|
||||
private bool _canWrite = true;
|
||||
|
||||
public override void WriteJson(JsonWriter writer, T value, Newtonsoft.Json.JsonSerializer serializer)
|
||||
{
|
||||
var actualType = value.GetType();
|
||||
if (!PolymorphicJsonConverter.IsSerializationSupportedFor<T>(actualType))
|
||||
{
|
||||
throw PolymorphicJsonConverter.NotSupported<T>();
|
||||
}
|
||||
|
||||
writer.WriteStartObject();
|
||||
writer.WritePropertyName(PolymorphicJsonConverter.TypeProperty);
|
||||
writer.WriteValue(actualType.Name);
|
||||
|
||||
JObject jValue;
|
||||
lock (this)
|
||||
{
|
||||
_canWrite = false;
|
||||
jValue = JObject.FromObject(value);
|
||||
_canWrite = true;
|
||||
}
|
||||
|
||||
foreach (var property in jValue.Properties())
|
||||
{
|
||||
property.WriteTo(writer);
|
||||
}
|
||||
|
||||
writer.WriteEndObject();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user