using DL.MessageBroker.Contracts;
using DL.Extensions.Json;
public static void Main()
var @event = new DeviceReplacedOnVehicleEvent(new DeviceReplacedDto("old-device-id", "new-device-id"), "TESTING");
.TryReplace(e => e.Device.OldDeviceId, "OldDevice", new Device("product-id", "product-name"))
.TryReplace(e => e.Device.NewDeviceId, "NewDevice", new Device("product-id", "product-name"))
Console.WriteLine(eventJson);
namespace DL.MessageBroker.Contracts
public record DeviceReplacedOnVehicleEvent(DeviceReplacedDto Device, string DataSource);
public record DeviceReplacedDto(string OldDeviceId, string NewDeviceId);
public record Device(string Id, string Name);
using System.Linq.Expressions;
public static class ExpressionExtensions
public static MemberExpression? GetMemberExpression(this Expression? expression)
Console.WriteLine($"DEBUG GetMemberExpression(): expression = {expression}");
if (expression is MemberExpression)
Console.WriteLine("DEBUG GetMemberExpression(): expression is a member expression");
return (MemberExpression)expression;
if (expression is LambdaExpression)
Console.WriteLine("DEBUG GetMemberExpression(): expression is a Lambda expression");
var lambdaExpression = (LambdaExpression)expression;
Console.WriteLine($"DEBUG GetMemberExpression(): lambdaExpression = {lambdaExpression}");
if (lambdaExpression.Body is MemberExpression)
Console.WriteLine($"DEBUG GetMemberExpression(): lambdaExpression Body is a member expression {lambdaExpression.Body}");
return (MemberExpression)lambdaExpression.Body;
if (lambdaExpression.Body is UnaryExpression)
Console.WriteLine($"DEBUG GetMemberExpression(): lambdaExpression Body is an Unary expression {((UnaryExpression)lambdaExpression.Body).Operand}");
return (MemberExpression)((UnaryExpression)lambdaExpression.Body).Operand;
public static string GetPropertyPath(this Expression expression)
var path = new StringBuilder();
var memberExpression = expression.GetMemberExpression();
while(memberExpression != null)
path.Insert(0, memberExpression.Member.Name);
memberExpression = memberExpression.Expression.GetMemberExpression();
namespace DL.Extensions.Json
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Text.Json.Nodes;
public class JsonObjectBuilder<TSource>
private readonly TSource? _source;
private readonly JsonObject? _jsonObject;
public JsonObjectBuilder(TSource? source)
_jsonObject = JsonSerializer.SerializeToNode(source)?.AsObject();
public JsonObjectBuilder<TSource> TryReplace<TProperty>(Expression<Func<TSource, TProperty>> selectorExpression,
string newPropertyName, object? newPropertyValue)
if (_jsonObject != null && newPropertyValue != null)
string oldPropertyNamePath = selectorExpression.GetPropertyPath();
var nestedProperties = oldPropertyNamePath
JsonObject? jsonObjectPointer = _jsonObject;
for (int i = 0; i < nestedProperties.Length - 1 && jsonObjectPointer != null; i++)
string parentPropertyName = nestedProperties[i];
jsonObjectPointer = jsonObjectPointer[parentPropertyName]?.AsObject();
if (jsonObjectPointer != null)
string oldPropertyName = nestedProperties[^1];
jsonObjectPointer.TryAdd(newPropertyName, JsonSerializer.SerializeToNode(newPropertyValue));
jsonObjectPointer.Remove(oldPropertyName);
public JsonObjectBuilder<TSource> TryReplace<TProperty>(Expression<Func<TSource, TProperty>> selectorExpression,
string newPropertyName, JsonNode? newPropertyValue)
if (_jsonObject != null && newPropertyValue != null)
string oldPropertyNamePath = selectorExpression.GetPropertyPath();
var nestedProperties = oldPropertyNamePath
JsonObject? jsonObjectPointer = _jsonObject;
for (int i = 0; i < nestedProperties.Length - 1 && jsonObjectPointer != null; i++)
string parentPropertyName = nestedProperties[i];
jsonObjectPointer = jsonObjectPointer[parentPropertyName]?.AsObject();
if (jsonObjectPointer != null)
string oldPropertyName = nestedProperties[^1];
_jsonObject.TryAdd(newPropertyName, newPropertyValue);
_jsonObject.Remove(oldPropertyName);
public JsonNode? ToJsonNode()
return JsonNode.Parse(ToJsonString());
public string ToJsonString(bool indented = true)
return JsonSerializer.Serialize(_source);
return _jsonObject.ToJsonString(new JsonSerializerOptions
public static class JsonSerializationExtensions
public static JsonObjectBuilder<TSource> ToJsonObjectBuilder<TSource>(this TSource? source)
return new JsonObjectBuilder<TSource>(source);