Skip to content

Commit

Permalink
bug: fix serialization
Browse files Browse the repository at this point in the history
  • Loading branch information
bnayae committed Jul 2, 2023
1 parent 492b5cf commit 1e11c2e
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@ async Task<bool> HandleBatchBreakerAsync(CancellationToken ct)
}
else
{
meta = JsonSerializer.Deserialize<Metadata>(metaJson, EventSourceOptions.FullSerializerOptions) ?? throw new EventSourcingException(nameof(Metadata));
meta = JsonSerializer.Deserialize<Metadata>(metaJson, EventSourceOptions.SerializerOptions) ?? throw new EventSourcingException(nameof(Metadata));
meta = meta with { EventKey = eventKey };

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public async ValueTask<string> SendAsync(

#region var entries = new NameValueEntry[]{...}

string metaJson = JsonSerializer.Serialize(meta, EventSourceOptions.FullSerializerOptions);
string metaJson = JsonSerializer.Serialize(meta, EventSourceOptions.SerializerOptions);

// local method
NameValueEntry KV(RedisValue key, RedisValue value) => new NameValueEntry(key, value);
Expand Down
19 changes: 18 additions & 1 deletion EventSourcing.Backbone.Abstractions/BucketJsonConverter.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Text.Json;
using System.ComponentModel;
using System.Text.Json;
using System.Text.Json.Serialization;

using static EventSourcing.Backbone.EventSourceOptions;
Expand Down Expand Up @@ -70,6 +71,22 @@ public override void Write(

#endregion // Write


#region SerializerOptions

internal static readonly JsonSerializerOptions SerializerOptions = new JsonSerializerOptions
{
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
DictionaryKeyPolicy = JsonNamingPolicy.CamelCase,
Converters =
{
EnumConvertor,
JsonMemoryBytesConverterFactory.Default
}
};

#endregion // SerializerOptions

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,4 @@ public static class EventSourceConstants
};

public const string TELEMETRY_SOURCE = "evt-src";
public const string REDIS_TELEMETRY_SOURCE = $"{EventSourceConstants.TELEMETRY_SOURCE}-redis";
}
55 changes: 17 additions & 38 deletions EventSourcing.Backbone.Abstractions/EventSourceOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,56 +8,35 @@ namespace EventSourcing.Backbone
/// </summary>
public record EventSourceOptions
{
private static readonly IDataSerializer DEFAULT_SERIALIZER = new JsonDataSerializer(FullSerializerOptions);
private static readonly JsonStringEnumConverter EnumConvertor = new JsonStringEnumConverter(JsonNamingPolicy.CamelCase);
private static readonly IDataSerializer DEFAULT_SERIALIZER;
internal static readonly JsonStringEnumConverter EnumConvertor = new JsonStringEnumConverter(JsonNamingPolicy.CamelCase);
public static readonly JsonSerializerOptions SerializerOptions;

#region TelemetryLevel

/// <summary>
/// Gets the telemetry level.
/// </summary>
public TelemetryLevel TelemetryLevel { get; init; } = TelemetryLevel.Default;

#endregion // TelemetryLevel

#region SerializerOptions

internal static readonly JsonSerializerOptions SerializerOptions = new JsonSerializerOptions
static EventSourceOptions()
{
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
DictionaryKeyPolicy = JsonNamingPolicy.CamelCase,
Converters =
{
EnumConvertor,
JsonMemoryBytesConverterFactory.Default
}
};

#endregion // SerializerOptions

#region FullSerializerOptions

public static readonly JsonSerializerOptions FullSerializerOptions = new JsonSerializerOptions
{
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
DictionaryKeyPolicy = JsonNamingPolicy.CamelCase,
Converters =
var serializerOptions = new JsonSerializerOptions
{
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
DictionaryKeyPolicy = JsonNamingPolicy.CamelCase,
Converters =
{
EnumConvertor,
JsonMemoryBytesConverterFactory.Default,
JsonBucketConverter.Default
}
};

#endregion // FullSerializerOptions
};
SerializerOptions = serializerOptions;
DEFAULT_SERIALIZER = new JsonDataSerializer(serializerOptions);
}

#region Serializer
/// <summary>
/// Gets the telemetry level.
/// </summary>
public TelemetryLevel TelemetryLevel { get; init; } = TelemetryLevel.Default;

/// <summary>
/// Gets the serializer.
/// </summary>
public IDataSerializer Serializer { get; init; } = DEFAULT_SERIALIZER;

#endregion // Serializer
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,7 @@ public static OpenTelemetryBuilder WithEventSourcingTracing(
.WithTracing(tracerProviderBuilder =>
{
var sources = new[] { (string)env,
EventSourceConstants.TELEMETRY_SOURCE,
EventSourceConstants.REDIS_TELEMETRY_SOURCE };
EventSourceConstants.TELEMETRY_SOURCE };

tracerProviderBuilder
.AddSource(sources)
Expand Down
2 changes: 0 additions & 2 deletions Tests/EventSourcing.Backbone.UnitTests/SerializationTests.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
using System.Text.Json;

using Xunit;

namespace EventSourcing.Backbone
Expand Down

0 comments on commit 1e11c2e

Please sign in to comment.