Example usage for com.google.gson.stream JsonReader JsonReader

List of usage examples for com.google.gson.stream JsonReader JsonReader

Introduction

In this page you can find the example usage for com.google.gson.stream JsonReader JsonReader.

Prototype

public JsonReader(Reader in) 

Source Link

Document

Creates a new instance that reads a JSON-encoded stream from in .

Usage

From source file:ed.cracken.code.servlets.PushHandler.java

/**
 * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
 * methods./*from   w w w .  ja va2 s .  com*/
 *
 * @param request servlet request
 * @param response servlet response
 * @throws ServletException if a servlet-specific error occurs
 * @throws IOException if an I/O error occurs
 */
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    response.setContentType("application/json");
    BufferedReader br = new BufferedReader(new InputStreamReader(request.getInputStream(), "UTF-8"));
    String json = "";
    StringBuilder sb = new StringBuilder();
    while ((json = br.readLine()) != null) {
        sb.append(json);
    }
    br.close();

    System.out.println(">> " + sb.toString());
    JsonReader reader = new JsonReader(new StringReader(sb.toString()));
    reader.setLenient(true);
    Persona persona = new Gson().fromJson(reader, Persona.class);
    IDsManager idsManager;
    if ((idsManager = (IDsManager) request.getServletContext().getAttribute("idsmanager")) == null) {
        idsManager = new IDsManager();
        request.getServletContext().setAttribute("idsmanager", idsManager);
    }
    idsManager.getIds().put(persona.getSession(), persona);

    response.setStatus(HttpServletResponse.SC_ACCEPTED);

}

From source file:edu.berkeley.ground.plugins.hive.GroundReadWriteEdgeResource.java

License:Apache License

private EdgeVersion getEdgeVersion(HttpMethod method)
        throws JsonParseException, JsonMappingException, HttpException, IOException {
    if (PluginUtil.client.executeMethod(method) == HttpURLConnection.HTTP_OK) {
        // create edge version from response of POST request
        String response = method.getResponseBodyAsString();
        JsonReader reader = new JsonReader(new StringReader(response));
        return PluginUtil.fromJson(reader, EdgeVersion.class);
    }//  ww  w. j  av a  2 s  . c o  m
    return null;
}

From source file:edu.berkeley.ground.plugins.hive.GroundReadWriteEdgeResource.java

License:Apache License

private Edge constructEdge(String response) throws GroundException {
    JsonReader reader = new JsonReader(new StringReader(response));
    return PluginUtil.fromJson(reader, Edge.class);
}

From source file:edu.berkeley.ground.plugins.hive.GroundReadWriteNodeResource.java

License:Apache License

private Node constructNode(String response) throws GroundException {
    if (response == null) {
        return null;
    }/*from   w w w  . j  a v a 2  s  . co m*/
    JsonReader reader = new JsonReader(new StringReader(response));
    return PluginUtil.fromJson(reader, Node.class);
}

From source file:edu.berkeley.ground.plugins.hive.GroundReadWriteNodeResource.java

License:Apache License

private NodeVersion constructNodeVersion(String response) throws GroundException {
    if (response == null) {
        return null;
    }/*from   w  w  w  .  j  ava 2  s  .  c  o  m*/
    JsonReader reader = new JsonReader(new StringReader(response));
    return PluginUtil.fromJson(reader, NodeVersion.class);
}

From source file:edu.berkeley.ground.plugins.hive.GroundReadWriteStructureResource.java

License:Apache License

private Structure constructStructure(String response) throws IOException {
    JsonReader reader = new JsonReader(new StringReader(response));
    return PluginUtil.fromJson(reader, Structure.class);
}

From source file:edu.berkeley.ground.plugins.hive.GroundReadWriteStructureResource.java

License:Apache License

private StructureVersion constructStructureVersion(String response) throws IOException {
    JsonReader reader = new JsonReader(new StringReader(response));
    return PluginUtil.fromJson(reader, StructureVersion.class);
}

From source file:edu.education.ucsb.muster.MusterServlet.java

License:BSD License

private MusterConfiguration loadConfiguration() {

    Gson gson = new Gson();
    JsonReader reader = null;/*ww w  .  j  a  va 2 s.c o m*/
    MusterConfiguration loadedConf = null;

    try {
        reader = new JsonReader(
                new InputStreamReader(getServletContext().getResourceAsStream(confPath), "UTF-8"));
    } catch (UnsupportedEncodingException e) {
        addException(e, "Unsupported encoding");
    } catch (NullPointerException e) {
        addException(e, "Couldn't open config file `" + confPath + "`");
    }

    loadedConf = gson.fromJson(reader, MusterConfiguration.class);
    loadedConf.lastLoaded = System.currentTimeMillis();
    return loadedConf;
}

From source file:edu.isi.karma.modeling.alignment.GraphUtil.java

License:Apache License

public static DirectedWeightedMultigraph<Node, DefaultLink> importJson(String filename) throws IOException {

    File file = new File(filename);
    if (!file.exists()) {
        logger.error("cannot open the file " + filename);
    }/*from www . ja va2 s .c  o m*/

    FileInputStream in = new FileInputStream(file);
    JsonReader reader = new JsonReader(new InputStreamReader(in, "UTF-8"));
    try {
        return readGraph(reader);
    } catch (Exception e) {
        logger.error("error in reading the model from json!");
        e.printStackTrace();
        return null;
    } finally {
        reader.close();
    }
}

From source file:edu.isi.karma.modeling.alignment.SemanticModel.java

License:Apache License

public static SemanticModel readJson(String filename) throws IOException {

    File file = new File(filename);
    if (!file.exists()) {
        logger.error("cannot open the file " + filename);
    }/*from   w  w w  . j a  va  2  s. c o  m*/

    FileInputStream in = new FileInputStream(file);
    JsonReader reader = new JsonReader(new InputStreamReader(in, "UTF-8"));
    try {
        return readModel(reader);
    } catch (Exception e) {
        logger.error("error in reading the model from json!");
        e.printStackTrace();
        return null;
    } finally {
        reader.close();
    }
}