Skip to content

Commit

Permalink
Another try at serializing
Browse files Browse the repository at this point in the history
  • Loading branch information
RobKraft committed Dec 9, 2024
1 parent f23c7dd commit db6767f
Showing 1 changed file with 18 additions and 9 deletions.
27 changes: 18 additions & 9 deletions Lambdas/GetCharityTypes/Function.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using System.Text.Json.Serialization.Metadata;
using System.Text.Json.Serialization;
using Amazon.Lambda.APIGatewayEvents;

namespace GetCharityTypes;

public class Function
Expand All @@ -33,8 +34,11 @@ private static async Task Main(string[] args)
_connectionString = $"Server={_dbHost};Port={_dbPort};Database={_dbName};" +
$"User={_dbUser};Password={authToken};SSL Mode=Required;";

Func<ILambdaContext, Task<string>> handler = FunctionHandler;
await LambdaBootstrapBuilder.Create(handler, new SourceGeneratorLambdaJsonSerializer<CustomSerializer>()).Build().RunAsync();
// Use the source generator serializer
await LambdaBootstrapBuilder.Create(
FunctionHandler,
new SourceGeneratorLambdaJsonSerializer<CustomSerializer>()
).Build().RunAsync();
}

public static async Task<string> FunctionHandler(ILambdaContext context)
Expand All @@ -45,7 +49,6 @@ public static async Task<string> FunctionHandler(ILambdaContext context)
using (MySqlConnection connection = new MySqlConnection(_connectionString))
{
await connection.OpenAsync();

string sql = "SELECT id, name FROM types";
using (MySqlCommand command = new MySqlCommand(sql, connection))
{
Expand All @@ -67,20 +70,26 @@ public static async Task<string> FunctionHandler(ILambdaContext context)
{
Console.WriteLine($"connstring={_connectionString} and error: {ex.Message}");
}
var options = new JsonSerializerOptions
{
TypeInfoResolver = null // Enables reflection-based serialization
};
return JsonSerializer.Serialize(types, options);

// Use the source generator serialization
return JsonSerializer.Serialize(types, CustomSerializerContext.Default.ListCharityType);

Check failure on line 75 in Lambdas/GetCharityTypes/Function.cs

View workflow job for this annotation

GitHub Actions / build-and-deploy

The name 'CustomSerializerContext' does not exist in the current context

Check failure on line 75 in Lambdas/GetCharityTypes/Function.cs

View workflow job for this annotation

GitHub Actions / build-and-deploy

The name 'CustomSerializerContext' does not exist in the current context
}
}

[JsonSerializable(typeof(APIGatewayProxyRequest))]
[JsonSerializable(typeof(APIGatewayProxyResponse))]
[JsonSerializable(typeof(CharityType))]
[JsonSerializable(typeof(List<CharityType>))]
public partial class CustomSerializer : JsonSerializerContext
{

}

//[JsonSerializable(typeof(List<CharityType>))]
//[JsonSourceGenerationOptions(PropertyNamingPolicy = JsonKnownNamingPolicy.CamelCase)]
//public partial class CustomSerializerContext : JsonSerializerContext
//{
//}

public class CharityType
{
public int Id { get; set; }
Expand Down

0 comments on commit db6767f

Please sign in to comment.