com.websocket.server.commons.JsonPojoMapper.java Source code

Java tutorial

Introduction

Here is the source code for com.websocket.server.commons.JsonPojoMapper.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.websocket.server.commons;

/**
 *
 * @author mamadu
 */
import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonGenerationException;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.StringWriter;

public class JsonPojoMapper {

    private static ObjectMapper mapper = new ObjectMapper();
    private static JsonFactory jsonFactory = new JsonFactory();

    public static <T> Object fromJson(String jsonAsString, Class<T> pojoClass)
            throws JsonMappingException, JsonParseException, IOException {
        return mapper.readValue(jsonAsString, pojoClass);
    }

    public static <T> Object fromJson(FileReader fr, Class<T> pojoClass) throws JsonParseException, IOException {
        return mapper.readValue(fr, pojoClass);
    }

    public static String toJson(Object pojo, boolean prettyPrint)
            throws JsonMappingException, JsonGenerationException, IOException {
        StringWriter sw = new StringWriter();
        JsonGenerator jsonGen = jsonFactory.createJsonGenerator(sw);
        if (prettyPrint) {
            jsonGen.useDefaultPrettyPrinter();
        }
        mapper.writeValue(jsonGen, pojo);
        return sw.toString();
    }

    public static void toJson(Object pojo, FileWriter fw, boolean prettyPrint)
            throws JsonMappingException, JsonGenerationException, IOException {
        JsonGenerator jsonGen = jsonFactory.createJsonGenerator(fw);
        if (prettyPrint) {
            jsonGen.useDefaultPrettyPrinter();
        }
        mapper.writeValue(jsonGen, pojo);
    }
}