-
-
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.
Browse files
Browse the repository at this point in the history
Co-authored-by: Sébastien Ros <[email protected]>
- Loading branch information
1 parent
e8bafa2
commit a906437
Showing
11 changed files
with
154 additions
and
43 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
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,34 @@ | ||
using System; | ||
using System.Text.Json; | ||
using System.Text.Json.Serialization; | ||
|
||
namespace YesSql.Serialization | ||
{ | ||
public class DefaultContentSerializer : IContentSerializer | ||
{ | ||
private readonly JsonSerializerOptions _options; | ||
|
||
public DefaultContentSerializer() | ||
{ | ||
_options = new(); | ||
_options.DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull; | ||
_options.Converters.Add(UtcDateTimeJsonConverter.Instance); | ||
_options.Converters.Add(DynamicJsonConverter.Instance); | ||
} | ||
|
||
public DefaultContentSerializer(JsonSerializerOptions options) | ||
{ | ||
_options = options; | ||
} | ||
|
||
public object Deserialize(string content, Type type) | ||
{ | ||
return JsonSerializer.Deserialize(content, type, _options); | ||
} | ||
|
||
public string Serialize(object item) | ||
{ | ||
return JsonSerializer.Serialize(item, _options); | ||
} | ||
} | ||
} |
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); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
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,29 @@ | ||
using System; | ||
using System.Diagnostics; | ||
using System.Text.Json; | ||
using System.Text.Json.Serialization; | ||
|
||
namespace YesSql.Serialization | ||
{ | ||
public class UtcDateTimeJsonConverter : JsonConverter<DateTime> | ||
{ | ||
public static readonly UtcDateTimeJsonConverter Instance = new(); | ||
|
||
public override DateTime Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) | ||
{ | ||
Debug.Assert(typeToConvert == typeof(DateTime)); | ||
|
||
if (!reader.TryGetDateTime(out DateTime value)) | ||
{ | ||
value = DateTime.UtcNow; | ||
} | ||
|
||
return value; | ||
} | ||
|
||
public override void Write(Utf8JsonWriter writer, DateTime value, JsonSerializerOptions options) | ||
{ | ||
writer.WriteStringValue(value.ToUniversalTime()); | ||
} | ||
} | ||
} |
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 |
---|---|---|
@@ -1,12 +1,12 @@ | ||
using System.Runtime.Serialization; | ||
using System.Text.Json.Serialization; | ||
|
||
namespace YesSql.Tests.Models | ||
{ | ||
public class Animal | ||
{ | ||
public string Name { get; set; } | ||
|
||
[IgnoreDataMember] | ||
[JsonIgnore] | ||
public string Color { get; set; } | ||
} | ||
} |
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