using System.Collections.Concurrent;
using System.Collections.Generic;
public static void Main()
var dict = new ConcurrentDictionary<string, object>();
dict.TryAdd("researchReason", "NotPreviouslyResearched");
var json = JsonSerializer.Serialize(dict);
Console.WriteLine("Serialized JSON:");
var deserialized = JsonSerializer.Deserialize<ConcurrentDictionary<string, object>>(json);
var debugDump = new Dictionary<string, object>();
foreach (var kvp in deserialized)
if (kvp.Value is JsonElement je)
ValueKind = (int)je.ValueKind
debugDump[kvp.Key] = kvp.Value;
var debugJson = JsonSerializer.Serialize(debugDump, new JsonSerializerOptions { WriteIndented = true });
Console.WriteLine("Debug Output:");
Console.WriteLine(debugJson);
var newDict = dict.FilterAndCombine(new Dictionary<string,object>());
var fixedd = JsonSerializer.Serialize(newDict, new JsonSerializerOptions { WriteIndented = true });
Console.Write("Fixed: ");
Console.WriteLine(fixedd);
public static class EventBusCore_ConcurrentDictionaryExtensions
public static Dictionary<string, object> FilterAndCombine(this ConcurrentDictionary<string, object> metadata, Dictionary<string, object> payload)
var result = payload.ToDictionary(x => x.Key, x => x.Value);
foreach (var item in metadata.Where(kvp => !kvp.Key.EndsWith("-Status")))
result[item.Key] = item.Value is JsonElement element
? element.ValueKind switch
JsonValueKind.String => element.GetString(),
JsonValueKind.Number => element.TryGetInt64(out long l) ? l : element.GetDouble(),
JsonValueKind.True => true,
JsonValueKind.False => false,
JsonValueKind.Null => null,
_ => element.GetRawText()