Example usage for com.fasterxml.jackson.databind ObjectMapper writeValue

List of usage examples for com.fasterxml.jackson.databind ObjectMapper writeValue

Introduction

In this page you can find the example usage for com.fasterxml.jackson.databind ObjectMapper writeValue.

Prototype

public void writeValue(Writer w, Object value)
        throws IOException, JsonGenerationException, JsonMappingException 

Source Link

Document

Method that can be used to serialize any Java value as JSON output, using Writer provided.

Usage

From source file:org.fusesource.restygwt.server.complex.DTOInterfaceServlet.java

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    DTOImplementation impl = new DTOImplementation();
    impl.setName("interface");

    resp.setContentType("application/json");
    ObjectMapper om = new ObjectMapper();
    try {//from w w w.j  a v a  2 s .co  m
        om.writeValue(resp.getOutputStream(), impl);
    } catch (Exception e) {
        throw new ServletException(e);
    }
}

From source file:com.msopentech.thali.devicehub.javahub.ThaliDeviceHubService.java

public void startService() throws UnrecoverableKeyException, NoSuchAlgorithmException, KeyStoreException,
        IOException, InterruptedException {
    ThaliTestUtilities.turnCouchbaseLoggingTo11();
    thaliListener = new ThaliListener();

    File userHomeDirectoryRoot = new File(System.getProperty("user.home"), tdhJavaSubdirectory);
    File userTorOnionProxyRoot = new File(userHomeDirectoryRoot, "TorOnionProxy");

    JavaOnionProxyManager javaOnionProxyManager = new JavaOnionProxyManager(
            new JavaOnionProxyContext(userTorOnionProxyRoot));
    JavaContext context = new JavaThaliListenerContext(userHomeDirectoryRoot);
    thaliListener.startServer(context, ThaliListener.DefaultThaliDeviceHubPort, javaOnionProxyManager);

    // Writing out HttpKeys to root directory so relays and other clients can find them
    File httpKeysFile = new File(context.getRootDirectory(), httpKeysFileName);
    if (httpKeysFile.exists() && httpKeysFile.delete() == false) {
        throw new RuntimeException("Could not delete httpkey file " + httpKeysFile.getAbsolutePath());
    }//from w w w . ja  v  a 2s  .c  o  m

    if (httpKeysFile.createNewFile() == false) {
        throw new RuntimeException("could not create httpkey file " + httpKeysFile.getAbsolutePath());
    }

    ObjectMapper mapper = new ObjectMapper();
    mapper.writeValue(httpKeysFile, thaliListener.getHttpKeys());
}

From source file:org.n52.tamis.core.json.serialize.processes.ProcessesSerializer.java

/**
 * Serializes the list of processes as an array of processes!
 *///from  w  w w.  jav  a  2  s  .  c o  m
@Override
public void serialize(Processes_Tamis processes, JsonGenerator jsonGenerator, SerializerProvider serProvider)
        throws IOException, JsonProcessingException {
    logger.info("Start serialization of process overview!");

    List<ProcessDescription_forProcessList> processesList = processes.getProcesses();

    ProcessDescription_forProcessList[] processesArray = new ProcessDescription_forProcessList[processesList
            .size()];
    processesArray = processesList.toArray(processesArray);

    ObjectMapper objMapper = new ObjectMapper();
    objMapper.writeValue(jsonGenerator, processesArray);

    logger.info("Serialization of processes overview ended!");

}

From source file:edu.slu.tpen.servlet.ProjectsServlet.java

/**
 * Handles the HTTP <code>GET</code> method, returning a list of projects available to the current user.
 *
 * @param req servlet request// ww w .  j a  va  2 s .  c o m
 * @param resp servlet response
 * @throws ServletException if a servlet-specific error occurs
 * @throws IOException if an I/O error occurs
 */
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    int uid = getUID(req, resp);
    if (uid >= 0) {
        resp.setContentType("application/json");
        try {
            User u = new User(uid);
            Project[] projs = u.getUserProjects();
            List<Map<String, Object>> result = new ArrayList<>();
            for (Project p : projs) {
                result.add(buildQuickMap("id", "projects/" + p.getProjectID(), "name", p.getProjectName()));
            }
            ObjectMapper mapper = new ObjectMapper();
            mapper.writeValue(resp.getOutputStream(), result);
        } catch (SQLException ex) {
            reportInternalError(resp, ex);
        }
    }
}

From source file:fish.payara.examples.jcache.rest.CustomerOrdersResource.java

/**
 * Retrieves representation of an instance of fish.payara.examples.jcache.rest.CustomerOrdersResource
 * @return an instance of java.lang.String
 *///from w  w w.ja v  a2 s  .c  om
@GET
@Produces("application/json")
public String getOrders(@PathParam("customerid") long customerId) throws IOException {
    ObjectMapper mapper = new ObjectMapper();
    StringWriter writer = new StringWriter();
    mapper.writeValue(writer, connector.getOrdersForCustomer(customerId));
    return writer.toString();
}

From source file:fish.payara.examples.jcache.rest.OrderResource.java

/**
 * Retrieves representation of an instance of fish.payara.examples.jcache.rest.OrderResource
 * @return an instance of java.lang.String
 *//*from ww  w .  ja  v a  2s .c om*/
@GET
@Produces("application/json")
public String getOrder(@PathParam("orderid") long orderId) throws IOException {
    ObjectMapper mapper = new ObjectMapper();
    StringWriter writer = new StringWriter();
    mapper.writeValue(writer, connector.getOrderById(orderId));
    return writer.toString();
}

From source file:com.buildria.mocking.serializer.JacksonJsonSerializer.java

@Override
public byte[] serialize(@Nonnull Object obj) throws IOException {
    Objects.requireNonNull(obj);//from   w w  w.j av  a2s .c  o  m

    ByteArrayOutputStream out = new ByteArrayOutputStream();
    JsonEncoding encoding = mappingFrom(ctx.getCharset());
    try (JsonGenerator g = new JsonFactory().createGenerator(out, encoding)) {
        ObjectMapper mapper = new ObjectMapper();
        mapper.writeValue(g, obj);
    }

    return out.toByteArray();
}

From source file:fish.payara.examples.jcache.rest.CustomerResource.java

/**
 * Retrieves representation of an instance of fish.payara.examples.jcache.rest.CustomerResource
 * @param customerId//from   w  w w . j a  v  a  2 s . c o m
 * @return an instance of java.lang.String
 * @throws java.io.IOException
 */
@GET
@Produces("application/json")
public String getCustomer(@PathParam("customerid") long customerId) throws IOException {
    ObjectMapper mapper = new ObjectMapper();
    StringWriter writer = new StringWriter();
    mapper.writeValue(writer, connector.findCustomerById(customerId));
    return writer.toString();
}

From source file:org.smartdeveloperhub.harvesters.it.backend.exhibitor.ITHarvesterEntityProvider.java

@Override
public void writeTo(T object, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType,
        MultivaluedMap<String, Object> httpHeaders, OutputStream entityStream)
        throws IOException, WebApplicationException {

    if (Entity.class.isAssignableFrom(object.getClass())) {

        String serialized = Entities.marshallEntity((Entity) object);
        IOUtils.write(serialized, entityStream, Charsets.UTF_8);

    } else {//  ww  w  .j  ava 2s . c  o m

        ObjectMapper mapper = new ObjectMapper();
        mapper.writeValue(entityStream, object);
    }
}

From source file:servlets.Tickets.java

/**
 * Handles the HTTP <code>GET</code> method.
 *
 * @param request servlet request//from w ww. j  a v a  2s.com
 * @param response servlet response
 * @throws ServletException if a servlet-specific error occurs
 * @throws IOException if an I/O error occurs
 */
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    response.setContentType("application/json");
    Map<String, String[]> paramMap = request.getParameterMap();

    if (paramMap.isEmpty()) {
        try (PrintWriter out = response.getWriter()) {
            SelectTicketListTemplate ticketList = new SelectTicketListTemplate(response.getStatus(),
                    DataHelper.getInstance().getTicketIds());
            ObjectMapper mapper = new ObjectMapper();
            mapper.writeValue(out, ticketList);
        } catch (Exception e) {

        }
        //?   ID  ? Issue
    }
}