-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update NewtonSoftJsonSerializer to use new ThreadLocal instance(s) of…
… JsonSerializer and improve performance for serializing and deserializing large objects. Based on [recommendation from Json.NET docs](https://www.newtonsoft.com/json/help/html/Performance.htm#MemoryUsage)
- Loading branch information
Showing
8 changed files
with
335 additions
and
164 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
//----------------------------------------------------------------------- | ||
// <copyright file="NewtonSoftJsonSerializer.cs" company="Akka.NET Project"> | ||
// Copyright (C) 2009-2021 Lightbend Inc. <http://www.lightbend.com> | ||
// Copyright (C) 2013-2021 .NET Foundation <https://github.com/akkadotnet/akka.net> | ||
// </copyright> | ||
//----------------------------------------------------------------------- | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using Newtonsoft.Json; | ||
|
||
namespace Akka.Serialization | ||
{ | ||
|
||
public partial class NewtonSoftJsonSerializer | ||
{ | ||
internal class DelegatedObjectConverter : JsonConverter | ||
{ | ||
private readonly IObjectConverter[] objectConverters; | ||
|
||
public DelegatedObjectConverter(params IObjectConverter[] objectConverters) | ||
{ | ||
this.objectConverters = objectConverters; | ||
} | ||
|
||
public override bool CanConvert(Type objectType) | ||
{ | ||
return objectType == typeof(object); | ||
} | ||
|
||
public override bool CanWrite => false; | ||
|
||
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) | ||
{ | ||
var deserializedValue = serializer.Deserialize(reader); | ||
|
||
foreach (var objectConverter in objectConverters) | ||
{ | ||
if (objectConverter.TryConvert(deserializedValue, out var convertedValue)) | ||
{ | ||
return convertedValue; | ||
} | ||
} | ||
|
||
return deserializedValue; | ||
} | ||
|
||
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) | ||
{ | ||
throw new NotSupportedException(); | ||
} | ||
} | ||
} | ||
} |
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,18 @@ | ||
//----------------------------------------------------------------------- | ||
// <copyright file="NewtonSoftJsonSerializer.cs" company="Akka.NET Project"> | ||
// Copyright (C) 2009-2021 Lightbend Inc. <http://www.lightbend.com> | ||
// Copyright (C) 2013-2021 .NET Foundation <https://github.com/akkadotnet/akka.net> | ||
// </copyright> | ||
//----------------------------------------------------------------------- | ||
|
||
namespace Akka.Serialization | ||
{ | ||
|
||
public partial class NewtonSoftJsonSerializer | ||
{ | ||
internal interface IObjectConverter | ||
{ | ||
bool TryConvert(object deserializedValue, out object convertedValue); | ||
} | ||
} | ||
} |
Oops, something went wrong.