How to get the Entity column value when it is bound to an enum?

hello everybody

I try to create a Json respectively a keyvalue map from “getEditedEntity”. This works out fine but for the columns of type enumeration I receive the enums value (which is a string) instead of its integer ID. In the database table itself the corresponding IDs of the enum are stored in the column as expected.

My JSON od KeyValue Map structure should reflect only the column name and its integer value exactly as seen in the Database Table.

Any hint is welcome since I am struggeling with this task for quite a while.

thx in advance

Michael

Hi

I think it is actually not Jmix issue.
How do you perform conversion of the entity to JSON? You need to configure your mapper to take enum property value id, not the value itself.

Thx Alexander, I am using com.fasterxml.jackson.databind.ObjectMapper. Either it’s methods “writeValueAsString” nor “readTree” delivery me a JSON based on my entity. Those methods obviously take the value instead of the property value.

Can you give me an idea accomplishing this task may without this ObjectMapper?

Thx for you hints!

best regards,

Michael

See this article for mapper customization basics: https://www.baeldung.com/jackson-object-mapper-tutorial#2-creating-custom-serializer-or-deserializer

Create custom serializer for your enum class that writes number value.
Serializer method will look like this:

    @Override
    public void serialize(TestEnum value, JsonGenerator jsonGenerator, SerializerProvider provider) throws IOException {
        jsonGenerator.writeNumber(value.getId());
    }

Thx Alexander

I will give it a try. Just to make sure - my intention is to retrieve the numeric value of the entity fields which are bound to enums.

best regards,

Michael