Skip to content

Commit

Permalink
update newtonsoft json serialization
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelstonis committed Sep 1, 2021
1 parent daefaa2 commit a6150e8
Showing 1 changed file with 11 additions and 11 deletions.
22 changes: 11 additions & 11 deletions Tycho.NewtonsoftJson/NewtonsoftJsonSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@ namespace Tycho
public class NewtonsoftJsonSerializer : IJsonSerializer
{
private readonly JsonSerializer _jsonSerializer;
private readonly JsonSerializerSettings _jsonSerializerSettings;

public NewtonsoftJsonSerializer (JsonSerializer jsonSerializer = null, JsonSerializerSettings jsonSerializerSettings = null)
public NewtonsoftJsonSerializer (JsonSerializer jsonSerializer = null)
{
_jsonSerializer =
jsonSerializer ??
Expand All @@ -20,14 +19,6 @@ public NewtonsoftJsonSerializer (JsonSerializer jsonSerializer = null, JsonSeria
DefaultValueHandling = DefaultValueHandling.Include,
NullValueHandling = NullValueHandling.Ignore,
};

_jsonSerializerSettings =
jsonSerializerSettings ??
new JsonSerializerSettings
{
DefaultValueHandling = DefaultValueHandling.Include,
NullValueHandling = NullValueHandling.Ignore,
};
}


Expand All @@ -40,7 +31,16 @@ public ValueTask<T> DeserializeAsync<T> (Stream stream, CancellationToken cancel

public object Serialize<T> (T obj)
{
return JsonConvert.SerializeObject (obj, _jsonSerializerSettings);
using var stream = new MemoryStream();
using var writer = new StreamWriter(stream);
using var jsonWriter = new JsonTextWriter(writer);

_jsonSerializer.Serialize(jsonWriter, obj);

jsonWriter.Flush();
stream.Position = 0;

return stream.ToArray();
}

public override string ToString () => nameof (NewtonsoftJsonSerializer);
Expand Down

0 comments on commit a6150e8

Please sign in to comment.