All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.google.sitebricks.client.transport.JacksonJsonTransport Maven / Gradle / Ivy

The newest version!
package com.google.sitebricks.client.transport;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.type.TypeFactory;
import com.google.inject.TypeLiteral;
import com.google.inject.Inject;
import com.google.inject.Singleton;

/**
 * @author Dhanji R. Prasanna ([email protected])
 */
@Singleton
public class JacksonJsonTransport extends Json {

  private final ObjectMapper objectMapper;

  @Inject
  public JacksonJsonTransport(ObjectMapper objectMapper) {
    this.objectMapper = objectMapper;
  }  
  
  public ObjectMapper getObjectMapper() {
    return objectMapper;
  }
  
  public  T in(InputStream in, Class type) throws IOException {
    return objectMapper.readValue(in, type);
  }

  @Override
  public  T in(InputStream in, TypeLiteral type) throws IOException {
    return objectMapper.readValue(in, TypeFactory.defaultInstance().constructType(type.getType()));
  }

  public  void out(OutputStream out, Class type, T data) {
    try {
      objectMapper.writeValue(out, data);
    } catch (IOException e) {
      throw new RuntimeException(e);
    }
  }


}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy