Skip to content

Commit

Permalink
Make the json options protected for custom serializers to have access…
Browse files Browse the repository at this point in the history
… to it.
  • Loading branch information
normj committed Apr 21, 2020
1 parent d013d09 commit abdfe05
Showing 1 changed file with 17 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,25 +20,31 @@ namespace Amazon.Lambda.Serialization.SystemTextJson
public class DefaultLambdaJsonSerializer : ILambdaSerializer
{
private const string DEBUG_ENVIRONMENT_VARIABLE_NAME = "LAMBDA_NET_SERIALIZER_DEBUG";
private readonly JsonSerializerOptions _options;
private readonly bool _debug;

/// <summary>
/// The options used to serialize JSON object.
/// </summary>
protected JsonSerializerOptions Options { get; }

/// <summary>
/// Constructs instance of serializer.
/// </summary>
public DefaultLambdaJsonSerializer()
{
_options = new JsonSerializerOptions()
Options = new JsonSerializerOptions()
{
IgnoreNullValues = true,
PropertyNameCaseInsensitive = true,
PropertyNamingPolicy = new AwsNamingPolicy()
PropertyNamingPolicy = new AwsNamingPolicy(),
Converters =
{
new DateTimeConverter(),
new MemoryStreamConverter(),
new ConstantClassConverter()
}
};

_options.Converters.Add(new DateTimeConverter());
_options.Converters.Add(new MemoryStreamConverter());
_options.Converters.Add(new ConstantClassConverter());

if (string.Equals(Environment.GetEnvironmentVariable(DEBUG_ENVIRONMENT_VARIABLE_NAME), "true", StringComparison.OrdinalIgnoreCase))
{
this._debug = true;
Expand All @@ -53,7 +59,7 @@ public DefaultLambdaJsonSerializer()
public DefaultLambdaJsonSerializer(Action<JsonSerializerOptions> customizer)
: this()
{
customizer?.Invoke(this._options);
customizer?.Invoke(this.Options);
}

/// <summary>
Expand All @@ -71,7 +77,7 @@ public void Serialize<T>(T response, Stream responseStream)
using (var debugWriter = new StringWriter())
using (var utf8Writer = new Utf8JsonWriter(responseStream))
{
JsonSerializer.Serialize(utf8Writer, response, _options);
JsonSerializer.Serialize(utf8Writer, response, Options);

var jsonDocument = debugWriter.ToString();
Console.WriteLine($"Lambda Serialize {response.GetType().FullName}: {jsonDocument}");
Expand All @@ -85,7 +91,7 @@ public void Serialize<T>(T response, Stream responseStream)
{
using (var writer = new Utf8JsonWriter(responseStream))
{
JsonSerializer.Serialize(writer, response, _options);
JsonSerializer.Serialize(writer, response, Options);
}
}
}
Expand Down Expand Up @@ -129,7 +135,7 @@ public T Deserialize<T>(Stream requestStream)
}
}

return JsonSerializer.Deserialize<T>(utf8Json, _options);
return JsonSerializer.Deserialize<T>(utf8Json, Options);
}
catch (Exception e)
{
Expand Down

0 comments on commit abdfe05

Please sign in to comment.