-
Notifications
You must be signed in to change notification settings - Fork 64
/
EcsJsonConverterBase.cs
136 lines (115 loc) · 4.44 KB
/
EcsJsonConverterBase.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
// Licensed to Elasticsearch B.V under one or more agreements.
// Elasticsearch B.V licenses this file to you under the Apache 2.0 License.
// See the LICENSE file in the project root for more information
using System;
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Text.Json.Serialization.Metadata;
namespace Elastic.CommonSchema.Serialization
{
/// <summary> A base implementation for dedicated Ecs Fieldset json converters </summary>
public abstract class EcsJsonConverterBase<T> : JsonConverter<T>
{
/// <summary></summary>
protected static bool ReadDateTime(ref Utf8JsonReader reader, ref DateTimeOffset? dateTime)
{
if (reader.TokenType == JsonTokenType.Null)
{
dateTime = null;
return true;
}
dateTime = EcsJsonConfiguration.DateTimeOffsetConverter.Read(ref reader, typeof(DateTimeOffset), EcsJsonConfiguration.SerializerOptions);
return true;
}
/// <summary></summary>
// ReSharper disable once RedundantAssignment
protected static bool ReadString(ref Utf8JsonReader reader, ref string? stringProp)
{
stringProp = reader.GetString();
return true;
}
/// <summary></summary>
protected static void WriteProp<TValue>(Utf8JsonWriter writer, string key, TValue value)
{
if (value == null) return;
var options = EcsJsonConfiguration.SerializerOptions;
writer.WritePropertyName(key);
// Attempt to use existing converter first before re-entering through JsonSerializer.Serialize().
// The default converter for object does not support writing.
JsonSerializer.Serialize<TValue>(writer, value, options);
}
/// <summary></summary>
protected static void WriteProp<TValue>(Utf8JsonWriter writer, string key, TValue value, JsonTypeInfo<TValue> typeInfo)
{
if (value == null) return;
writer.WritePropertyName(key);
var type = value.GetType();
//To support user supplied subtypes
if (type != typeof(TValue))
JsonSerializer.Serialize(writer, value, type, EcsJsonConfiguration.SerializerOptions);
else JsonSerializer.Serialize(writer, value, typeInfo);
}
internal static object? ReadPropDeserialize(ref Utf8JsonReader reader, Type type)
{
if (reader.TokenType == JsonTokenType.Null) return null;
var options = EcsJsonConfiguration.SerializerOptions;
return JsonSerializer.Deserialize(ref reader, type, options);
}
// ReSharper disable once UnusedParameter.Local (key is used for readability)
private static long? ReadPropLong(ref Utf8JsonReader reader, string key)
{
if (reader.TokenType == JsonTokenType.PropertyName) reader.Read();
return reader.TokenType != JsonTokenType.Number ? null : reader.TryGetInt64(out var l) ? l : null;
}
/// <summary></summary>
protected static bool ReadPropLong(ref Utf8JsonReader reader, string key, T b, Action<T, long?> set)
{
set(b, ReadPropLong(ref reader, key));
return true;
}
/// <summary></summary>
// ReSharper disable once UnusedParameter.Local (key is used for readability)
private static string? ReadPropString(ref Utf8JsonReader reader, string key)
{
if (reader.TokenType == JsonTokenType.PropertyName) reader.Read();
return reader.TokenType != JsonTokenType.String ? null : reader.GetString();
}
/// <summary></summary>
protected static bool ReadPropString(ref Utf8JsonReader reader, string key, T b, Action<T, string?> set)
{
set(b, ReadPropString(ref reader, key));
return true;
}
/// <summary></summary>
// ReSharper disable once UnusedParameter.Local (key is used for readability)
private static TValue? ReadProp<TValue>(ref Utf8JsonReader reader, string key) where TValue : class
{
if (reader.TokenType == JsonTokenType.Null) return null;
var options = EcsJsonConfiguration.SerializerOptions;
return JsonSerializer.Deserialize<TValue>(ref reader, options);
}
/// <summary></summary>
protected static bool ReadProp<TValue>(ref Utf8JsonReader reader, string key, T b, Action<T, TValue?> set)
where TValue : class
{
set(b, ReadProp<TValue>(ref reader, key));
return true;
}
/// <summary></summary>
// ReSharper disable once UnusedParameter.Local (key is used for readability)
protected static bool ReadProp<TValue>(ref Utf8JsonReader reader, string key, JsonTypeInfo<TValue> typeInfo, T b, Action<T, TValue?> set)
where TValue : class
{
try
{
var value = JsonSerializer.Deserialize(ref reader, typeInfo);
set(b, value);
}
catch (Exception e)
{
throw new Exception(key, e);
}
return true;
}
}
}