AWS.Lambda.Powertools.Kafka.Json
1.0.2
Prefix Reserved
dotnet add package AWS.Lambda.Powertools.Kafka.Json --version 1.0.2
NuGet\Install-Package AWS.Lambda.Powertools.Kafka.Json -Version 1.0.2
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="AWS.Lambda.Powertools.Kafka.Json" Version="1.0.2" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="AWS.Lambda.Powertools.Kafka.Json" Version="1.0.2" />
<PackageReference Include="AWS.Lambda.Powertools.Kafka.Json" />
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add AWS.Lambda.Powertools.Kafka.Json --version 1.0.2
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: AWS.Lambda.Powertools.Kafka.Json, 1.0.2"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package AWS.Lambda.Powertools.Kafka.Json@1.0.2
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=AWS.Lambda.Powertools.Kafka.Json&version=1.0.2
#tool nuget:?package=AWS.Lambda.Powertools.Kafka.Json&version=1.0.2
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
Powertools for AWS Lambda (.NET) - Kafka JSON
A specialized Lambda serializer for handling Kafka events with JSON-formatted data in .NET Lambda functions.
Features
- Automatic JSON Deserialization: Seamlessly converts JSON data from Kafka records into strongly-typed .NET objects
- Base64 Decoding: Handles base64-encoded JSON data from Kafka events automatically
- Type Safety: Leverages compile-time type checking with .NET classes
- Flexible Configuration: Supports custom JSON serialization options and AOT-compatible contexts
- High Performance: Optimized JSON processing using System.Text.Json
- Error Handling: Provides clear error messages for serialization failures
Installation
dotnet add package AWS.Lambda.Powertools.Kafka.Json
Quick Start
1. Configure the Serializer
Add the serializer to your Lambda function assembly:
[assembly: LambdaSerializer(typeof(PowertoolsKafkaJsonSerializer))]
2. Define Your Data Model
Create your .NET classes with JSON serialization attributes:
public class Customer
{
[JsonPropertyName("id")]
public string Id { get; set; } = "";
[JsonPropertyName("name")]
public string Name { get; set; } = "";
[JsonPropertyName("age")]
public int Age { get; set; }
[JsonPropertyName("email")]
public string Email { get; set; } = "";
}
3. Create Your Lambda Handler
public class Function
{
public void Handler(ConsumerRecords<string, Customer> records, ILambdaContext context)
{
foreach (var record in records)
{
Customer customer = record.Value; // Automatically deserialized from JSON
context.Logger.LogInformation($"Processing customer: {customer.Name}, Age: {customer.Age}");
}
}
}
Advanced Configuration
Custom JSON Options
[assembly: LambdaSerializer(typeof(PowertoolsKafkaJsonSerializer))]
// In your startup or configuration
var jsonOptions = new JsonSerializerOptions
{
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
PropertyNameCaseInsensitive = true,
WriteIndented = true,
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull
};
var serializer = new PowertoolsKafkaJsonSerializer(jsonOptions);
AOT-Compatible Serialization
[JsonSerializable(typeof(ConsumerRecords<string, Customer>))]
[JsonSerializable(typeof(Customer))]
public partial class MyJsonContext : JsonSerializerContext { }
[assembly: LambdaSerializer(typeof(PowertoolsKafkaJsonSerializer))]
// Configure with AOT context
var serializer = new PowertoolsKafkaJsonSerializer(MyJsonContext.Default);
Complex Object Handling
public class Order
{
[JsonPropertyName("id")]
public string Id { get; set; } = "";
[JsonPropertyName("customer")]
public Customer Customer { get; set; } = new();
[JsonPropertyName("items")]
public List<OrderItem> Items { get; set; } = new();
[JsonPropertyName("total")]
public decimal Total { get; set; }
[JsonPropertyName("created_at")]
public DateTime CreatedAt { get; set; }
}
public class Function
{
public void Handler(ConsumerRecords<string, Order> records, ILambdaContext context)
{
foreach (var record in records)
{
Order order = record.Value;
context.Logger.LogInformation($"Order {order.Id} from {order.Customer.Name}");
context.Logger.LogInformation($"Total: ${order.Total:F2}, Items: {order.Items.Count}");
}
}
}
Requirements
- .NET 6.0+: This library targets .NET 6.0 and later versions
- System.Text.Json: Uses the high-performance JSON library from .NET
- JSON Serializable Types: Your data classes should be compatible with System.Text.Json
- AWS Lambda: Designed specifically for AWS Lambda runtime environments
JSON Serialization Best Practices
Property Naming
// Use JsonPropertyName for explicit mapping
public class Product
{
[JsonPropertyName("product_id")]
public string ProductId { get; set; } = "";
[JsonPropertyName("display_name")]
public string DisplayName { get; set; } = "";
}
// Or configure global naming policy
var options = new JsonSerializerOptions
{
PropertyNamingPolicy = JsonNamingPolicy.SnakeCaseLower
};
Handling Nullable Types
public class Customer
{
[JsonPropertyName("id")]
public string Id { get; set; } = "";
[JsonPropertyName("email")]
public string? Email { get; set; } // Nullable reference type
[JsonPropertyName("age")]
public int? Age { get; set; } // Nullable value type
}
Custom Converters
public class DateTimeConverter : JsonConverter<DateTime>
{
public override DateTime Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
return DateTime.Parse(reader.GetString()!);
}
public override void Write(Utf8JsonWriter writer, DateTime value, JsonSerializerOptions options)
{
writer.WriteStringValue(value.ToString("yyyy-MM-ddTHH:mm:ssZ"));
}
}
// Register the converter
var options = new JsonSerializerOptions();
options.Converters.Add(new DateTimeConverter());
Error Handling
The serializer provides detailed error messages for common issues:
// JSON parsing errors
JsonException: "The JSON value could not be converted to [Type]. Path: [path] | LineNumber: [line] | BytePositionInLine: [position]."
// Type conversion errors
SerializationException: "Failed to deserialize value data: [specific error details]"
Performance Optimization
Source Generation (AOT)
[JsonSerializable(typeof(Customer))]
[JsonSerializable(typeof(Order))]
[JsonSerializable(typeof(ConsumerRecords<string, Customer>))]
[JsonSerializable(typeof(ConsumerRecords<string, Order>))]
[JsonSourceGenerationOptions(
PropertyNamingPolicy = JsonKnownNamingPolicy.CamelCase,
WriteIndented = false,
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull)]
public partial class AppJsonContext : JsonSerializerContext { }
Memory Optimization
// Configure for minimal memory allocation
var options = new JsonSerializerOptions
{
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
DefaultBufferSize = 4096, // Adjust based on typical message size
MaxDepth = 32 // Prevent deep recursion
};
Compatibility Notes
- AOT Support: Full support for Native AOT when using source generation
- Trimming: Compatible with IL trimming when properly configured
- Performance: Optimized for high-throughput Lambda scenarios
- Memory Usage: Efficient memory allocation patterns for serverless environments
Migration from Newtonsoft.Json
If migrating from Newtonsoft.Json, consider these differences:
// Newtonsoft.Json attribute
[JsonProperty("customer_name")]
public string CustomerName { get; set; }
// System.Text.Json equivalent
[JsonPropertyName("customer_name")]
public string CustomerName { get; set; }
Related Packages
- AWS.Lambda.Powertools.Kafka.Avro - Avro serialization
- AWS.Lambda.Powertools.Kafka.Protobuf - Protobuf serialization
- AWS.Lambda.Powertools.Logging - Structured logging
- AWS.Lambda.Powertools.Tracing - Distributed tracing
Documentation
For more detailed documentation and examples, visit the official documentation.
License
This library is licensed under the Apache License 2.0.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
-
net8.0
- Amazon.Lambda.Core (>= 2.5.0)
- AspectInjector (>= 2.8.1)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.