Skip to content

Commit

Permalink
Create DynamicJsonConverter
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastienros committed Oct 21, 2023
1 parent 39aab8c commit 34f96f1
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/YesSql.Core/Serialization/DefaultContentSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public DefaultContentSerializer()
_options = new();
_options.DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull;
_options.Converters.Add(UtcDateTimeJsonConverter.Instance);
_options.Converters.Add(DynamicJsonConverter.Instance);
}

public DefaultContentSerializer(JsonSerializerOptions options)
Expand Down
82 changes: 82 additions & 0 deletions src/YesSql.Core/Serialization/DynamicJsonConverter.cs
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);
}
}

0 comments on commit 34f96f1

Please sign in to comment.