forked from faustodavid/ListPool
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add resolver, formatters tests, update benchmarks
- Loading branch information
Fausto David
committed
Feb 18, 2020
1 parent
9609b7e
commit 57eb9b6
Showing
17 changed files
with
131 additions
and
100 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
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
20 changes: 19 additions & 1 deletion
20
src/ListPool.Resolvers.Utf8Json/ListPool.Resolvers.Utf8Json.csproj
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,71 +1,48 @@ | ||
using System; | ||
using System.Runtime.CompilerServices; | ||
using Utf8Json; | ||
using Utf8Json.Resolvers; | ||
using Utf8Json; | ||
|
||
namespace ListPool.Resolvers.Utf8Json | ||
{ | ||
public class ListPoolFormatter<T> : IJsonFormatter<ListPool<T>> | ||
{ | ||
public void Serialize(ref JsonWriter writer, ListPool<T> values, IJsonFormatterResolver formatterResolver) | ||
public void Serialize(ref JsonWriter writer, ListPool<T> value, IJsonFormatterResolver formatterResolver) | ||
{ | ||
if (values == null) | ||
if (value == null) | ||
{ | ||
writer.WriteNull(); | ||
return; | ||
} | ||
|
||
writer.WriteBeginArray(); | ||
var formatter = formatterResolver.GetFormatterWithVerify<T>(); | ||
|
||
ReadOnlySpan<T> buffer = values.AsSpan(); | ||
IJsonFormatter<T> formatter = formatterResolver.GetFormatterWithVerify<T>(); | ||
|
||
foreach (T item in buffer) | ||
if (value.Count != 0) | ||
{ | ||
formatter.Serialize(ref writer, item, formatterResolver); | ||
writer.WriteValueSeparator(); | ||
formatter.Serialize(ref writer, value[0], formatterResolver); | ||
} | ||
|
||
writer.GetBuffer().AsSpan()[writer.CurrentOffset - 1] = (byte)']'; | ||
} | ||
|
||
public ListPool<T> Deserialize(ref JsonReader reader, IJsonFormatterResolver formatterResolver) | ||
{ | ||
int count = 0; | ||
var formatter = formatterResolver.GetFormatterWithVerify<T>(); | ||
|
||
var list = new ListPool<T>(); | ||
reader.ReadIsBeginArrayWithVerify(); | ||
while (!reader.ReadIsEndArrayWithSkipValueSeparator(ref count)) | ||
foreach (var item in value.AsSpan().Slice(1)) | ||
{ | ||
list.Add(formatter.Deserialize(ref reader, formatterResolver)); | ||
writer.WriteValueSeparator(); | ||
formatter.Serialize(ref writer, item, formatterResolver); | ||
} | ||
|
||
return list; | ||
writer.WriteEndArray(); | ||
} | ||
|
||
public ListPool<T> Deserialize(byte[] bytes) | ||
public ListPool<T> Deserialize(ref JsonReader reader, IJsonFormatterResolver formatterResolver) | ||
{ | ||
JsonReader reader = new JsonReader(bytes); | ||
int count = 0; | ||
var formatter = StandardResolver.Default.GetFormatterWithVerify<T>(); | ||
var formatter = formatterResolver.GetFormatterWithVerify<T>(); | ||
|
||
var list = new ListPool<T>(); | ||
ListPool<T> listPool = new ListPool<T>(); | ||
reader.ReadIsBeginArrayWithVerify(); | ||
while (!reader.ReadIsEndArrayWithSkipValueSeparator(ref count)) | ||
{ | ||
list.Add(formatter.Deserialize(ref reader, StandardResolver.Default)); | ||
listPool.Add(formatter.Deserialize(ref reader, formatterResolver)); | ||
} | ||
|
||
return list; | ||
} | ||
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
public byte[] Serialize(ListPool<T> listPool) | ||
{ | ||
JsonWriter writer = new JsonWriter(MemoryPool.GetBuffer()); | ||
Serialize(ref writer, listPool, StandardResolver.Default); | ||
return writer.ToUtf8ByteArray(); | ||
return listPool; | ||
} | ||
} | ||
} |
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 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
2 changes: 1 addition & 1 deletion
2
tests/ListPool.Resolvers.Utf8Json.Tests/CustomObjectWithListPool.cs
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
Oops, something went wrong.