public void EnumDiscriminatorPropertySupportNamingStrategy() { var serializerSettings = new JsonSerializerSettings { ContractResolver = new DefaultContractResolver { NamingStrategy = new SnakeCaseNamingStrategy(), }, Converters = new List <JsonConverter> { new StringEnumConverter { NamingStrategy = new SnakeCaseNamingStrategy() }, JsonSubtypesConverterBuilder .Of(typeof(IMyType), "enum_value") .RegisterSubtype(typeof(MyTypeOne), EnumType.EnumMemberOne) .RegisterSubtype(typeof(MyTypeTwo), EnumType.EnumMemberTwo) .Build() } }; var json = "{\"enum_value\":\"enum_member_one\"}"; var result = JsonConvert.DeserializeObject <IMyType>(json, serializerSettings); var serializeObject = JsonConvert.SerializeObject(result, serializerSettings); Assert.AreEqual(json, serializeObject); }
public void CheckDeserializeFirst() { var settings = new JsonSerializerSettings(); JsonConvert.DefaultSettings = () => settings; settings.Converters.Add(JsonSubtypesConverterBuilder .Of(typeof(SimpleBase), "type") .SerializeDiscriminatorProperty(true) .RegisterSubtype(typeof(SimpleChildA), "TypeA") .RegisterSubtype(typeof(SimpleChildB), "TypeB") .Build()); SimpleBase test_object = new SimpleChildB() { Name = "bob", Height = 36 }; var json_first = "{\"type\":\"TypeB\",\"Height\":36,\"Name\":\"bob\"}"; var result = JsonConvert.DeserializeObject <SimpleBase>(json_first); Assert.IsInstanceOf(typeof(SimpleChildB), result); }
public void CheckExplicitLast() { var settings = new JsonSerializerSettings(); JsonConvert.DefaultSettings = () => settings; settings.Converters.Add(JsonSubtypesConverterBuilder .Of(typeof(SimpleBase), "type") .SerializeDiscriminatorProperty(false) .RegisterSubtype(typeof(SimpleChildA), "TypeA") .RegisterSubtype(typeof(SimpleChildB), "TypeB") .Build()); SimpleBase test_object = new SimpleChildA() { Name = "bob", Age = 12 }; var json_first = "{\"Age\":12,\"Name\":\"bob\",\"type\":\"TypeA\"}"; var result = JsonConvert.SerializeObject(test_object); Assert.AreEqual(json_first, result); }
public static JsonSerializerSettings AddValidatorConverter(this JsonSerializerSettings settings) { settings.Converters.Add(JsonSubtypesConverterBuilder .Of(typeof(Specification <ICheckElementViewModel>), "Type") // type property is only defined here .RegisterSubtype(typeof(CheckElement_IsChecked_Validator), nameof(CheckElement_IsChecked_Validator)) .RegisterSubtype(typeof(CheckElement_IsNotChecked_Validator), nameof(CheckElement_IsNotChecked_Validator)) .SerializeDiscriminatorProperty() // ask to serialize the type property .Build()); settings.Converters.Add(JsonSubtypesConverterBuilder .Of(typeof(Specification <ISelectElementViewModel>), "Type") // type property is only defined here .RegisterSubtype(typeof(SelectElement_SelectionMade_Validator), nameof(SelectElement_SelectionMade_Validator)) .SerializeDiscriminatorProperty() // ask to serialize the type property .Build()); settings.Converters.Add(JsonSubtypesConverterBuilder .Of(typeof(Specification <ITextElementViewModel>), "Type") // type property is only defined here .RegisterSubtype(typeof(TextElement_MaximumLength_Validator), nameof(TextElement_MaximumLength_Validator)) .RegisterSubtype(typeof(TextElement_MinimunLength_Validator), nameof(TextElement_MinimunLength_Validator)) .SerializeDiscriminatorProperty() // ask to serialize the type property .Build()); return(settings); }
public void UpdateSerializables() { original.Objects = new List <MeepleObjectSerializable>(); AddSerializable(root); var settings = new JsonSerializerSettings(); settings.ContractResolver = new CamelCasePropertyNamesContractResolver(); settings.Converters.Add(JsonSubtypesConverterBuilder .Of(typeof(MeepleObjectSerializable), "type") // type property is only defined here .RegisterSubtype(typeof(CardSerializable), "card") .RegisterSubtype(typeof(GridSerializable), "grid") .RegisterSubtype(typeof(BoardSerializable), "board") .RegisterSubtype(typeof(TextureImporterSerializable), "cardImporter") .RegisterSubtype(typeof(ObjImporterSerializable), "objImporter") .RegisterSubtype(typeof(DeckSerializable), "deck") .RegisterSubtype(typeof(MeepleButtonSerializable), "meepleButton") .RegisterSubtype(typeof(CustomObjectSerializable), "customObject") .RegisterSubtype(typeof(HandSerializable), "hand") .SerializeDiscriminatorProperty() // ask to serialize the type property .Build()); var export = JsonConvert.SerializeObject(original, settings); Debug.Log(export); File.WriteAllText($@"{path}", export); }
public void NestedTypeDeserializeTest() { var settings = new JsonSerializerSettings(); JsonConvert.DefaultSettings = () => settings; settings.Converters.Add(JsonSubtypesConverterBuilder .Of(typeof(Animal), "type") .RegisterSubtype(typeof(Cat), AnimalType.Cat) .RegisterSubtype(typeof(Dog), AnimalType.Dog) // Shark is not registered .RegisterSubtype(typeof(HammerheadShark), AnimalType.HammerheadShark) .Build()); var json = "{\"age\":11,\"fins\":3,\"teethRows\":4,\"hammerSize\":42.1,\"type\":4}"; 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 UnregisteredTypeSerializeTest2() { var settings = new JsonSerializerSettings(); JsonConvert.DefaultSettings = () => settings; settings.Converters.Add(JsonSubtypesConverterBuilder .Of(typeof(Animal), "type") .RegisterSubtype(typeof(Cat), AnimalType.Cat) .RegisterSubtype(typeof(Dog), AnimalType.Dog) // Shark is not registered .RegisterSubtype(typeof(HammerheadShark), AnimalType.HammerheadShark) .Build()); var json = "{\"age\":11,\"fins\":4,\"teethRows\":4}"; // no type property shall be added var result = JsonConvert.SerializeObject(new Shark { Age = 11, FinCount = 4, TeethRows = 4 }); Assert.AreEqual(json, result); }
public static void AddYandexKassa(this IServiceCollection services, Action <Configuration> configure) { var cfg = new Configuration(); configure(cfg); services.AddSingleton(cfg); services.AddSingleton <IPaymentService, PaymentService>(); var settings = new JsonSerializerSettings(); JsonConvert.DefaultSettings = () => settings; settings.Converters.Add(JsonSubtypesConverterBuilder .Of(typeof(PaymentMethod), "Id") .SerializeDiscriminatorProperty() .RegisterSubtype(typeof(BankCardPaymentMethod), PaymentMethodType.BankCard) .RegisterSubtype(typeof(ApplePayPaymentMethod), PaymentMethodType.ApplePay) .RegisterSubtype(typeof(GooglePayPaymentMethod), PaymentMethodType.GooglePay) .RegisterSubtype(typeof(YandexMoneyPaymentMethod), PaymentMethodType.YandexMoney) .RegisterSubtype(typeof(QiwiPaymentMethod), PaymentMethodType.Qiwi) .RegisterSubtype(typeof(WebmoneyPaymentMethod), PaymentMethodType.Webmoney) .RegisterSubtype(typeof(WeChatPaymentMethod), PaymentMethodType.WeChat) .RegisterSubtype(typeof(SberbankPaymentMethod), PaymentMethodType.Sberbank) .RegisterSubtype(typeof(AlfabankPaymentMethod), PaymentMethodType.Alfabank) .RegisterSubtype(typeof(TinkoffbankPaymentMethod), PaymentMethodType.Tinkoffbank) .RegisterSubtype(typeof(B2BSberbankPaymentMethod), PaymentMethodType.B2BSberbank) .RegisterSubtype(typeof(MobileBalancePaymentMethod), PaymentMethodType.MobileBalance) .RegisterSubtype(typeof(CashPaymentMethod), PaymentMethodType.Cash) .RegisterSubtype(typeof(InstallmentsPaymentMethod), PaymentMethodType.Installments) .Build()); }
public void TestIfNestedObjectIsSerialized2() { var settings = new JsonSerializerSettings(); JsonConvert.DefaultSettings = () => settings; settings.Converters.Add(JsonSubtypesConverterBuilder .Of(typeof(IExpression2), "Type") .SerializeDiscriminatorProperty() .RegisterSubtype(typeof(ConstantExpression2), "Constant") .RegisterSubtype(typeof(BinaryExpression2), "Binary") .Build()); var target = JsonConvert.SerializeObject(new BinaryExpression2 { SubExpressionA = new ConstantExpression2 { Value = "A" }, SubExpressionB = new ConstantExpression2 { Value = "B" } }); Assert.AreEqual("{" + "\"SubExpressionA\":{\"Value\":\"A\",\"Type\":\"Constant\"}," + "\"SubExpressionB\":{\"Value\":\"B\",\"Type\":\"Constant\"}" + ",\"Type\":\"Binary\"}", target); }
public void ConfigureServices(IServiceCollection services) { services .AddControllers() .AddNewtonsoftJson(options => { options.SerializerSettings.Converters.Add( JsonSubtypesConverterBuilder .Of(typeof(Musician), nameof(Musician.InstrumentType)) .RegisterSubtype(typeof(Musician.Guitarist), "Guitar") .RegisterSubtype(typeof(Musician.Pianist), "Piano") .SerializeDiscriminatorProperty() .Build() ); }); services.AddMvc(); services.AddSwaggerGen(c => { c.SwaggerDoc("v1", new OpenApiInfo { Title = "My API", Version = "v1" }); c.GeneratePolymorphicSchemas(discriminatorSelector: d => { if (d == typeof(Musician)) { return("instrumentType"); } return(null); }); }); services.AddSwaggerGenNewtonsoftSupport(); }
private void Awake() { _serializerSettings = new JsonSerializerSettings(); _serializerSettings.Converters.Add(JsonSubtypesConverterBuilder .Of(typeof(MeepleObjectSerializable), "type") // type property is only defined here .RegisterSubtype(typeof(CardSerializable), "card") .RegisterSubtype(typeof(GridSerializable), "grid") .RegisterSubtype(typeof(BoardSerializable), "board") .RegisterSubtype(typeof(TextureImporterSerializable), "cardImporter") .RegisterSubtype(typeof(ObjImporterSerializable), "objImporter") .RegisterSubtype(typeof(DeckSerializable), "deck") .RegisterSubtype(typeof(MeepleButtonSerializable), "meepleButton") .RegisterSubtype(typeof(CustomObjectSerializable), "customObject") .RegisterSubtype(typeof(HandSerializable), "hand") .SerializeDiscriminatorProperty() // ask to serialize the type property .Build()); _serializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver(); prefabs.Add(typeof(CardSerializable).ToString(), cardPrefab); prefabs.Add(typeof(GridSerializable).ToString(), gridPrefab); prefabs.Add(typeof(BoardSerializable).ToString(), boardPrefab); prefabs.Add(typeof(DeckSerializable).ToString(), deckPrefab); prefabs.Add(typeof(TextureImporterSerializable).ToString(), textureImporterPrefab); prefabs.Add(typeof(ObjImporterSerializable).ToString(), objImporterPrefab); prefabs.Add(typeof(MeepleButtonSerializable).ToString(), buttonPrefab); prefabs.Add(typeof(CustomObjectSerializable).ToString(), customObjectPrefab); prefabs.Add(typeof(HandSerializable).ToString(), handPrefab); }
public void Prueba() { var settings = new JsonSerializerSettings(); settings.Converters.Add(JsonSubtypesConverterBuilder .Of <Map>("Type") .RegisterSubtype <Door>(1) .SerializeDiscriminatorProperty() .Build()); //using (StreamWriter file = File.CreateText("prueba.json")) //{ // JsonSerializer serializer = new JsonSerializer(); // serializer.Serialize(file, X); //} var json = JsonConvert.SerializeObject(X, settings); var result = JsonConvert.DeserializeObject <Map>(json, settings); //List<MapElement> elements = new List<MapElement>(); //using (StreamReader file = File.OpenText("prueba.json")) //{ // var animal = JsonConvert.DeserializeObject<MapElement>(file.ReadToEnd()); // using (JsonTextReader reader = new JsonTextReader(file)) // { // JsonSerializer serializer = new JsonSerializer(); // elements = serializer.Deserialize<List<MapElement>>(reader); // } //} }
public void ShouldDeserializeNestedLevel() { var settings = new JsonSerializerSettings(); JsonConvert.DefaultSettings = () => settings; settings.Converters.Add(JsonSubtypesConverterBuilder .Of(typeof(Payload), Payload.PAYLOAD_KIND) .SerializeDiscriminatorProperty() .RegisterSubtype(typeof(Game), PayloadDiscriminator.GAME) .RegisterSubtype(typeof(Com), PayloadDiscriminator.COM) .Build()); settings.Converters.Add(JsonSubtypesConverterBuilder .Of(typeof(Game), Game.GAME_KIND) .SerializeDiscriminatorProperty() .RegisterSubtype(typeof(Walk), GameDiscriminator.WALK) .RegisterSubtype(typeof(Run), GameDiscriminator.RUN) .Build()); Payload run = new Run(); var data = JsonConvert.SerializeObject(run, settings); Assert.IsInstanceOf <Run>(JsonConvert.DeserializeObject <Payload>(data, settings)); }
public void ItRefuseToRegisterTwiceWithTheSameNullValue() { var jsonSubtypesConverterBuilder = JsonSubtypesConverterBuilder .Of(typeof(Animal), "type") .RegisterSubtype(typeof(Cat), null); Assert.Throws <ArgumentException>(() => jsonSubtypesConverterBuilder.RegisterSubtype(typeof(Dog), null)); }
public void TestNestedObjectInBothWayParallel() { var settings = new JsonSerializerSettings(); JsonConvert.DefaultSettings = () => settings; settings.Converters.Add(JsonSubtypesConverterBuilder .Of(typeof(IExpression2), "Type") .SerializeDiscriminatorProperty() .RegisterSubtype(typeof(ConstantExpression2), "Constant") .RegisterSubtype(typeof(BinaryExpression2), "Binary") .RegisterSubtype(typeof(ManyOrExpression2), "ManyOr") .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\",\"Type\":\"Constant\"},{\"Value\":\"B\",\"Type\":\"Constant\"}],\"Type\":\"ManyOr\"}," + "\"SubExpressionB\":{\"OrExpr\":[{\"Value\":\"A\",\"Type\":\"Constant\"},{\"OrExpr\":[{\"Value\":\"A\",\"Type\":\"Constant\"},{\"Value\":\"B\",\"Type\":\"Constant\"}],\"Type\":\"ManyOr\"}],\"Type\":\"ManyOr\"}" + ",\"Type\":\"Binary\"}"; Assert.AreEqual(json, target); Assert.AreEqual(json, JsonConvert.SerializeObject(JsonConvert.DeserializeObject <IExpression2>(json))); }; Parallel.For(0, 100, index => test()); }
public static IHostBuilder UseCommandHost(this IHostBuilder hostBuilder, string commandFile, string section, Assembly commandAssembly = null, JsonSerializerSettings serializerSettings = null) { var settings = serializerSettings ?? new JsonSerializerSettings(); if (commandAssembly != null) { var converterBuilder = JsonSubtypesConverterBuilder.Of(typeof(ICommand), "kind"); var commandTypes = commandAssembly.GetTypes().Where(s => s.IsAssignableTo <ICommand>()); foreach (var command in commandTypes) { converterBuilder.RegisterSubtype(command, command.Name); } settings.Converters.Add(converterBuilder.SerializeDiscriminatorProperty().Build()); } hostBuilder.UseServiceProviderFactory(new AutofacServiceProviderFactory()); s_commands = JObject.Parse(File.ReadAllText(commandFile)).GetCommandsFromConfiguration(section, settings).GetEnumerator(); hostBuilder.ConfigureServices((hostContext, services) => services.AddHostedService(p => p.GetService <ICommandProcessor>())); hostBuilder.ConfigureContainer <ContainerBuilder>((y, b) => { var commandList = new CommandList(); while (s_commands.MoveNext()) { var command = s_commands.Current; if (command == null) { return; } commandList.Add(command); b.Register(c => Convert.ChangeType(commandList.Current, command.GetType())).As(command.GetType()); } b.RegisterBuildCallback(x => AutoFacContainerProvider.Container = x); b.Register(s => commandList).SingleInstance(); b.RegisterType <CommandProcessor>().As <ICommandProcessor>().SingleInstance(); if (commandAssembly != null) { b.RegisterAssemblyTypes(commandAssembly).Where(x => x.Name.EndsWith("Handler")).Named(s => s.Name.Split("Handler").First(), typeof(ICommandHandler)); } }); return(hostBuilder); }
public static JsonSerializerSettings CreateSettings() { var stg = new JsonSerializerSettings(); stg.Converters.Add(JsonSubtypesConverterBuilder .Of(typeof(Descriptor), "Type") // type property is only defined here .RegisterSubtype <ItemDescriptor>("Item") .RegisterSubtype <EquipmentDescriptor>("Equipment") .SerializeDiscriminatorProperty() // ask to serialize the type property .Build()); return(stg); }
public static JsonSerializerSettings CreateSettings() { var stg = new JsonSerializerSettings(); stg.Converters.Add(JsonSubtypesConverterBuilder .Of(typeof(BaseShip), "Type") // type property is only defined here .RegisterSubtype <Cruiser>("Cruiser") .SerializeDiscriminatorProperty() // ask to serialize the type property .Build()); return(stg); }
public HangfireFixture() { var settings = new JsonSerializerSettings(); settings.ContractResolver = new CamelCasePropertyNamesContractResolver(); settings.Converters.Add(new StringEnumConverter(new CamelCaseNamingStrategy())); settings.Converters.Add(JsonSubtypesConverterBuilder .Of(typeof(CrawlRequestBase), "type") .RegisterSubtype(typeof(HyperlinkCrawlRequest), CrawlRequestType.Hyperlink) .SerializeDiscriminatorProperty() .Build()); JobHelper.SetSerializerSettings(settings); }
public static JsonConverter JsonConvertor() { return(JsonSubtypesConverterBuilder.Of(typeof(TransactionResponseValue), "type") .RegisterSubtype(typeof(DownloadProgress), nameof(DownloadProgress)) .RegisterSubtype(typeof(DownloadComplete), nameof(DownloadComplete)) .RegisterSubtype(typeof(InstallStarted), nameof(InstallStarted)) .RegisterSubtype(typeof(UninstallStarted), nameof(UninstallStarted)) .RegisterSubtype(typeof(TransactionProgress), nameof(TransactionProgress)) .RegisterSubtype(typeof(TransactionError), nameof(TransactionError)) .RegisterSubtype(typeof(TransactionStarted), nameof(TransactionStarted)) .RegisterSubtype(typeof(TransactionComplete), nameof(TransactionComplete)) .RegisterSubtype(typeof(TransactionQueued), nameof(TransactionQueued)) .RegisterSubtype(typeof(VerificationFailed), nameof(VerificationFailed)) .Build()); }
public void MultipleRegistrationNotAllowedWithSerializeDiscriminatorProperty() { var settings = new JsonSerializerSettings(); JsonConvert.DefaultSettings = () => settings; var exception = Assert.Throws <InvalidOperationException>(() => JsonSubtypesConverterBuilder .Of(typeof(Animal), "type") .SerializeDiscriminatorProperty() .RegisterSubtype(typeof(Shark), AnimalType.Shark) .RegisterSubtype(typeof(Shark), AnimalType.HammerheadShark) .Build()); Assert.AreEqual("Multiple discriminators on single type are not supported when discriminator serialization is enabled", exception.Message); }
public TypeHandlerService(Services services) { _services = services; _builder = JsonSubtypesConverterBuilder.Of(typeof(TypeHandlerBase), nameof(TypeHandlerBase.TypeId)); if (services?.Options?.StandardTypes != null) { foreach (var typeHandler in services.Options.StandardTypes.Select(x => x.GetType()).Distinct()) { Register(typeHandler); } } Register(typeof(UnsupportedTypeHandler)); }
public TypeHandlerService(Services services, IEnumerable <TypeHandlerBase> typeHandlers) { _services = services; _builder = JsonSubtypesConverterBuilder.Of(typeof(TypeHandlerBase), nameof(TypeHandlerBase.TypeId)); if (typeHandlers != null) { foreach (var typeHandler in typeHandlers.Select(x => x.GetType()).Distinct()) { Register(typeHandler); } } Register(typeof(UnsupportedTypeHandler)); }
private static JsonConverter CreateJsonConverter <T>(Type baseType, string discriminatorProperty, Func <string, string> classNameFactory) { var subTypes = baseType.Assembly.GetTypes().Where(x => x.IsInNamespace(baseType.Namespace !) && !x.IsAbstract && x.IsAssignableTo <T>()); var builder = JsonSubtypesConverterBuilder.Of <T>(discriminatorProperty); foreach (var type in subTypes) { var key = classNameFactory(type.Name); builder.RegisterSubtype(type, key); } return(builder.SerializeDiscriminatorProperty().Build()); }
static JsonUtils() { Settings = new JsonSerializerSettings(); Settings.Converters .Add(JsonSubtypesConverterBuilder .Of(typeof(Var), "Type") // type property is only defined here .RegisterSubtype(typeof(Var <bool>), VarType.Bool) .RegisterSubtype(typeof(Var <int>), VarType.Int) .RegisterSubtype(typeof(Var <float>), VarType.Float) .RegisterSubtype(typeof(Var <long>), VarType.Long) .RegisterSubtype(typeof(Var <string>), VarType.String) .RegisterSubtype(typeof(Var <Nuid>), VarType.Nuid) .RegisterSubtype(typeof(Var <Entity>), VarType.Entity) .RegisterSubtype(typeof(Var <NList>), VarType.List) .SerializeDiscriminatorProperty() // ask to serialize the type property .Build()); }
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(JsonSubtypesConverterBuilder .Of(typeof(Game), Game.GAME_KIND) .RegisterSubtype(typeof(Walk), GameDiscriminator.WALK) .RegisterSubtype(typeof(Run), GameDiscriminator.RUN) .Build()); }
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 static JsonSerializerSettings AddFormSpecificationConverter(this JsonSerializerSettings settings) { settings.Converters.Add(JsonSubtypesConverterBuilder .Of(typeof(Specification <FormViewModel>), "Type") // type property is only defined here .RegisterSubtype(typeof(AndSpecification <FormViewModel>), nameof(AndSpecification <FormViewModel>)) .RegisterSubtype(typeof(OrSpecification <FormViewModel>), nameof(OrSpecification <FormViewModel>)) .RegisterSubtype(typeof(NotSpecification <FormViewModel>), nameof(NotSpecification <FormViewModel>)) .RegisterSubtype(typeof(IdentitySpecification <FormViewModel>), nameof(IdentitySpecification <FormViewModel>)) .RegisterSubtype(typeof(NullSpecification <FormViewModel>), nameof(NullSpecification <FormViewModel>)) .RegisterSubtype(typeof(ElementPropertyFormSpecification), nameof(ElementPropertyFormSpecification)) .SerializeDiscriminatorProperty() // ask to serialize the type property .Build()); return(settings); }
private static JsonConverter GetSceneConverter() { var baseType = typeof(AutonomousScene); var sceneTypes = baseType.Assembly.GetTypes() .Where(x => x.IsInNamespace(baseType.Namespace !) && x.IsAssignableTo <IScene>()); var builder = JsonSubtypesConverterBuilder.Of <IScene>("type"); foreach (var sceneType in sceneTypes) { var key = sceneType.Name.Replace("Scene", null).ToCamelCase(); builder.RegisterSubtype(sceneType, key); } return(builder.SerializeDiscriminatorProperty().Build()); }
public static JsonSerializerSettings AddEventConverter(this JsonSerializerSettings settings) { settings.Converters.Add(JsonSubtypesConverterBuilder .Of(typeof(FormElementEvent), "Type") // type property is only defined here .RegisterSubtype(typeof(ButtonElementEvent), nameof(ButtonElementEvent)) .RegisterSubtype(typeof(ButtonElementClickedEvent), nameof(ButtonElementClickedEvent)) .RegisterSubtype(typeof(CheckElementEvent), nameof(CheckElementEvent)) .RegisterSubtype(typeof(CheckElementChangedEvent), nameof(CheckElementChangedEvent)) .RegisterSubtype(typeof(SelectElementEvent), nameof(SelectElementEvent)) .RegisterSubtype(typeof(SelectElementChangedEvent), nameof(SelectElementChangedEvent)) .RegisterSubtype(typeof(TextElementEvent), nameof(TextElementEvent)) .RegisterSubtype(typeof(TextElementChangedEvent), nameof(TextElementChangedEvent)) .SerializeDiscriminatorProperty() // ask to serialize the type property .Build()); return(settings); }