Skip to content

1728 - Enhance/Fix ByteArrayConverter to support base64 deserialization of byte[] #1736

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<AssemblyName>Amazon.Lambda.Serialization.SystemTextJson</AssemblyName>
<PackageId>Amazon.Lambda.Serialization.SystemTextJson</PackageId>
<PackageTags>AWS;Amazon;Lambda</PackageTags>
<VersionPrefix>2.4.2</VersionPrefix>
<VersionPrefix>2.4.3</VersionPrefix>
<PackageReadmeFile>README.md</PackageReadmeFile>
</PropertyGroup>
<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ public override byte[] Read(ref Utf8JsonReader reader, Type typeToConvert, JsonS
return null;
}

if (reader.TokenType == JsonTokenType.String)
{
return reader.GetBytesFromBase64();
}

var byteList = new List<byte>();

while (reader.Read())
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using Amazon.Lambda.Serialization.SystemTextJson;
using System.IO;
using System.Text;
using Xunit;

namespace Amazon.Lambda.AspNetCoreServer.Test;

public class DefaultLambdaJsonSerializerTests
{
private const string SampleValue = "Hello World!";

[Fact]
public void CanDeserializeIntArrayEncodedString() =>
RunTest("{\"Value\":[72,101,108,108,111,32,87,111,114,108,100,33]}");

[Fact]
public void CanDeserializeBase64EncodedString() =>
RunTest("{\"Value\":\"SGVsbG8gV29ybGQh\"}");

private void RunTest(string jsonPayload)
{
using var ms = new MemoryStream(Encoding.UTF8.GetBytes(jsonPayload));

var defaultLambdaJsonSerializer = new DefaultLambdaJsonSerializer();
var inputFromMemoryStream = defaultLambdaJsonSerializer.Deserialize<ClassWithByteArrayProperty>(ms);

Assert.Equal(SampleValue, Encoding.UTF8.GetString(inputFromMemoryStream.Value));
}

private class ClassWithByteArrayProperty
{
public byte[] Value { get; set; }
}
}