Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
590 views
in Technique[技术] by (71.8m points)

oop - How do I parametrize response parsing in Java?

I'm writing a network class and want to be able to parse different responses to different classes (there's still one-to-one relationship but I want to have a single parseResponse() that will deal with all responses from different endpoints, and endpoint.className has the expected classType that I should map to):

private Class<?> parseResponse(StringBuilder responseContent, Endpoint endpoint) {
    ObjectMapper mapper = new ObjectMapper();
    try {
        Class<?> object = mapper.readValue(responseContent.toString(), endpoint.className);
        // endpoint.className has Class<?> type
        if (object instanceof endpoint.className) {
        }
    } catch (IOException e) {
        // handle errors
    }
}

But there's an error if I write if (object instanceof endpoint.className)

Update: probably the better option is to add parse() method to Endpoint class:

public Class<?> parseResponse(String responseContent) {
   // this.className has Class<?> type (e.g., Foo.class).
}

public enum Endpoint {
    FOO (Foo.class),
    BAR (Bar.class);

    private Class<?> classType;
}

But there're still the same type errors.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You should separate JSON deserialisation from other parts of your app. You can not implement one method for all responses but you probably have a limited number of responses and you can declare some simple methods for each class. Generally, you could have only one method with declaration like below:

public <T> T deserialise(String payload, Class<T> expectedClass) {
    Objects.requireNonNull(payload);
    Objects.requireNonNull(expectedClass);

    try {
        return mapper.readValue(payload, expectedClass);
    } catch (IOException e) {
        throw new IllegalStateException("JSON is not valid!", e);
    }
} 

And now, you can deserialise all payloads you want. You need to provide JSON payload and POJO class you want to receive back.

Simple working solution which shows that concept:

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;

import java.io.IOException;
import java.util.Objects;

public class JsonMapper {

    private final ObjectMapper mapper = new ObjectMapper();

    public JsonMapper() {
        // configure mapper instance if required
        mapper.enable(SerializationFeature.INDENT_OUTPUT);
        mapper.enable(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT);
        // etc...
    }

    public String serialise(Object value) {
        try {
            return mapper.writeValueAsString(value);
        } catch (JsonProcessingException e) {
            throw new IllegalStateException("Could not generate JSON!", e);
        }
    }

    public <T> T deserialise(String payload, Class<T> expectedClass) {
        Objects.requireNonNull(payload);
        Objects.requireNonNull(expectedClass);

        try {
            return mapper.readValue(payload, expectedClass);
        } catch (IOException e) {
            throw new IllegalStateException("JSON is not valid!", e);
        }
    }

    public Foo parseResponseFoo(String payload) {
        return deserialise(payload, Foo.class);
    }

    public Bar parseResponseBar(String payload) {
        return deserialise(payload, Bar.class);
    }

    public static void main(String[] args) {
        JsonMapper jsonMapper = new JsonMapper();

        String bar = "{"bar" : 2}";
        System.out.println(jsonMapper.parseResponseBar(bar));

        String foo = "{"foo" : 1}";
        System.out.println(jsonMapper.parseResponseFoo(foo));

        System.out.println("General method:");
        System.out.println(jsonMapper.deserialise(foo, Foo.class));
        System.out.println(jsonMapper.deserialise(bar, Bar.class));
    }
}

class Foo {

    public int foo;

    @Override
    public String toString() {
        return "Foo{" +
                "foo=" + foo +
                '}';
    }
}

class Bar {

    public int bar;

    @Override
    public String toString() {
        return "Bar{" +
                "bar=" + bar +
                '}';
    }
}

See also:


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...