YamlDotNet icon indicating copy to clipboard operation
YamlDotNet copied to clipboard

Enable deserealization of types without default constructor

Open stanislav-a-frolov opened this issue 9 years ago • 1 comments

Using TypeConverter for this is quite complicated and as far as I understand full parsing needs to be implemented. The feature is present in Json.Net and I expected to find it here too. Some interfaces for ctor parameter's name and value conversion would be great too.

stanislav-a-frolov avatar Dec 07 '15 06:12 stanislav-a-frolov

At first I didn't think this would be possible. You can't usually deserialize something that doesn't have a default constructor. In order to deserialize it needs to know what constructor it can call, and also what parameters of that constructor map to the properties it's trying to set. The deserializer has neither of those critical pieces of info and thus it can't work. After I looked around some I realized that the Jackson serializer for Java accomplishes this using Annotations.

@JsonCreator
public ImportResultItemImpl(@JsonProperty("name") String name, 
        @JsonProperty("resultType") ImportResultItemType resultType, 
        @JsonProperty("message") String message) {
    super();
    this.resultType = resultType;
    this.message = message;
    this.name = name;
}

I'm sure a similar approach is possible in JsonDotNet. If I can find the time maybe I'll code it up.

NightWatchman avatar Jul 07 '16 18:07 NightWatchman

This issue appears to be abandoned, I'm going to close it.

EdwardCooke avatar Jan 13 '23 20:01 EdwardCooke