feat(work): 新增若干 List、Array 非标准化 JsonConverter

This commit is contained in:
Fu Diwei
2021-06-03 10:37:46 +08:00
parent f6a5df8687
commit b25eadfaa6
12 changed files with 468 additions and 0 deletions

View File

@@ -0,0 +1,59 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
namespace Newtonsoft.Json.Converters
{
internal class SeparatedByVBarInt32ArrayConverter : JsonConverter<int[]?>
{
private const string SEPARATOR = "|";
public override bool CanRead
{
get { return true; }
}
public override bool CanWrite
{
get { return true; }
}
public override int[]? ReadJson(JsonReader reader, Type objectType, int[]? existingValue, bool hasExistingValue, JsonSerializer serializer)
{
if (reader.TokenType == JsonToken.Null)
{
return null;
}
else if (reader.TokenType == JsonToken.String)
{
string? value = serializer.Deserialize<string>(reader);
if (value == null)
return existingValue;
try
{
return value
.Split(new string[] { SEPARATOR }, StringSplitOptions.RemoveEmptyEntries)
.Select(e => int.Parse(e))
.ToArray();
}
catch (FormatException ex)
{
throw new JsonReaderException(ex.Message, ex);
}
}
throw new JsonReaderException();
}
public override void WriteJson(JsonWriter writer, int[]? value, JsonSerializer serializer)
{
if (value != null)
writer.WriteValue(string.Join(SEPARATOR, value));
else
writer.WriteNull();
}
}
}

View File

@@ -0,0 +1,45 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
namespace Newtonsoft.Json.Converters
{
internal class SeparatedByVBarInt32IListConverter : JsonConverter<IList<int>?>
{
private readonly JsonConverter<List<int>?> _converter = new SeparatedByVBarInt32ListConverter();
public override bool CanRead
{
get { return true; }
}
public override bool CanWrite
{
get { return true; }
}
public override IList<int>? ReadJson(JsonReader reader, Type objectType, IList<int>? existingValue, bool hasExistingValue, JsonSerializer serializer)
{
return _converter.ReadJson(reader, objectType, ConvertIListToList(existingValue), hasExistingValue, serializer);
}
public override void WriteJson(JsonWriter writer, IList<int>? value, JsonSerializer serializer)
{
_converter.WriteJson(writer, ConvertIListToList(value), serializer);
}
private List<int>? ConvertIListToList(IList<int>? src)
{
if (src == null)
return null;
List<int>? dest = src as List<int>;
if (dest != null)
return dest;
return new List<int>(src);
}
}
}

View File

@@ -0,0 +1,33 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
namespace Newtonsoft.Json.Converters
{
internal class SeparatedByVBarInt32ListConverter : JsonConverter<List<int>?>
{
private readonly JsonConverter<int[]?> _converter = new SeparatedByVBarInt32ArrayConverter();
public override bool CanRead
{
get { return true; }
}
public override bool CanWrite
{
get { return true; }
}
public override List<int>? ReadJson(JsonReader reader, Type objectType, List<int>? existingValue, bool hasExistingValue, JsonSerializer serializer)
{
return _converter.ReadJson(reader, objectType, existingValue?.ToArray(), hasExistingValue, serializer)?.ToList();
}
public override void WriteJson(JsonWriter writer, List<int>? value, JsonSerializer serializer)
{
_converter.WriteJson(writer, value?.ToArray(), serializer);
}
}
}

View File

@@ -0,0 +1,49 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
namespace Newtonsoft.Json.Converters
{
internal class SeparatedByVBarStringArrayConverter : JsonConverter<string[]?>
{
private const string SEPARATOR = "|";
public override bool CanRead
{
get { return true; }
}
public override bool CanWrite
{
get { return true; }
}
public override string[]? ReadJson(JsonReader reader, Type objectType, string[]? existingValue, bool hasExistingValue, JsonSerializer serializer)
{
if (reader.TokenType == JsonToken.Null)
{
return null;
}
else if (reader.TokenType == JsonToken.String)
{
string? value = serializer.Deserialize<string>(reader);
if (value == null)
return existingValue;
return value.Split(new string[] { SEPARATOR }, StringSplitOptions.RemoveEmptyEntries);
}
throw new JsonReaderException();
}
public override void WriteJson(JsonWriter writer, string[]? value, JsonSerializer serializer)
{
if (value != null)
writer.WriteValue(string.Join(SEPARATOR, value));
else
writer.WriteNull();
}
}
}

View File

@@ -0,0 +1,45 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
namespace Newtonsoft.Json.Converters
{
internal class SeparatedByVBarStringIListConverter : JsonConverter<IList<string>?>
{
private readonly JsonConverter<List<string>?> _converter = new SeparatedByVBarStringListConverter();
public override bool CanRead
{
get { return true; }
}
public override bool CanWrite
{
get { return true; }
}
public override IList<string>? ReadJson(JsonReader reader, Type objectType, IList<string>? existingValue, bool hasExistingValue, JsonSerializer serializer)
{
return _converter.ReadJson(reader, objectType, ConvertIListToList(existingValue), hasExistingValue, serializer);
}
public override void WriteJson(JsonWriter writer, IList<string>? value, JsonSerializer serializer)
{
_converter.WriteJson(writer, ConvertIListToList(value), serializer);
}
private List<string>? ConvertIListToList(IList<string>? src)
{
if (src == null)
return null;
List<string>? dest = src as List<string>;
if (dest != null)
return dest;
return new List<string>(src);
}
}
}

View File

@@ -0,0 +1,33 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
namespace Newtonsoft.Json.Converters
{
internal class SeparatedByVBarStringListConverter : JsonConverter<List<string>?>
{
private readonly JsonConverter<string[]?> _converter = new SeparatedByVBarStringArrayConverter();
public override bool CanRead
{
get { return true; }
}
public override bool CanWrite
{
get { return true; }
}
public override List<string>? ReadJson(JsonReader reader, Type objectType, List<string>? existingValue, bool hasExistingValue, JsonSerializer serializer)
{
return _converter.ReadJson(reader, objectType, existingValue?.ToArray(), hasExistingValue, serializer)?.ToList();
}
public override void WriteJson(JsonWriter writer, List<string>? value, JsonSerializer serializer)
{
_converter.WriteJson(writer, value?.ToArray(), serializer);
}
}
}

View File

@@ -0,0 +1,49 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.Json;
using System.Text.Json.Serialization;
namespace System.Text.Json.Converters
{
internal class SeparatedByVBarInt32ArrayConverter : JsonConverter<int[]?>
{
private const string SEPARATOR = "|";
public override int[]? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
if (reader.TokenType == JsonTokenType.Null)
{
return null;
}
else if (reader.TokenType == JsonTokenType.String)
{
string? value = reader.GetString();
if (value == null)
return null;
try
{
return value
.Split(new string[] { SEPARATOR }, StringSplitOptions.RemoveEmptyEntries)
.Select(e => int.Parse(e))
.ToArray();
}
catch (FormatException ex)
{
throw new JsonException(ex.Message, ex);
}
}
throw new JsonException();
}
public override void Write(Utf8JsonWriter writer, int[]? value, JsonSerializerOptions options)
{
if (value != null)
writer.WriteStringValue(string.Join(SEPARATOR, value));
else
writer.WriteNullValue();
}
}
}

View File

@@ -0,0 +1,35 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.Json;
using System.Text.Json.Serialization;
namespace System.Text.Json.Converters
{
internal class SeparatedByVBarInt32IListConverter : JsonConverter<IList<int>?>
{
private readonly JsonConverter<List<int>?> _converter = new SeparatedByVBarInt32ListConverter();
public override IList<int>? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
return _converter.Read(ref reader, typeToConvert, options);
}
public override void Write(Utf8JsonWriter writer, IList<int>? value, JsonSerializerOptions options)
{
_converter.Write(writer, ConvertIListToList(value), options);
}
private List<int>? ConvertIListToList(IList<int>? src)
{
if (src == null)
return null;
List<int>? dest = src as List<int>;
if (dest != null)
return dest;
return new List<int>(src);
}
}
}

View File

@@ -0,0 +1,23 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.Json;
using System.Text.Json.Serialization;
namespace System.Text.Json.Converters
{
internal class SeparatedByVBarInt32ListConverter : JsonConverter<List<int>?>
{
private readonly JsonConverter<int[]?> _converter = new SeparatedByVBarInt32ArrayConverter();
public override List<int>? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
return _converter.Read(ref reader, typeToConvert, options)?.ToList();
}
public override void Write(Utf8JsonWriter writer, List<int>? value, JsonSerializerOptions options)
{
_converter.Write(writer, value?.ToArray(), options);
}
}
}

View File

@@ -0,0 +1,39 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.Json;
using System.Text.Json.Serialization;
namespace System.Text.Json.Converters
{
internal class SeparatedByVBarStringArrayConverter : JsonConverter<string[]?>
{
private const string SEPARATOR = "|";
public override string[]? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
if (reader.TokenType == JsonTokenType.Null)
{
return null;
}
else if (reader.TokenType == JsonTokenType.String)
{
string? value = reader.GetString();
if (value == null)
return null;
return value.Split(new string[] { SEPARATOR }, StringSplitOptions.RemoveEmptyEntries);
}
throw new JsonException();
}
public override void Write(Utf8JsonWriter writer, string[]? value, JsonSerializerOptions options)
{
if (value != null)
writer.WriteStringValue(string.Join(SEPARATOR, value));
else
writer.WriteNullValue();
}
}
}

View File

@@ -0,0 +1,35 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.Json;
using System.Text.Json.Serialization;
namespace System.Text.Json.Converters
{
internal class SeparatedByVBarStringIListConverter : JsonConverter<IList<string>?>
{
private readonly JsonConverter<List<string>?> _converter = new SeparatedByVBarStringListConverter();
public override IList<string>? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
return _converter.Read(ref reader, typeToConvert, options);
}
public override void Write(Utf8JsonWriter writer, IList<string>? value, JsonSerializerOptions options)
{
_converter.Write(writer, ConvertIListToList(value), options);
}
private List<string>? ConvertIListToList(IList<string>? src)
{
if (src == null)
return null;
List<string>? dest = src as List<string>;
if (dest != null)
return dest;
return new List<string>(src);
}
}
}

View File

@@ -0,0 +1,23 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.Json;
using System.Text.Json.Serialization;
namespace System.Text.Json.Converters
{
internal class SeparatedByVBarStringListConverter : JsonConverter<List<string>?>
{
private readonly JsonConverter<string[]?> _converter = new SeparatedByVBarStringArrayConverter();
public override List<string>? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
return _converter.Read(ref reader, typeToConvert, options)?.ToList();
}
public override void Write(Utf8JsonWriter writer, List<string>? value, JsonSerializerOptions options)
{
_converter.Write(writer, value?.ToArray(), options);
}
}
}