-
-
Notifications
You must be signed in to change notification settings - Fork 198
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
39aab8c
commit 34f96f1
Showing
2 changed files
with
83 additions
and
0 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 |
---|---|---|
@@ -0,0 +1,82 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Dynamic; | ||
using System.Text.Json; | ||
using System.Text.Json.Serialization; | ||
|
||
namespace YesSql.Serialization | ||
{ | ||
public class DynamicJsonConverter : JsonConverter<object> | ||
{ | ||
public static readonly DynamicJsonConverter Instance = new(); | ||
|
||
public override object Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) | ||
{ | ||
switch (reader.TokenType) | ||
{ | ||
case JsonTokenType.Null: | ||
return null; | ||
case JsonTokenType.False: | ||
return false; | ||
case JsonTokenType.True: | ||
return true; | ||
case JsonTokenType.String: | ||
return reader.GetString(); | ||
case JsonTokenType.Number: | ||
{ | ||
if (reader.TryGetInt32(out var i)) | ||
return i; | ||
if (reader.TryGetInt64(out var l)) | ||
return l; | ||
// BigInteger could be added here. | ||
if (reader.TryGetDouble(out var d)) | ||
return d; | ||
|
||
throw new JsonException("Cannot parse number"); | ||
} | ||
case JsonTokenType.StartArray: | ||
{ | ||
var list = new List<object>(); | ||
while (reader.Read()) | ||
{ | ||
switch (reader.TokenType) | ||
{ | ||
default: | ||
list.Add(Read(ref reader, typeof(object), options)); | ||
break; | ||
case JsonTokenType.EndArray: | ||
return list; | ||
} | ||
} | ||
throw new JsonException(); | ||
} | ||
case JsonTokenType.StartObject: | ||
IDictionary<string, object> dict = new ExpandoObject(); | ||
while (reader.Read()) | ||
{ | ||
switch (reader.TokenType) | ||
{ | ||
case JsonTokenType.EndObject: | ||
return dict; | ||
case JsonTokenType.PropertyName: | ||
var key = reader.GetString(); | ||
reader.Read(); | ||
dict[key] = Read(ref reader, typeof(object), options); | ||
break; | ||
default: | ||
throw new JsonException(); | ||
} | ||
} | ||
throw new JsonException(); | ||
default: | ||
throw new JsonException(string.Format("Unknown token {0}", reader.TokenType)); | ||
} | ||
} | ||
|
||
public override void Write( | ||
Utf8JsonWriter writer, | ||
object objectToWrite, | ||
JsonSerializerOptions options) => | ||
JsonSerializer.Serialize(writer, objectToWrite, objectToWrite.GetType(), options); | ||
} | ||
} |