Jackson enum property not recognized

I have an enumeration like this:

public enum Type {
   @JsonProperty("private")Private, group, supergroup, channel    
}

the json file itself has a private field, so for serialization and matching I need a private field (this is a Java keyboard), but if I use private with all lowercase letters that are a run-time error. I used @JsonProperty as above, but still not working.

Do you have a solution for this?

here is the exception:

com.fasterxml.jackson.databind.exc.InvalidFormatException: cannot build an instance com.mehdi.model.Chat $ Type from value String 'private': value is not one of the declared instance names Enum: [Private, group, supergroup, channel] in [Source: /home/mehdi/Desktop/json.txt; row: 14, column: 16] (via the reference chain: com.mehdi.model.Update ["message"] → com.mehdi.model.Message ["chat"] → com.mehdi.model.Chat ["type" ]) in com.fasterxml.jackson.databind.exc.InvalidFormatException.from (InvalidFormatException.java:55) in com.fasterxml.jackson.databind.DeserializationContext.weirdStringException (DeserializationContext.java:742) in com.fasterxml.jackson.datab .deser.std.EnumDeserializer.deserialize (EnumDeserializer.java:91) in com.fasterxml.jackson.databind.deser.std.EnumDeserializer.deserialize (EnumDeserializer.java:20) in com.fasterxml.jackson.databind.deser.SettableBeanProperty.deserialize (SettableBeanProperty.javahaps25) in com.fasterxml.jackson.databind.deser.impl.MethodProperty.deserializeAndSet ( 99) in com.fasterxml.jackson.databind.deser.BeanDeserializer.vanillaDeserialize (BeanDeserializer.java:242) in com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize (BeanDeserializer.java:118) in com.fasterxml.jack .databind.deser.SettableBeanProperty.deserialize (SettableBeanProperty.javahaps25) in com.fasterxml.jackson.databind.deser.impl.MethodProperty.deserializeAndSet (MethodProperty.java:99) in com.fasterxml.jackson.databindeser .vanillaDeserialize (BeanDeserializer.java:242) in com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize (BeanDeserializer.java:118) in com.fasterxml.jackson.databind.deser.SettableBeanProperty.deserialize (SettableBeanProperty.javahaps25) in com.fasterxml.jackson.databind.deser.impl.MethodProperty.deserializeAndSet (MethodProperty.java:99ml in com.fasterx .jackson.databind.deser.BeanDeserializer.vanillaDeserialize (BeanDeserializer.java:242) in com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize (BeanDeserializer.java:118) in com.fasterxml.jackson.databinderbread (ObjectMapper.java:2993) at com.fasterxml.jackson.databind.ObjectMapper.readValue (ObjectMapper.java:2052) at com.mehdi.ff.Test.main (Test.java:25) in sun.reflect.NativeMethodAccessorImpl. invoke0 (native method) in sun.reflect.NativeMethodAccessorImpl.invoke (NativeMethodAccessorImpl.java:62) in sun.reflect.DelegatingMethodAccessorImpl.invoke (DelegatingMethodAccessorImpl.java: 43) in java.lang.reflect.Method.invoke (Method.java:497) in com.intellij.rt.execution.application.AppMain.main (AppMain.java:140)

0

+4
2

@JsonValue.

public enum Type {

    Private("private"),
    Group("group"),
    Supergroup("supergroup"),
    Channel("channel")

    private String jsonValue;

    private Type(final String json) {
        this.jsonValue = json;
    }

    @JsonValue
    public String jsonValue() {
        return this.jsonValue;
    }

}
+5

- @Toilal :

public enum Type {
    Private, group, supergroup, channel;
    @JsonValue
    public String toString() {
        return super.toString().toLowerCase();
    }
}
0

Source: https://habr.com/ru/post/1617211/


All Articles