diff --git a/src/Abc.Zebus.Tests/Routing/BindingKeyTests.cs b/src/Abc.Zebus.Tests/Routing/BindingKeyTests.cs index 2620b40a..1feb13f8 100644 --- a/src/Abc.Zebus.Tests/Routing/BindingKeyTests.cs +++ b/src/Abc.Zebus.Tests/Routing/BindingKeyTests.cs @@ -1,6 +1,7 @@ using System; using Abc.Zebus.Routing; using Abc.Zebus.Testing.Extensions; +using Newtonsoft.Json; using NUnit.Framework; namespace Abc.Zebus.Tests.Routing @@ -15,5 +16,14 @@ public void should_use_special_char_for_empty_binding_key() empty.ToString().ShouldEqual("#"); } + + [Test] + public void should_serialize_parts_to_json() + { + var bindingKey = new BindingKey("foobar", "*"); + var json = JsonConvert.SerializeObject(bindingKey); + json.ShouldContain("foobar"); + json.ShouldContain("*"); + } } } diff --git a/src/Abc.Zebus/Routing/BindingKey.cs b/src/Abc.Zebus/Routing/BindingKey.cs index fb84c8d4..5bcb313b 100644 --- a/src/Abc.Zebus/Routing/BindingKey.cs +++ b/src/Abc.Zebus/Routing/BindingKey.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using Abc.Zebus.Util.Extensions; using JetBrains.Annotations; +using Newtonsoft.Json; using ProtoBuf; namespace Abc.Zebus.Routing @@ -12,12 +13,12 @@ namespace Abc.Zebus.Routing [ProtoContract] public readonly struct BindingKey : IEquatable { - public static readonly BindingKey Empty = new BindingKey(); + public static readonly BindingKey Empty = new(); [ProtoMember(1, IsRequired = true)] private readonly string[]? _parts; - public BindingKey(params string[] parts) + public BindingKey(params string[]? parts) { if (parts == null || parts.Length == 0) _parts = null; @@ -25,8 +26,13 @@ public BindingKey(params string[] parts) _parts = parts; } + [JsonProperty] + public IReadOnlyList Parts => _parts ?? Array.Empty(); + + [JsonIgnore] public int PartCount => _parts?.Length ?? 0; + [JsonIgnore] public bool IsEmpty => _parts == null || _parts.Length == 1 && IsSharp(0); public bool IsSharp(int index)