forked from zcsizmadia/ZCS.Utf8Json
-
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.
- Loading branch information
Showing
2 changed files
with
57 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Text; | ||
|
||
namespace Utf8Json.Formatters | ||
{ | ||
// MEMO: Not recommended. should write/read base64 directly like corefxlab/System.Binary.Base64 | ||
// https://github.com/dotnet/runtime/issues/30456 | ||
public sealed class ByteArrayAsListFormatter : IJsonFormatter<byte[]> | ||
{ | ||
public static readonly IJsonFormatter<byte[]> Default = new ByteArrayAsListFormatter(); | ||
|
||
public void Serialize(ref JsonWriter writer, byte[] value, IJsonFormatterResolver formatterResolver) | ||
{ | ||
if (value == null) { writer.WriteNull(); return; } | ||
|
||
new ArrayFormatter<byte>().Serialize(ref writer, value, formatterResolver); | ||
} | ||
|
||
public byte[] Deserialize(ref JsonReader reader, IJsonFormatterResolver formatterResolver) | ||
{ | ||
if (reader.ReadIsNull()) return null; | ||
|
||
if (reader.GetCurrentJsonToken() == JsonToken.BeginArray) | ||
{ | ||
return new ArrayFormatter<byte>().Deserialize(ref reader, formatterResolver); | ||
} | ||
|
||
return ByteArrayFormatter.Default.Deserialize(ref reader, formatterResolver); | ||
} | ||
} | ||
} |
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