Skip to content

Commit

Permalink
Update serialize code to non generic overloads
Browse files Browse the repository at this point in the history
  • Loading branch information
Mpdreamz committed Mar 26, 2020
1 parent b4eb73e commit f20285f
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
16 changes: 13 additions & 3 deletions src/Elasticsearch.Net/Serialization/SystemTextJsonSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,23 @@ public T Deserialize<T>(Stream stream)
public void Serialize<T>(T data, Stream stream, SerializationFormatting formatting = None)
{
using var writer = new Utf8JsonWriter(stream);
JsonSerializer.Serialize<T>(writer, data, GetFormatting(formatting));
if (data == null)
JsonSerializer.Serialize(writer, null, typeof(object), GetFormatting(formatting));
//TODO validate if we can avoid boxing by checking if data is typeof(object)
else
JsonSerializer.Serialize(writer, data, data.GetType(), GetFormatting(formatting));
}

public async Task SerializeAsync<T>(
T data, Stream stream, SerializationFormatting formatting = None, CancellationToken cancellationToken = default
) =>
await JsonSerializer.SerializeAsync<T>(stream, data, GetFormatting(formatting), cancellationToken).ConfigureAwait(false);
)
{
if (data == null)
await JsonSerializer.SerializeAsync(stream, null, typeof(object), GetFormatting(formatting)).ConfigureAwait(false);
else
await JsonSerializer.SerializeAsync(stream, data, data.GetType(), GetFormatting(formatting), cancellationToken).ConfigureAwait(false);
}


private JsonSerializerOptions GetFormatting(SerializationFormatting formatting) => formatting == None ? _none : _indented;

Expand Down
2 changes: 2 additions & 0 deletions tests/Tests/CodeStandards/NamingConventions.doc.cs
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,7 @@ public void AllElasticsearchNetTypesAreInElasticsearchNetNamespace()
elasticsearchNetAssembly.GetType("System.FormattableString"),
elasticsearchNetAssembly.GetType("System.Runtime.CompilerServices.FormattableStringFactory"),
elasticsearchNetAssembly.GetType("System.Runtime.CompilerServices.FormattableStringFactory"),
elasticsearchNetAssembly.GetType("System.Runtime.CompilerServices.FormattableStringFactory"),
elasticsearchNetAssembly.GetType("Purify.Purifier"),
elasticsearchNetAssembly.GetType("Purify.Purifier+IPurifier"),
elasticsearchNetAssembly.GetType("Purify.Purifier+PurifierDotNet"),
Expand All @@ -210,6 +211,7 @@ public void AllElasticsearchNetTypesAreInElasticsearchNetNamespace()
.Where(t => !t.Namespace.StartsWith("Elasticsearch.Net.Extensions"))
.Where(t => !t.Namespace.StartsWith("Elasticsearch.Net.Diagnostics"))
.Where(t => !t.Namespace.StartsWith("Elasticsearch.Net.CrossPlatform"))
.Where(t => !t.Namespace.StartsWith("System.Runtime.CompilerServices"))
.Where(t => !t.Name.StartsWith("<"))
.Where(t => IsValidTypeNameOrIdentifier(t.Name, true))
.ToList();
Expand Down

0 comments on commit f20285f

Please sign in to comment.