JsonSubTypes
JsonSubTypes copied to clipboard
KnownSubTypeWithProperty it would be great to support aliases
Is your feature request related to a problem? Please describe. I am trying to write a property to distinguish my objects, but the problem is the name in json doesnt match the name in object, I wrote [JsonProperty("123name")] and the property name is name123, I am forced to use this because a variable cannot start with digits.
Describe the solution you'd like It would be great to have one more constructor, with two properties: propertyName from the object and propertyName from json, in this way will be supported different names.
Describe alternatives you've considered It would be great to have one more constructor, with two properties: propertyName from the object and propertyName from json, in this way will be supported different names.
*** Source/destination types
// Put the types you are serializing or deserializing here
public class Example123: ExampleBase
{
[Required]
[JsonProperty("123Configuration")]
public Example123Config Configuration123{ get; set; }
}
*** Source/destination JSON
"123Configuration": { "Id": "1", "Name": "NameValue", }
Hi,
In the sample provided I don't see usage of JsonSubTypes
The KnownSubTypeWithProperty use the property name that is found in the json
This will not work because property from Json and property from the object should be the same, aliases like here
[JsonProperty("123Configuration")] are not taken in consideration
[JsonConverter(typeof(JsonSubtypes))]
[JsonSubtypes.KnownSubTypeWithProperty(typeof(Example123), "123Configuration")]
public abstract class ExampleBase
{
}
public class Example123: ExampleBase
{
[Required]
[JsonProperty("123Configuration")]
public Example123Config Configuration123{ get; set; }
}
*** Source/destination JSON
"123Configuration": {
"Id": "1",
"Name": "NameValue",
}
Hello @mihailovdumitru
I can't reproduce your issue with the code sample provided : https://dotnetfiddle.net/O23eiA
Runnable sample:
```csharp using System; using JsonSubTypes; using Newtonsoft.Json; [JsonConverter(typeof(JsonSubtypes))]
[JsonSubtypes.KnownSubTypeWithProperty(typeof(Example123), "123Configuration")]
public abstract class ExampleBase
{
}
public class Example123: ExampleBase
{
[JsonProperty("123Configuration", Required = Required.Always)]
public Example123Config Configuration123{ get; set; }
}
public class Example123Config
{
public string Id { get; set; }
public string Name { get; set; }
}
public class Program { public static void Main() { var root = JsonConvert.DeserializeObject<ExampleBase>("{"123Configuration": {\n"Id": "1",\n"Name": "NameValue"}}"); Console.WriteLine(root.GetType()); } }
</details>