Search⌘ K
AI Features

Working with Parameter Serializers

Understand the role of JSON serializers in AWS Lambda functions using C#. Learn how to implement the Amazon.Lambda.Serialization.SystemTextJson package to automatically convert data between JSON strings and C# objects. This lesson helps you improve your function handlers by using serializable input and output classes, making code easier to write and maintain.

How and why to use a serializer

We want to use a JSON serializer inside our .NET AWS Lambda project because it makes the development experience a lot more convenient. With a serializer, we can use any JSON-serializable data type as an input and output in our function handler. These include primitive data types, such as string and int, and Plain Old CLR Object (POCO) classes and records that are used purely for storing data.

Without a serializer, both the input and output of a function handler method have to be Stream. We’d have to extract data from the stream, do something with it, and place the response on another stream. This is much more difficult to implement and makes the code far less readable.

The following playground shows how to use serializers.

namespace LambdaApp;

public class Output
{
    public bool Success { get; set; }
    public string? OriginalInput { get; set; }
}
AWS Lambda parameters

In this setup, we have a function handler where both the input and the output are represented by two JSON-serializable POCO classes. The following diagram outlines the process of serialization and deserialization.

Serialization vs. deserialization
Serialization vs. deserialization

Essentially, JSON serialization is when we convert C# objects to a JSON string. Deserialization is the opposite—converting a JSON string into a C# object we can use in our code. Serializers are the components that do this conversion so we don’t have to write any custom code for it.

Adding serialization dependency

There are several ways of implementing JSON serializers in an AWS Lambda project. One of the standard libraries is Amazon.Lambda.Serialization.SystemTextJson. It provides a standard JSON serializer that is usable in most scenarios. This library is added to our project as a NuGet package. It can be found on line 13 of the LambdaApp.csproj file.

Using the serializer

Before we can apply a JSON serializer in a function handler, we need to reference the appropriate serializer in a LambdaSerializer assembly attribute. We do this on line 3 of the Function.cs file of the setup. We’re using the DefaultLambdaJsonSerializer type from the Amazon.Lambda.Serialization.SystemTextJson namespace as the serializer.

The serializer we’re using in this example is suitable for most real-world scenarios. However, if we need any custom serialization logic, we can create our own serializer class. We just need to make sure that it implements the ILambdaSerializer interface.

Using serializable objects

This is all we need to do to implement the serializer. The serialization process will now be done automatically by AWS Lambda runtime. On line 9, where we have our function handler, we use the following POCO classes as the input and output:

  • The Input class is used as the input. Its definition can be found in the Input.cs file, and it consists of two public properties: an integer Id and a nullable string Name.

  • The Output class is used as the output. It’s defined in the Output.cs file and consists of two properties: a boolean Success and a nullable string OriginalInput.

To test our function, we can launch the application in the playground by clicking the “Run” button. Once the application is loaded, we can pass a JSON input into the “Function Input” field that resembles a serialized version of the Input class. Here’s an example we could use.

{
"id": 5,
"name": "David"
}
Example JSON input resembling the Input class

We just need to change the first letter of each property name in the Input class from an uppercase to a lowercase letter. This is the expected output.

{
"Success": true,
"OriginalInput": "{\"id\":5,\"name\":\"David\"}"
}
Example output