How to convert JSON field name to Java bean class property with Jackson

ErnstZ :

I have access to a RESTful API which returns JSON Strings, such as the following:

{
    "Container1": {
        "active": true
    },
    "Container2": {
        "active": false
    },
}

The problem is that the RESTful API is a bit maldesigned. The field name contains the data already. With the Jackson library it is not possible to deserialize the field name to a property name of the corresponding Java bean class. I assume, this isn't intended by the JSON specification neither. The above JSON string needs to be deserialized to an instance of the following class:

public class Container {
    private Boolean active;
    private String name;
}

I end up with UnrecognizedPropertyException for the field Container1.

I thought to configure to ignore unknown properties and to provide a JsonDeserializer for that property like this:

@JsonIgnoreProperties(ignoreUnknown = true)
public class Container {
    private Boolean active;
    private String name;

    @JsonDeserialize(using = FieldNameToPropertyDeserializer.class)
    public void setName(String name) {
        this.name = name;
    }
}

and the FieldNameToPropertyDeserializer:

public class FieldNameToPropertyDeserializer extends StdDeserializer<String> {
    public FieldNameToPropertyDeserializer() {
        super(String.class);
    }

    @Override
    public String deserialize(JsonParser parser, DeserializationContext context) throws IOException, JsonProcessingException {
        return parser.getCurrentName();
    }
}

The invocation of the deserialization is achieved as follows:

String jsonString = response.readEntity(String.class);
ObjectMapper objectMapper = new ObjectMapper();
ObjectReader readerFor = objectMapper.readerFor(Container.class);
MappingIterator<Container> mappingIterator = readerFor.readValues(jsonString);
while (mappingIterator.hasNext()) {
    Container container = (Container) mappingIterator.next();
    containers.add(container);
}

But I only receive empty objects (properties set to null) because the parsing of the properties is skipped since I set @JsonIgnoreProperties(ignoreUnknown = true).

Is this possible at all? Or should I implement something like a post-processing afterwards?

Hades :

Just a quick solution, if the object is such that, that all of it object is a container object you can receive the JSON inside and JSONObject you may use below code

import java.io.IOException;
import org.json.JSONException;
import org.json.JSONObject;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;

public class TestSO {

    public static void main(String[] args) throws JsonParseException, JsonMappingException, JSONException, IOException {
        String jsonString = "{\r\n" + 
                "    \"Container1\": {\r\n" + 
                "        \"active\": true\r\n" + 
                "    },\r\n" + 
                "    \"Container2\": {\r\n" + 
                "        \"active\": false\r\n" + 
                "    },\r\n" + 
                "}";

        JSONObject jsonObject = new JSONObject(jsonString);

        ObjectMapper mapper = new ObjectMapper();
        for (String key : jsonObject.keySet()) {
            Container container = mapper.readValue(jsonObject.get(key).toString(), Container.class);
            System.out.println(container);
        }   
    }

    static class Container{
        private String name;
        private Boolean active;
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        public Boolean getActive() {
            return active;
        }
        public void setActive(Boolean active) {
            this.active = active;
        }
        @Override
        public String toString() {
            return "Container [name=" + name + ", active=" + active + "]";
        }
    }
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326412&siteId=1