Skip to content

Commit

Permalink
try another way to serialize
Browse files Browse the repository at this point in the history
  • Loading branch information
RobKraft committed Dec 4, 2024
1 parent 66e420a commit 08024e4
Showing 1 changed file with 20 additions and 7 deletions.
27 changes: 20 additions & 7 deletions Lambdas/GetCharityTypes/Function.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
using Amazon.RDS.Util;
using Amazon;
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Text.Json.Serialization.Metadata;
using System.Text.Json.Nodes;
namespace GetCharityTypes;

public class Function
Expand Down Expand Up @@ -35,6 +37,7 @@ private static async Task Main(string[] args)
await LambdaBootstrapBuilder.Create(handler, new DefaultLambdaJsonSerializer()).Build().RunAsync();

Check warning on line 37 in Lambdas/GetCharityTypes/Function.cs

View workflow job for this annotation

GitHub Actions / build-and-deploy

Using member 'Amazon.Lambda.Serialization.SystemTextJson.DefaultLambdaJsonSerializer.DefaultLambdaJsonSerializer()' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. DefaultLambdaJsonSerializer does not support trimming. For trimmed Lambda functions SourceGeneratorLambdaJsonSerializer passing in JsonSerializerContext should be used instead.

Check warning on line 37 in Lambdas/GetCharityTypes/Function.cs

View workflow job for this annotation

GitHub Actions / build-and-deploy

Using member 'Amazon.Lambda.Serialization.SystemTextJson.DefaultLambdaJsonSerializer.DefaultLambdaJsonSerializer()' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. DefaultLambdaJsonSerializer does not support trimming. For trimmed Lambda functions SourceGeneratorLambdaJsonSerializer passing in JsonSerializerContext should be used instead.

Check warning on line 37 in Lambdas/GetCharityTypes/Function.cs

View workflow job for this annotation

GitHub Actions / build-and-deploy

Using member 'Amazon.Lambda.Serialization.SystemTextJson.DefaultLambdaJsonSerializer.DefaultLambdaJsonSerializer()' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. DefaultLambdaJsonSerializer does not support trimming. For trimmed Lambda functions SourceGeneratorLambdaJsonSerializer passing in JsonSerializerContext should be used instead.
}


public static async Task<string> FunctionHandler(ILambdaContext context)
{
List<CharityType> types = new List<CharityType>();
Expand Down Expand Up @@ -65,15 +68,25 @@ 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);
var jsonNode = JsonNode.Parse(JsonSerializer.Serialize(types));

Check warning on line 71 in Lambdas/GetCharityTypes/Function.cs

View workflow job for this annotation

GitHub Actions / build-and-deploy

Using member 'System.Text.Json.JsonSerializer.Serialize<TValue>(TValue, JsonSerializerOptions)' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. JSON serialization and deserialization might require types that cannot be statically analyzed. Use the overload that takes a JsonTypeInfo or JsonSerializerContext, or make sure all of the required types are preserved.

Check warning on line 71 in Lambdas/GetCharityTypes/Function.cs

View workflow job for this annotation

GitHub Actions / build-and-deploy

Using member 'System.Text.Json.JsonSerializer.Serialize<TValue>(TValue, JsonSerializerOptions)' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. JSON serialization and deserialization might require types that cannot be statically analyzed. Use the overload that takes a JsonTypeInfo or JsonSerializerContext, or make sure all of the required types are preserved.

Check warning on line 71 in Lambdas/GetCharityTypes/Function.cs

View workflow job for this annotation

GitHub Actions / build-and-deploy

Using member 'System.Text.Json.JsonSerializer.Serialize<TValue>(TValue, JsonSerializerOptions)' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. JSON serialization and deserialization might require types that cannot be statically analyzed. Use the overload that takes a JsonTypeInfo or JsonSerializerContext, or make sure all of the required types are preserved.
string json = jsonNode.ToJsonString();

Check warning on line 72 in Lambdas/GetCharityTypes/Function.cs

View workflow job for this annotation

GitHub Actions / build-and-deploy

Dereference of a possibly null reference.

Check warning on line 72 in Lambdas/GetCharityTypes/Function.cs

View workflow job for this annotation

GitHub Actions / build-and-deploy

Dereference of a possibly null reference.

Check warning on line 72 in Lambdas/GetCharityTypes/Function.cs

View workflow job for this annotation

GitHub Actions / build-and-deploy

Dereference of a possibly null reference.
return json;
}
}
public class CharityType
[JsonSourceGenerationOptions(
WriteIndented = true,
PropertyNamingPolicy = JsonKnownNamingPolicy.CamelCase,
GenerationMode = JsonSourceGenerationMode.Serialization)]
[JsonSerializable(typeof(CharityType))]
public class CharityType //: JsonSerializerContext

Check warning on line 81 in Lambdas/GetCharityTypes/Function.cs

View workflow job for this annotation

GitHub Actions / build-and-deploy

The type 'GetCharityTypes.CharityType' has been annotated with JsonSerializableAttribute but does not derive from JsonSerializerContext. No source code will be generated. (https://learn.microsoft.com/dotnet/fundamentals/syslib-diagnostics/syslib1224)

Check warning on line 81 in Lambdas/GetCharityTypes/Function.cs

View workflow job for this annotation

GitHub Actions / build-and-deploy

The type 'GetCharityTypes.CharityType' has been annotated with JsonSerializableAttribute but does not derive from JsonSerializerContext. No source code will be generated. (https://learn.microsoft.com/dotnet/fundamentals/syslib-diagnostics/syslib1224)

Check warning on line 81 in Lambdas/GetCharityTypes/Function.cs

View workflow job for this annotation

GitHub Actions / build-and-deploy

The type 'GetCharityTypes.CharityType' has been annotated with JsonSerializableAttribute but does not derive from JsonSerializerContext. No source code will be generated. (https://learn.microsoft.com/dotnet/fundamentals/syslib-diagnostics/syslib1224)
{
public int Id { get; set; }
public string Type { get; set; } = string.Empty;
}

//protected override JsonSerializerOptions? GeneratedSerializerOptions => throw new NotImplementedException();

//public override JsonTypeInfo? GetTypeInfo(Type type)
//{
// throw new NotImplementedException();
//}
}

0 comments on commit 08024e4

Please sign in to comment.