|
7 | 7 |
|
8 | 8 | using System;
|
9 | 9 | using System.Collections.Concurrent;
|
| 10 | +using System.Collections.Generic; |
10 | 11 | using System.IO;
|
11 | 12 | using System.Reflection;
|
12 | 13 | using System.Runtime.Serialization;
|
|
21 | 22 | using Akka.Util;
|
22 | 23 | using Akka.Util.Reflection;
|
23 | 24 | using FluentAssertions;
|
| 25 | +using Newtonsoft.Json.Linq; |
24 | 26 | using Xunit;
|
25 | 27 |
|
26 | 28 | namespace Akka.Tests.Serialization
|
@@ -608,6 +610,60 @@ public void Missing_custom_serializer_id_should_append_help_message()
|
608 | 610 | .Where(ex => ex.Message.Contains("Serializer Id [101] is not one of the internal Akka.NET serializer."));
|
609 | 611 | }
|
610 | 612 |
|
| 613 | + [Fact(DisplayName = "Should be able to serialize object property with JObject value")] |
| 614 | + public void ObjectPropertyJObjectTest() |
| 615 | + { |
| 616 | + var serializer = (NewtonSoftJsonSerializer) Sys.Serialization.FindSerializerForType(typeof(object)); |
| 617 | + var obj = JObject.FromObject(new |
| 618 | + { |
| 619 | + FormattedMessage = "We are apple 20 points above value 10.01 ms", |
| 620 | + Message = "We are {0} {1} points above value {2} ms", |
| 621 | + Parameters = new List<object> { "apple", 20, 10.01F, 50L, (decimal) 9.9 }, |
| 622 | + MessageType = 200 |
| 623 | + }); |
| 624 | + var instance = new ObjectTestClass { MyObject = obj}; |
| 625 | + |
| 626 | + var serialized = serializer.ToBinary(instance); |
| 627 | + |
| 628 | + // Stack overflowed in the original bug |
| 629 | + var deserialized = serializer.FromBinary<ObjectTestClass>(serialized); |
| 630 | + deserialized.MyObject.Should().BeOfType<JObject>(); |
| 631 | + var jObj = (JObject) deserialized.MyObject; |
| 632 | + |
| 633 | + ((JValue)jObj["FormattedMessage"]).Value.Should().Be("We are apple 20 points above value 10.01 ms"); |
| 634 | + ((JValue)jObj["Message"]).Value.Should().Be("We are {0} {1} points above value {2} ms"); |
| 635 | + var arr = ((JArray)jObj["Parameters"]); |
| 636 | + ((JValue)arr[0]).Value.Should().Be("apple"); |
| 637 | + ((JValue)arr[1]).Value.Should().BeOfType<int>(); |
| 638 | + ((JValue)arr[1]).Value.Should().Be(20); |
| 639 | + ((JValue)arr[2]).Value.Should().BeOfType<float>(); |
| 640 | + ((JValue)arr[2]).Value.Should().Be(10.01F); |
| 641 | + ((JValue)arr[3]).Value.Should().BeOfType<long>(); |
| 642 | + ((JValue)arr[3]).Value.Should().Be(50L); |
| 643 | + ((JValue)arr[4]).Value.Should().BeOfType<decimal>(); |
| 644 | + ((JValue)arr[4]).Value.Should().Be((decimal)9.9); |
| 645 | + ((JValue)jObj["MessageType"]).Value.Should().Be(200); |
| 646 | + } |
| 647 | + |
| 648 | + [Fact(DisplayName = "Should be able to serialize object property with anonymous type value")] |
| 649 | + public void ObjectPropertyObjectTest() |
| 650 | + { |
| 651 | + var serializer = (NewtonSoftJsonSerializer) Sys.Serialization.FindSerializerForType(typeof(object)); |
| 652 | + var obj = new |
| 653 | + { |
| 654 | + FormattedMessage = "We are apple 20 points above value 10.01 ms", |
| 655 | + Message = "We are {0} {1} points above value {2} ms", |
| 656 | + Parameters = new List<object> { "apple", 20, 10.01F, 50L, (decimal) 9.9 }, |
| 657 | + MessageType = 200 |
| 658 | + }; |
| 659 | + var instance = new ObjectTestClass { MyObject = obj}; |
| 660 | + |
| 661 | + var serialized = serializer.ToBinary(instance); |
| 662 | + |
| 663 | + var deserialized = serializer.FromBinary<ObjectTestClass>(serialized); |
| 664 | + deserialized.MyObject.Should().BeEquivalentTo(obj); |
| 665 | + } |
| 666 | + |
611 | 667 | public SerializationSpec():base(GetConfig())
|
612 | 668 | {
|
613 | 669 | }
|
@@ -708,6 +764,11 @@ public sealed class ChildClass
|
708 | 764 | public string Value { get; set; }
|
709 | 765 | }
|
710 | 766 | }
|
| 767 | + |
| 768 | + public sealed class ObjectTestClass |
| 769 | + { |
| 770 | + public object MyObject { get; set; } |
| 771 | + } |
711 | 772 | }
|
712 | 773 | }
|
713 | 774 |
|
0 commit comments