Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ENHANCE: ADD SUPPORT FOR JACKSON AND GSON #45 #771

Open
mountainowl opened this issue Dec 6, 2016 · 1 comment
Open

ENHANCE: ADD SUPPORT FOR JACKSON AND GSON #45 #771

mountainowl opened this issue Dec 6, 2016 · 1 comment

Comments

@mountainowl
Copy link

mountainowl commented Dec 6, 2016

I'm trying to use Jackson with Qbit. So far this is what I have.

Service code:

EndpointServerBuilder serverBuilder = managedServiceBuilder
                   .addEndpointService(serviceObject)
                   .getEndpointServerBuilder()
                   .setJsonMapper(new JacksonMapper());
           serverBuilder
                   .setFactory(new AltQbitFactory());

           serverBuilder
                   .build()
                   .startServer();

JacksonMapper.java

public class JacksonMapper implements JsonMapper {

    private static final ObjectMapper objectMapper = ObjectMapperConfiguration.getObjectMapper();

    public JacksonMapper() {
        System.out.println("Jackson Mapper Instantiated!");
    }

    @Override
    public Object fromJson(String json) {
        try {
            return objectMapper.readValue(json, Object.class);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public <T> T fromJson(String json, Class<T> cls) {

        TypeReference<T> typeReference = new TypeReference<T>() {
            @Override
            public Type getType() {
                return cls;
            }
        };

        try {
            return objectMapper.readValue(json, typeReference);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public <T> List<T> fromJsonArray(String json, Class<T> componentClass) {

        TypeReference<List<T>> typeReference = new TypeReference<List<T>>() {
            @Override
            public Type getType() {
                return componentClass;
            }
        };

        try {
            return objectMapper.readValue(json, typeReference);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public String toJson(Object object) {
        try {
            return objectMapper.writeValueAsString(object);
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public <K, V> Map<K, V> fromJsonMap(String json, Class<K> componentClassKey, Class<V> componentClassValue) {

//        SimpleModule module = new SimpleModule();
//        module.addKeySerializer(Date.class, new DateAsTimestampSerializer());

        return null;
    }
}

AltQbitFactory.java

public class AltQbitFactory extends BoonQBitFactory {

   @Override
   public JsonMapper createJsonMapper() {
       return new JacksonMapper();
   }
}

Everything get instantiated correctly, however when the request arrives, and StandardRequestTransformer#transform is called from HttpRequestServiceServerHandlerUsingMetaImpl#handleRestCall BoonJsonMapper is invoked even though I'm registering Jackson mapper and factory.

Any pointers will be really helpful.

@RichardHightower
Copy link
Member

Send a pull request and I will add support. I'd love to have support for GSON and Jackson.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants