<Compile Include="System\Text\Json\Serialization\Converters\DefaultImmutableConverter.cs" />
<Compile Include="System\Text\Json\Serialization\Converters\JsonValueConverterBoolean.cs" />
<Compile Include="System\Text\Json\Serialization\Converters\JsonValueConverterByte.cs" />
+ <Compile Include="System\Text\Json\Serialization\Converters\JsonValueConverterByteArray.cs" />
<Compile Include="System\Text\Json\Serialization\Converters\JsonValueConverterChar.cs" />
<Compile Include="System\Text\Json\Serialization\Converters\JsonValueConverterDateTime.cs" />
<Compile Include="System\Text\Json\Serialization\Converters\JsonValueConverterDateTimeOffset.cs" />
{
private static readonly Dictionary<Type, object> s_valueConverters = new Dictionary<Type, object>()
{
+ { typeof(byte[]), new JsonValueConverterByteArray() },
{ typeof(DateTimeOffset), new JsonValueConverterDateTimeOffset() },
{ typeof(Guid), new JsonValueConverterGuid() },
- { typeof(JsonElement), new JsonValueConverterJsonElement() }
+ { typeof(JsonElement), new JsonValueConverterJsonElement() },
};
internal static bool IsValueConvertable(Type type)
new object[] { false },
culture: null);
}
+
TypeCode typeCode = Type.GetTypeCode(type);
switch (typeCode)
{
--- /dev/null
+// Licensed to the .NET Foundation under one or more agreements.
+// The .NET Foundation licenses this file to you under the MIT license.
+// See the LICENSE file in the project root for more information.
+
+using System.Text.Json.Serialization.Policies;
+
+namespace System.Text.Json.Serialization.Converters
+{
+ internal sealed class JsonValueConverterByteArray : JsonValueConverter<byte[]>
+ {
+ public override bool TryRead(Type valueType, ref Utf8JsonReader reader, out byte[] value)
+ {
+ if (reader.TokenType != JsonTokenType.String)
+ {
+ value = default;
+ return false;
+ }
+
+ return reader.TryGetBytesFromBase64(out value);
+ }
+
+ public override void Write(byte[] value, Utf8JsonWriter writer)
+ {
+ writer.WriteBase64StringValue(value);
+ }
+
+ public override void Write(JsonEncodedText propertyName, byte[] value, Utf8JsonWriter writer)
+ {
+ writer.WriteBase64String(propertyName, value);
+ }
+ }
+}
IsPropertyPolicy = true;
HasGetter = true;
HasSetter = true;
- ValueConverter = DefaultConverters<TRuntimeProperty>.s_converter;
}
GetPolicies();
}
[Fact]
+ public static void ReadNullByteArray()
+ {
+ string json = @"null";
+ byte[] arr = JsonSerializer.Parse<byte[]>(json);
+ Assert.Null(arr);
+ }
+
+ [Fact]
+ public static void ReadEmptyByteArray()
+ {
+ string json = @"""""";
+ byte[] arr = JsonSerializer.Parse<byte[]>(json);
+ Assert.Equal(0, arr.Length);
+ }
+
+ [Fact]
+ public static void ReadByteArray()
+ {
+ string json = $"\"{Convert.ToBase64String(new byte[] { 1, 2 })}\"";
+ byte[] arr = JsonSerializer.Parse<byte[]>(json);
+
+ Assert.Equal(2, arr.Length);
+ Assert.Equal(1, arr[0]);
+ Assert.Equal(2, arr[1]);
+ }
+
+ [Fact]
+ public static void Read2dByteArray()
+ {
+ // Baseline for comparison.
+ Assert.Equal("AQI=", Convert.ToBase64String(new byte[] { 1, 2 }));
+
+ string json = "[\"AQI=\",\"AQI=\"]";
+ byte[][] arr = JsonSerializer.Parse<byte[][]>(json);
+ Assert.Equal(2, arr.Length);
+
+ Assert.Equal(2, arr[0].Length);
+ Assert.Equal(1, arr[0][0]);
+ Assert.Equal(2, arr[0][1]);
+
+ Assert.Equal(2, arr[1].Length);
+ Assert.Equal(1, arr[1][0]);
+ Assert.Equal(2, arr[1][1]);
+ }
+
+ [Fact]
+ public static void ReadByteArrayFail()
+ {
+ Assert.Throws<JsonException>(() => JsonSerializer.Parse<byte[]>(@"""1"""));
+ Assert.Throws<JsonException>(() => JsonSerializer.Parse<byte[]>(@"""A==="""));
+ }
+
+ [Fact]
+ public static void ReadByteArrayAsJsonArrayFail()
+ {
+ string json = $"[1, 2]";
+ // Currently no support deserializing JSON arrays as byte[] - only Base64 string.
+ Assert.Throws<JsonException>(() => JsonSerializer.Parse<byte[]>(json));
+ }
+
+ [Fact]
+ public static void ReadByteListAsJsonArray()
+ {
+ string json = $"[1, 2]";
+ List<byte> list = JsonSerializer.Parse<List<byte>>(json);
+
+ Assert.Equal(2, list.Count);
+ Assert.Equal(1, list[0]);
+ Assert.Equal(2, list[1]);
+ }
+
+ [Fact]
public static void DeserializeObjectArray_36167()
{
// https://github.com/dotnet/corefx/issues/36167
}
[Fact]
+ public static void WriteNullByteArray()
+ {
+ byte[] input = null;
+ string json = JsonSerializer.ToString(input);
+ Assert.Equal($"null", json);
+ }
+
+ [Fact]
+ public static void WriteEmptyByteArray()
+ {
+ var input = new byte[] {};
+ string json = JsonSerializer.ToString(input);
+ Assert.Equal(@"""""", json);
+ }
+
+ [Fact]
+ public static void WriteByteArray()
+ {
+ var input = new byte[] { 1, 2 };
+ string json = JsonSerializer.ToString(input);
+ Assert.Equal($"\"{Convert.ToBase64String(input)}\"", json);
+ }
+
+ [Fact]
+ public static void WriteTwo2dByteArray()
+ {
+ var inner = new byte[] { 1, 2 };
+ var outer = new byte[2][] { inner, inner };
+ string json = JsonSerializer.ToString(outer);
+ Assert.Equal($"[\"{Convert.ToBase64String(inner)}\",\"{Convert.ToBase64String(inner)}\"]", json);
+ }
+
+ [Fact]
public static void WriteObjectArray()
{
string json;
@"""MyUInt16Array"" : [4]," +
@"""MyUInt32Array"" : [5]," +
@"""MyUInt64Array"" : [6]," +
- @"""MyByteArray"" : [7]," +
+ @"""MyByteArray"" : ""Bw==""," + // Base64 encoded value of 7
@"""MySByteArray"" : [8]," +
@"""MyCharArray"" : [""a""]," +
@"""MyStringArray"" : [""Hello""]," +
@"""MyUInt16Array"" : [4]," +
@"""MyUInt32Array"" : [5]," +
@"""MyUInt64Array"" : [6]," +
- @"""MyByteArray"" : [7]," +
+ @"""MyByteArray"" : ""Bw==""," + // Base64 encoded value of 7
@"""MySByteArray"" : [8]," +
@"""MyCharArray"" : [""a""]," +
@"""MyStringArray"" : [""Hello""]," +