using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Text.Json;
using Amazon.Lambda.Core;
using Amazon.Lambda.Serialization.SystemTextJson.Converters;
namespace Amazon.Lambda.Serialization.SystemTextJson
{
///
/// Custom ILambdaSerializer implementation which uses System.Text.Json
/// for serialization.
///
///
/// If the environment variable LAMBDA_NET_SERIALIZER_DEBUG is set to true the JSON coming
/// in from Lambda and being sent back to Lambda will be logged.
///
///
public class DefaultLambdaJsonSerializer : AbstractLambdaJsonSerializer, ILambdaSerializer
{
///
/// The options used to serialize JSON object.
///
protected JsonSerializerOptions SerializerOptions { get; }
///
/// Constructs instance of serializer.
///
public DefaultLambdaJsonSerializer()
: this(null, null)
{
}
///
/// Constructs instance of serializer with the option to customize the JsonSerializerOptions after the
/// Amazon.Lambda.Serialization.SystemTextJson's default settings have been applied.
///
///
public DefaultLambdaJsonSerializer(Action customizer)
: this(customizer, null)
{
}
///
/// Constructs instance of serializer with the option to customize the JsonSerializerOptions after the
/// Amazon.Lambda.Serialization.SystemTextJson's default settings have been applied.
///
///
///
public DefaultLambdaJsonSerializer(Action customizer, Action jsonWriterCustomizer)
: base(jsonWriterCustomizer)
{
SerializerOptions = CreateDefaultJsonSerializationOptions();
customizer?.Invoke(this.SerializerOptions);
jsonWriterCustomizer?.Invoke(this.WriterOptions);
}
///
protected override void InternalSerialize(Utf8JsonWriter writer, T response)
{
JsonSerializer.Serialize(writer, response, SerializerOptions);
}
///
protected override T InternalDeserialize(byte[] utf8Json)
{
return JsonSerializer.Deserialize(utf8Json, SerializerOptions);
}
}
}