فهرست منبع

Serialize BindingKey contents to JSON

Lucas Trzesniewski 2 سال پیش
والد
کامیت
5d50d292a4
2فایلهای تغییر یافته به همراه18 افزوده شده و 2 حذف شده
  1. 10 0
      src/Abc.Zebus.Tests/Routing/BindingKeyTests.cs
  2. 8 2
      src/Abc.Zebus/Routing/BindingKey.cs

+ 10 - 0
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 @@ namespace Abc.Zebus.Tests.Routing
 
             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("*");
+        }
     }
 }

+ 8 - 2
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<BindingKey>
     {
-        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 @@ namespace Abc.Zebus.Routing
                 _parts = parts;
         }
 
+        [JsonProperty]
+        public IReadOnlyList<string> Parts => _parts ?? Array.Empty<string>();
+
+        [JsonIgnore]
         public int PartCount => _parts?.Length ?? 0;
 
+        [JsonIgnore]
         public bool IsEmpty => _parts == null || _parts.Length == 1 && IsSharp(0);
 
         public bool IsSharp(int index)