public void NestedTypeDeserializeTest() { var settings = new JsonSerializerSettings(); JsonConvert.DefaultSettings = () => settings; settings.Converters.Add(JsonSubtypesWithPropertyConverterBuilder .Of(typeof(Animal)) .RegisterSubtypeWithProperty(typeof(Cat), "catLives") .RegisterSubtypeWithProperty(typeof(Dog), "CanBark") // Shark is not registered .RegisterSubtypeWithProperty(typeof(HammerheadShark), "hammerSize") .Build()); var json = "{\"age\":11,\"fins\":3,\"teethRows\":4,\"hammerSize\":42.1}"; var result = JsonConvert.DeserializeObject <Animal>(json); Assert.AreEqual(typeof(HammerheadShark), result.GetType()); Assert.AreEqual(11, result.Age); Assert.AreEqual(3u, (result as Fish)?.FinCount); Assert.AreEqual(4u, (result as Shark)?.TeethRows); Assert.AreEqual(42.1f, (result as HammerheadShark)?.HammerSize); }
public void TestIfNestedObjectIsSerialized2() { var settings = new JsonSerializerSettings(); JsonConvert.DefaultSettings = () => settings; settings.Converters.Add(JsonSubtypesWithPropertyConverterBuilder .Of(typeof(IExpression2)) .RegisterSubtypeWithProperty(typeof(ConstantExpression2), "Value") .RegisterSubtypeWithProperty(typeof(BinaryExpression2), "SubExpressionB") .Build()); var target = JsonConvert.SerializeObject(new BinaryExpression2 { SubExpressionA = new ConstantExpression2 { Value = "A" }, SubExpressionB = new ConstantExpression2 { Value = "B" } }); Assert.AreEqual("{" + "\"SubExpressionA\":{\"Value\":\"A\"}," + "\"SubExpressionB\":{\"Value\":\"B\"}" + "}", target); }
public void ItRefuseToRegisterTwiceWithTheSameValue() { var jsonSubtypesConverterBuilder = JsonSubtypesWithPropertyConverterBuilder .Of(typeof(Animal)) .RegisterSubtypeWithProperty(typeof(Cat), "catLives"); Assert.Throws <ArgumentException>(() => jsonSubtypesConverterBuilder.RegisterSubtypeWithProperty(typeof(Cat), "catLives")); }
public void TestNestedObjectInBothWayParallel() { var settings = new JsonSerializerSettings(); JsonConvert.DefaultSettings = () => settings; settings.Converters.Add(JsonSubtypesWithPropertyConverterBuilder .Of(typeof(IExpression2)) .RegisterSubtypeWithProperty(typeof(ConstantExpression2), "Value") .RegisterSubtypeWithProperty(typeof(BinaryExpression2), "SubExpressionB") .RegisterSubtypeWithProperty(typeof(ManyOrExpression2), "OrExpr") .Build()); Action test = () => { var target = JsonConvert.SerializeObject(new BinaryExpression2 { SubExpressionA = new ManyOrExpression2 { OrExpr = new List <IExpression2> { new ConstantExpression2 { Value = "A" }, new ConstantExpression2 { Value = "B" } } }, SubExpressionB = new ManyOrExpression2 { OrExpr = new List <IExpression2> { new ConstantExpression2 { Value = "A" }, new ManyOrExpression2 { OrExpr = new List <IExpression2> { new ConstantExpression2 { Value = "A" }, new ConstantExpression2 { Value = "B" } } } } } }); var json = "{" + "\"SubExpressionA\":{\"OrExpr\":[{\"Value\":\"A\"},{\"Value\":\"B\"}]}," + "\"SubExpressionB\":{\"OrExpr\":[{\"Value\":\"A\"},{\"OrExpr\":[{\"Value\":\"A\"},{\"Value\":\"B\"}]}]}" + "}"; Assert.AreEqual(json, target); Assert.AreEqual(json, JsonConvert.SerializeObject(JsonConvert.DeserializeObject <IExpression2>(json))); }; Parallel.For(0, 100, index => test()); }
public void TestFallBack() { var settings = new JsonSerializerSettings(); JsonConvert.DefaultSettings = () => settings; settings.Converters.Add(JsonSubtypesWithPropertyConverterBuilder .Of(typeof(IExpression)) .SetFallbackSubtype(typeof(UnknownExpression)) .RegisterSubtypeWithProperty(typeof(ConstantExpression), "Value") .Build()); var expr = JsonConvert.DeserializeObject <IExpression>("{\"Any\": \"False\"}"); Assert.AreEqual("False", (expr as UnknownExpression)?.Any); }
public void Init() { settings = new JsonSerializerSettings(); JsonConvert.DefaultSettings = () => settings; settings.Converters.Add(JsonSubtypesConverterBuilder .Of(typeof(Payload), Payload.PAYLOAD_KIND) .RegisterSubtype(typeof(Game), PayloadDiscriminator.GAME) .RegisterSubtype(typeof(Com), PayloadDiscriminator.COM) .Build()); settings.Converters.Add(JsonSubtypesWithPropertyConverterBuilder .Of(typeof(Game)) .RegisterSubtypeWithProperty(typeof(Walk), nameof(Walk.FootCount)) .RegisterSubtypeWithProperty(typeof(Ride), nameof(Ride.WheelCount)) .Build()); }
public void RegisterWithGeneric() { var settings = new JsonSerializerSettings(); JsonConvert.DefaultSettings = () => settings; settings.Converters.Add(JsonSubtypesWithPropertyConverterBuilder .Of <Animal>() .RegisterSubtypeWithProperty <Cat>("catLives") .RegisterSubtypeWithProperty <Dog>("CanBark") .Build()); var json = "{\"catLives\":11,}"; var result = JsonConvert.DeserializeObject <Animal>(json); Assert.AreEqual(typeof(Cat), result.GetType()); }
public void TestIfNestedObjectIsDeserialized2() { var settings = new JsonSerializerSettings(); JsonConvert.DefaultSettings = () => settings; settings.Converters.Add(JsonSubtypesWithPropertyConverterBuilder .Of(typeof(IExpression2)) .RegisterSubtypeWithProperty(typeof(ConstantExpression2), "Value") .RegisterSubtypeWithProperty(typeof(BinaryExpression2), "SubExpressionB") .Build()); var binary = JsonConvert.DeserializeObject <IExpression2>("{" + "\"SubExpressionA\":{\"Value\":\"A\"}," + "\"SubExpressionB\":{\"Value\":\"B\"}" + "}", settings); Assert.AreEqual(typeof(ConstantExpression2), (binary as BinaryExpression2)?.SubExpressionA.GetType()); }
public HttpBackend(Uri apiUrl, string userId) { _jsonSerializer = new JsonSerializer(); _client = new HttpClient(); _baseUrl = apiUrl; _userId = userId; ServicePointManager.ServerCertificateValidationCallback += AcceptCertificate; _jsonSerializer.Converters.Add(JsonSubtypesConverterBuilder .Of(typeof(IEvent), @"type") .RegisterSubtype(typeof(PresenceEvent), PresenceEvent.ToJsonString()) .RegisterSubtype(typeof(ReceiptEvent), ReceiptEvent.ToJsonString()) .RegisterSubtype(typeof(StateEvent <RoomAvatarContent>), EventKind.RoomAvatar.ToJsonString()) .RegisterSubtype(typeof(StateEvent <RoomCanonicalAliasContent>), EventKind.RoomCanonicalAlias.ToJsonString()) .RegisterSubtype(typeof(StateEvent <RoomCreateContent>), EventKind.RoomCreate.ToJsonString()) .RegisterSubtype(typeof(StateEvent <RoomGuestAccessContent>), EventKind.RoomGuestAccess.ToJsonString()) .RegisterSubtype(typeof(StateEvent <RoomHistoryVisibilityContent>), EventKind.RoomHistoryVisibility.ToJsonString()) .RegisterSubtype(typeof(StateEvent <RoomJoinRulesContent>), EventKind.RoomJoinRule.ToJsonString()) .RegisterSubtype(typeof(StateEvent <RoomMembershipContent>), EventKind.RoomMembership.ToJsonString()) .RegisterSubtype(typeof(RoomEvent <IMessageContent>), EventKind.RoomMessage.ToJsonString()) .RegisterSubtype(typeof(StateEvent <RoomNameContent>), EventKind.RoomName.ToJsonString()) .RegisterSubtype(typeof(StateEvent <RoomPinnedEventsContent>), EventKind.RoomPinnedEvents.ToJsonString()) .RegisterSubtype(typeof(StateEvent <RoomPowerLevelsContent>), EventKind.RoomPowerLevels.ToJsonString()) .RegisterSubtype(typeof(RoomEvent <RedactionContent>), EventKind.RoomRedaction.ToJsonString()) .RegisterSubtype(typeof(StateEvent <RoomServerAclContent>), EventKind.RoomServerAcl.ToJsonString()) .RegisterSubtype(typeof(StateEvent <RoomThirdPartyInviteContent>), EventKind.RoomThirdPartyInvite.ToJsonString()) .RegisterSubtype(typeof(StateEvent <RoomTombstoneContent>), EventKind.RoomTombstone.ToJsonString()) .RegisterSubtype(typeof(StateEvent <RoomTopicContent>), EventKind.RoomTopic.ToJsonString()) .SerializeDiscriminatorProperty() .Build()); _jsonSerializer.Converters.Add(JsonSubtypesConverterBuilder .Of(typeof(IStrippedState), @"type") .RegisterSubtype(typeof(StrippedState <RoomAvatarContent>), EventKind.RoomAvatar.ToJsonString()) .RegisterSubtype(typeof(StrippedState <RoomCanonicalAliasContent>), EventKind.RoomCanonicalAlias.ToJsonString()) .RegisterSubtype(typeof(StrippedState <RoomCreateContent>), EventKind.RoomCreate.ToJsonString()) .RegisterSubtype(typeof(StrippedState <RoomGuestAccessContent>), EventKind.RoomGuestAccess.ToJsonString()) .RegisterSubtype(typeof(StrippedState <RoomHistoryVisibilityContent>), EventKind.RoomHistoryVisibility.ToJsonString()) .RegisterSubtype(typeof(StrippedState <RoomJoinRulesContent>), EventKind.RoomJoinRule.ToJsonString()) .RegisterSubtype(typeof(StrippedState <RoomMembershipContent>), EventKind.RoomMembership.ToJsonString()) .RegisterSubtype(typeof(StrippedState <RoomNameContent>), EventKind.RoomName.ToJsonString()) .RegisterSubtype(typeof(StrippedState <RoomPinnedEventsContent>), EventKind.RoomPinnedEvents.ToJsonString()) .RegisterSubtype(typeof(StrippedState <RoomPowerLevelsContent>), EventKind.RoomPowerLevels.ToJsonString()) .RegisterSubtype(typeof(StrippedState <RoomServerAclContent>), EventKind.RoomServerAcl.ToJsonString()) .RegisterSubtype(typeof(StrippedState <RoomThirdPartyInviteContent>), EventKind.RoomThirdPartyInvite.ToJsonString()) .RegisterSubtype(typeof(StrippedState <RoomTombstoneContent>), EventKind.RoomTombstone.ToJsonString()) .RegisterSubtype(typeof(StrippedState <RoomTopicContent>), EventKind.RoomTopic.ToJsonString()) .SerializeDiscriminatorProperty() .Build()); _jsonSerializer.Converters.Add(new AuthKindJsonConverter()); _jsonSerializer.Converters.Add(new ErrorCodeJsonConverter()); _jsonSerializer.Converters.Add(new EventKindJsonConverter()); _jsonSerializer.Converters.Add(new GuestAccessJsonConverter()); _jsonSerializer.Converters.Add(new HistoryVisibilityJsonConverter()); _jsonSerializer.Converters.Add(new JoinRuleJsonConverter()); _jsonSerializer.Converters.Add(new MembershipStateJsonConverter()); _jsonSerializer.Converters.Add(new MessageKindJsonConverter()); _jsonSerializer.Converters.Add(new PresenceStateJsonConverter()); _jsonSerializer.Converters.Add(new ClientServerVersionJsonConverter()); _jsonSerializer.Converters.Add(new RoomsVersionsJsonConverter()); _jsonSerializer.Converters.Add(JsonSubtypesWithPropertyConverterBuilder .Of(typeof(IEventContent)) .RegisterSubtypeWithProperty(typeof(PresenceContent), @"presence") .Build()); //_jsonSerializer.Converters.Add(JsonSubtypesWithPropertyConverterBuilder // .Of(typeof(IRoomContent)) // .RegisterSubtypeWithProperty(typeof(PresenceContent), @"presence") // .Build()); _jsonSerializer.Converters.Add(JsonSubtypesConverterBuilder .Of(typeof(IMessageContent), @"msgtype") .RegisterSubtype(typeof(AudioMessageContent), MessageKind.Audio.ToJsonString()) .RegisterSubtype(typeof(EmoteMessageContent), MessageKind.Emote.ToJsonString()) .RegisterSubtype(typeof(FileMessageContent), MessageKind.File.ToJsonString()) .RegisterSubtype(typeof(ImageMessageContent), MessageKind.Image.ToJsonString()) .RegisterSubtype(typeof(LocationMessageContent), MessageKind.Location.ToJsonString()) .RegisterSubtype(typeof(NoticeMessageContent), MessageKind.Notice.ToJsonString()) .RegisterSubtype(typeof(ServerNoticeMessageContent), MessageKind.ServerNotice.ToJsonString()) .RegisterSubtype(typeof(TextMessageContent), MessageKind.Text.ToJsonString()) //.RegisterSubtype(typeof(VideoMessageEventContent), MessageKind.Video.ToJsonString()) .Build()); _jsonSerializer.Converters.Add(JsonSubtypesWithPropertyConverterBuilder .Of(typeof(IStateContent)) .RegisterSubtypeWithProperty(typeof(RoomCanonicalAliasContent), @"alias") .RegisterSubtypeWithProperty(typeof(RoomCanonicalAliasContent), @"alt_aliases") .RegisterSubtypeWithProperty(typeof(RoomAvatarContent), @"url") .RegisterSubtypeWithProperty(typeof(RoomCreateContent), @"creator") .RegisterSubtypeWithProperty(typeof(RoomGuestAccessContent), @"guest_access") .RegisterSubtypeWithProperty(typeof(RoomHistoryVisibilityContent), @"history_visibility") .RegisterSubtypeWithProperty(typeof(RoomJoinRulesContent), @"join_rule") .RegisterSubtypeWithProperty(typeof(RoomMembershipContent), @"membership") .RegisterSubtypeWithProperty(typeof(RoomNameContent), @"name") .RegisterSubtypeWithProperty(typeof(RoomPinnedEventsContent), @"pinned") .RegisterSubtypeWithProperty(typeof(RedactionContent), @"redacts") .RegisterSubtypeWithProperty(typeof(RoomThirdPartyInviteContent), @"key_validity_url") .RegisterSubtypeWithProperty(typeof(RoomTombstoneContent), @"replacement_room") .RegisterSubtypeWithProperty(typeof(RoomTopicContent), @"topic") .Build()); _jsonSerializer.Converters.Add(JsonSubtypesConverterBuilder .Of(typeof(IRequest), @"type") .RegisterSubtype(typeof(PasswordAuthRequest <>), AuthKind.Password.ToJsonString()) .RegisterSubtype(typeof(TokenAuthRequest), AuthKind.Token.ToJsonString()) .SerializeDiscriminatorProperty() .Build()); _jsonSerializer.Converters.Add(JsonSubtypesConverterBuilder .Of(typeof(IAuthIdentifier), @"type") .RegisterSubtype(typeof(UserAuthIdentifier), UserAuthIdentifier.ToJsonString()) .RegisterSubtype(typeof(ThirdPartyAuthIdentifier), ThirdPartyAuthIdentifier.ToJsonString()) .RegisterSubtype(typeof(PhoneAuthIdentifier), PhoneAuthIdentifier.ToJsonString()) .SerializeDiscriminatorProperty() .Build()); }