Skip to content

Commit ef8b900

Browse files
authored
Merge pull request awsdocs#317 from mgorski-mg/patch-1
Update info about json serialization
2 parents a1bb6b6 + 20e3f1a commit ef8b900

File tree

1 file changed

+36
-52
lines changed

1 file changed

+36
-52
lines changed

doc_source/csharp-handler.md

Lines changed: 36 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -50,47 +50,9 @@ All other types, as listed below, require you to specify a serializer\.
5050
+ For asynchronous invocations the return\-type will be ignored by Lambda\. The return type may be set to void in such cases\.
5151
+ If you are using \.NET asynchronous programming, the return type can be Task and Task<T> types and use `async` and `await` keywords\. For more information, see [Using async in C\# functions with AWS Lambda](#csharp-handler-async)\.
5252

53-
Unless your function input and output parameters are of type `System.IO.Stream`, you will need to serialize them\. AWS Lambda provides a default serializer that can be applied at the assembly or method level of your application, or you can define your own by implementing the `ILambdaSerializer` interface provided by the `Amazon.Lambda.Core` library\. For more information, see [Deploy C\# Lambda functions with \.zip file archives](csharp-package.md)\.
53+
Unless your function input and output parameters are of type `System.IO.Stream`, you will need to serialize them\. AWS Lambda provides a default serializers that can be applied at the assembly or method level of your application, or you can define your own by implementing the `ILambdaSerializer` interface provided by the `Amazon.Lambda.Core` library\. For more information, see [Deploy C\# Lambda functions with \.zip file archives](csharp-package.md)\.
5454

55-
To add the default serializer attribute to a method, first add a dependency on `Amazon.Lambda.Serialization.Json` in your `.csproj` file\.
56-
57-
```
58-
<Project Sdk="Microsoft.NET.Sdk">
59-
<PropertyGroup>
60-
<TargetFramework>netcoreapp3.1</TargetFramework>
61-
<GenerateRuntimeConfigurationFiles>true</GenerateRuntimeConfigurationFiles>
62-
<AssemblyName>AssemblyName</AssemblyName>
63-
</PropertyGroup>
64-
65-
<ItemGroup>
66-
<PackageReference Include="Amazon.Lambda.Core" Version="1.2.0" />
67-
<PackageReference Include="Amazon.Lambda.APIGatewayEvents" Version="2.3.0" />
68-
<PackageReference Include="Amazon.Lambda.Serialization.Json" Version="1.8.0" />
69-
<PackageReference Include="Newtonsoft.Json" Version="12.0.1" />
70-
</ItemGroup>
71-
</Project>
72-
```
73-
74-
The example below illustrates the flexibility you can leverage by specifying the default Json\.NET serializer on one method and another of your choosing on a different method:
75-
76-
```
77-
public class ProductService{
78-
[LambdaSerializer(typeof(Amazon.Lambda.Serialization.Json.JsonSerializer))]
79-
public Product DescribeProduct(DescribeProductRequest request)
80-
{
81-
return catalogService.DescribeProduct(request.Id);
82-
}
83-
84-
[LambdaSerializer(typeof(MyJsonSerializer))]
85-
public Customer DescribeCustomer(DescribeCustomerRequest request)
86-
{
87-
return customerService.DescribeCustomer(request.Id);
88-
}
89-
}
90-
```
91-
92-
**Note**
93-
If you are using \.NET Core 3\.1, we recommend that you use the [ Amazon\.Lambda\.Serialization\.SystemTextJson](https://github.com/aws/aws-lambda-dotnet/tree/master/Libraries/src/Amazon.Lambda.Serialization.SystemTextJson) serializer\. This package provides a performance improvement over `Amazon.Lambda.Serialization.Json`\.
55+
Instruction how to define serialize you can find in [Serializing Lambda functions](#csharp-handler-serializer)\.
9456

9557
## Handler signatures<a name="csharp-handler-signatures"></a>
9658

@@ -128,9 +90,8 @@ If the method specified in your handler string is overloaded, you must provide t
12890
## Serializing Lambda functions<a name="csharp-handler-serializer"></a>
12991

13092
For any Lambda functions that use input or output types other than a `Stream` object, you will need to add a serialization library to your application\. You can do this in the following ways:
131-
+ Use the `Amazon.Lambda.Serialization.Json` NuGet package\. This library uses JSON\.NET to handle serialization\.
132-
**Note**
133-
If you are using \.NET Core 3\.1, we recommend that you use the [ Amazon\.Lambda\.Serialization\.SystemTextJson](https://github.com/aws/aws-lambda-dotnet/tree/master/Libraries/src/Amazon.Lambda.Serialization.SystemTextJson) serializer\. This package provides a performance improvement over `Amazon.Lambda.Serialization.Json`\.
93+
+ Use the `Amazon.Lambda.Serialization.SystemTextJson` NuGet package\. This library uses native .net core JSON serializer to handle serialization\. This package provides a performance improvement over `Amazon.Lambda.Serialization.Json`, but please note that there are some limitation documented in the [Microsoft's documentation](https://docs.microsoft.com/en-us/dotnet/standard/serialization/system-text-json-how-to?pivots=dotnet-core-3-1)\. It is only available for \.net core 3\.1 runtimes or higher\.
94+
+ Use the `Amazon.Lambda.Serialization.Json` NuGet package\. This library uses `Newtonsoft.Json` NuGet package to handle serialization\.
13495
+ Create your own serialization library by implementing the `ILambdaSerializer` interface, which is available as part of the `Amazon.Lambda.Core` library\. The interface defines two methods:
13596
+ `T Deserialize<T>(Stream requestStream);`
13697

@@ -139,24 +100,47 @@ If you are using \.NET Core 3\.1, we recommend that you use the [ Amazon\.Lambda
139100

140101
You implement this method to serialize the result returned from the Lambda function handler into the response payload that is returned by the `Invoke` API\.
141102

142-
You use whichever serializer you wish by adding it as a dependency to your `MyProject.csproj` file\.
103+
104+
To use serializer you need to add a dependency to your `csproj` file\.
143105

144106
```
145-
...
146107
<ItemGroup>
147-
<PackageReference Include="Amazon.Lambda.Core" Version="1.0.0" />
148-
<PackageReference Include="Amazon.Lambda.Serialization.Json" Version="1.3.0" />
108+
<PackageReference Include="Amazon.Lambda.Serialization.SystemTextJson" Version="2.1.0" />
109+
<!-- or -->
110+
<PackageReference Include="Amazon.Lambda.Serialization.Json" Version="2.0.0" />
149111
</ItemGroup>
150112
```
151113

152-
You then add it to your AssemblyInfo\.cs file\. For example, if you are using the default Json\.NET serializer, this is what you would add:
114+
Next you need to define the serializer. For example you can define it in the AssemblyInfo\.cs file\. If you are using `Amazon.Lambda.Serialization.SystemTextJson`, the serializercan be defined that way:
153115

154116
```
155-
[assembly:LambdaSerializer(typeof(Amazon.Lambda.Serialization.Json.JsonSerializer))]
117+
[assembly: LambdaSerializer(typeof(DefaultLambdaJsonSerializer))]
118+
```
119+
120+
Using `Amazon.Lambda.Serialization.Json`, it will be:
121+
122+
```
123+
[assembly: LambdaSerializer(typeof(JsonSerializer))]
156124
```
157125

158-
**Note**
159-
You can define a custom serialization attribute at the method level, which will override the default serializer specified at the assembly level\. For more information, see [Handling standard data types](#csharp-handler-types)\.
126+
You can also define a custom serialization attribute at the method level, which will override the default serializer specified at the assembly level\.
127+
128+
```
129+
public class ProductService{
130+
131+
[LambdaSerializer(typeof(JsonSerializer))]
132+
public Product DescribeProduct(DescribeProductRequest request)
133+
{
134+
return catalogService.DescribeProduct(request.Id);
135+
}
136+
137+
[LambdaSerializer(typeof(MyJsonSerializer))]
138+
public Customer DescribeCustomer(DescribeCustomerRequest request)
139+
{
140+
return customerService.DescribeCustomer(request.Id);
141+
}
142+
}
143+
```
160144

161145
## Lambda function handler restrictions<a name="csharp-handler-restrictions"></a>
162146

@@ -216,4 +200,4 @@ If you use this pattern, there are some considerations you must take into accoun
216200
// Lambda may return before printing all tests since we're only waiting for one to finish.
217201
await Task.WhenAny(task1, task2, task3);
218202
}
219-
```
203+
```

0 commit comments

Comments
 (0)