Is it possible to change the output alias in pydantic?

Update (2023-10-07): Check the comments in the question for other answers and this answer in the same question for pydantic 2.0 or newer. Switch aliases and field names and use the allow_population_by_field_name model config option: class TMDB_Category(BaseModel): strCategory: str = Field(alias=”name”) strCategoryDescription: str = Field(alias=”description”) class Config: allow_population_by_field_name = True Let the aliases configure the … Read more

JSON.NET: How to deserialize interface property based on parent (holder) object value?

You are on the right track. You do need to implement a custom JsonConverter for your Holder class to handle this situation, as you suggested. But, don’t worry, it is possible to write the converter in such a way that you can use the original reader and serializer instances passed to the converter, without ever … Read more

Deserialization of reference types without parameterless constructor is not supported

You can achieve your desired result. You need to switch to NewtonsoftJson serialization (from package Microsoft.AspNetCore.Mvc.NewtonsoftJson) Call this in Startup.cs in the ConfigureServices method: services.AddControllers().AddNewtonsoftJson(); After this, your constructor will be called by deserialization. Extra info: I am using ASP Net Core 3.1 Later Edit: I wanted to give more info on this, as it … Read more

GSON Case-Insensitive Enum Deserialization

A simpler way I found (just now) to do this is to use the @SerializedName annotation. I found it in the EnumTest.java here (the Gender class around ln 195): https://code.google.com/p/google-gson/source/browse/trunk/gson/src/test/java/com/google/gson/functional/EnumTest.java?r=1230 This assumes that all of your Types will come in as lowercase as opposed to being “case insensitive” public enum Type { @SerializedName(“live”) LIVE, @SerializedName(“upcoming”) … Read more

Can Jackson polymorphic deserialization be used to serialize to a subtype if a specific field is present?

As of Jackson 2.12.2, the following accomplishes the goal using the “deduction-based polymorphism” feature. If properties distinct to the Bird subtype (i.e. wingspan) are present, the deserialized type will be Bird; else it will be Animal: @JsonTypeInfo(use=Id.DEDUCTION, defaultImpl = Animal.class) @JsonSubTypes({@Type(Bird.class)}) public class Animal { public String name; public int age; } Deduction-based polymorphism The … Read more

Deserialize json character as enumeration

You don’t necessary need a custom JsonConverter you can use the built in StringEnumConverter with the combination of the EnumMemberAttribute (from the System.Runtime.Serialization assembly). Without the EnumMemberAttribute it uses the enum names so Artist, Contemporary, etc so you need to change the names with it to your A,C, etc value. But it is not the … Read more