Example usage for com.fasterxml.jackson.core JsonGenerator close

List of usage examples for com.fasterxml.jackson.core JsonGenerator close

Introduction

In this page you can find the example usage for com.fasterxml.jackson.core JsonGenerator close.

Prototype

@Override
public abstract void close() throws IOException;

Source Link

Document

Method called to close this generator, so that no more content can be written.

Usage

From source file:com.meetingninja.csse.database.ContactDatabaseAdapter.java

public static List<Contact> addContact(String contactUserID) throws IOException {

    String _url = getBaseUri().build().toString();
    URL url = new URL(_url);
    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
    conn.setRequestMethod(IRequest.PUT);
    addRequestHeader(conn, false);//from w  w  w .  j  av  a 2s  .  co  m
    SessionManager session = SessionManager.getInstance();
    String userID = session.getUserID();
    ByteArrayOutputStream json = new ByteArrayOutputStream();
    // this type of print stream allows us to get a string easily
    PrintStream ps = new PrintStream(json);
    // Create a generator to build the JSON string
    JsonGenerator jgen = JFACTORY.createGenerator(ps, JsonEncoding.UTF8);
    // Build JSON Object for Title
    jgen.writeStartObject();
    jgen.writeStringField(Keys.User.ID, userID);
    jgen.writeArrayFieldStart(Keys.User.CONTACTS);
    jgen.writeStartObject();
    jgen.writeStringField(Keys.User.CONTACTID, contactUserID);
    jgen.writeEndObject();
    jgen.writeEndArray();
    jgen.writeEndObject();
    jgen.close();
    String payload = json.toString("UTF8");
    ps.close();

    sendPostPayload(conn, payload);
    String response = getServerResponse(conn);

    // TODO: put add useful check here
    // User userContact=null;
    // String relationID=null;
    // String result = new String();
    // if (!response.isEmpty()) {
    // JsonNode contactNode = MAPPER.readTree(response);
    // if (!contactNode.has(Keys.User.ID)) {
    // result = "invalid";
    // } else {
    // result = contactNode.get(Keys.User.ID).asText();
    // userContact = getUserInfo(result);
    // relationID = contactNode.get(Keys.User.RELATIONID).asText();
    // }
    // }

    // if (!result.equalsIgnoreCase("invalid"))
    // g.setID(result);
    conn.disconnect();

    // Contact contact = new Contact(userContact,relationID);
    List<Contact> contacts = new ArrayList<Contact>();
    contacts = getContacts(userID);
    return contacts;
}

From source file:io.protostuff.JsonIOUtil.java

/**
 * Serializes the {@code message} into a {@link Writer} using the given {@code schema}.
 */// ww w  .  ja v  a2  s . co  m
public static <T> void writeTo(Writer writer, T message, Schema<T> schema, boolean numeric) throws IOException {
    final JsonGenerator generator = DEFAULT_JSON_FACTORY.createJsonGenerator(writer);
    try {
        writeTo(generator, message, schema, numeric);
    } finally {
        generator.close();
    }
}

From source file:io.protostuff.JsonIOUtil.java

/**
 * Serializes the {@code messages} into the writer using the given schema.
 *//*from w  w w  . j  a  va2  s.  co m*/
public static <T> void writeListTo(Writer writer, List<T> messages, Schema<T> schema, boolean numeric)
        throws IOException {
    final JsonGenerator generator = DEFAULT_JSON_FACTORY.createJsonGenerator(writer);
    try {
        writeListTo(generator, messages, schema, numeric);
    } finally {
        generator.close();
    }
}

From source file:io.protostuff.JsonIOUtil.java

/**
 * Serializes the {@code message} into an {@link OutputStream} using the given {@code schema}.
 * <p>/*from   w w w .j  a  v  a2  s .c  o m*/
 * The {@link LinkedBuffer}'s internal byte array will be used as the primary buffer when writing the message.
 */
public static <T> void writeTo(OutputStream out, T message, Schema<T> schema, boolean numeric,
        LinkedBuffer buffer) throws IOException {
    final IOContext context = new IOContext(DEFAULT_JSON_FACTORY._getBufferRecycler(), out, false);

    final JsonGenerator generator = newJsonGenerator(out, buffer.buffer, 0, false, context);
    try {
        writeTo(generator, message, schema, numeric);
    } finally {
        generator.close();
    }
}

From source file:io.protostuff.JsonIOUtil.java

/**
 * Serializes the {@code messages} into the stream using the given schema.
 * <p>//from w  w  w. j  a  v  a2  s  .c  o  m
 * The {@link LinkedBuffer}'s internal byte array will be used as the primary buffer when writing the message.
 */
public static <T> void writeListTo(OutputStream out, List<T> messages, Schema<T> schema, boolean numeric,
        LinkedBuffer buffer) throws IOException {
    final IOContext context = new IOContext(DEFAULT_JSON_FACTORY._getBufferRecycler(), out, false);

    final JsonGenerator generator = newJsonGenerator(out, buffer.buffer, 0, false, context);
    try {
        writeListTo(generator, messages, schema, numeric);
    } finally {
        generator.close();
    }
}

From source file:com.infinities.skyport.util.JsonUtil.java

public static String toLegendJson(Throwable e) {
    StringWriter wtr = new StringWriter();
    try {//  w  w w.j  av a2s  .co m
        JsonGenerator g = new JsonFactory().createGenerator(wtr);
        g.writeStartArray();
        g.writeStartObject();
        g.writeStringField("RES", "FALSE");
        g.writeStringField("REASON", e.toString());
        // g.writeStringField("REASON", Objects.firstNonNull(e.getMessage(),
        // e.toString()));
        g.writeEndObject();
        g.writeEndArray();
        g.close();
    } catch (Exception ee) {
        ArrayNode array = getObjectMapper().createArrayNode();
        ObjectNode reason = getObjectMapper().createObjectNode();
        ObjectNode status = getObjectMapper().createObjectNode();

        status.put(JsonConstants.STATUS, String.valueOf("FALSE"));
        reason.put(JsonConstants.REASON, "an unexpected error occurred");
        array.add(status).add(reason);

        // "[{\"RES\":\"FALSE\"}, {\"REASON\":\"an unexpected error occurred\"}]";
        return array.toString();
    }

    return wtr.toString();
}

From source file:com.meetingninja.csse.database.AgendaDatabaseAdapter.java

public static JsonNode update(String agendaID, Map<String, String> key_values)
        throws JsonGenerationException, IOException, InterruptedException {
    // prepare POST payload
    ByteArrayOutputStream json = new ByteArrayOutputStream();
    // this type of print stream allows us to get a string easily
    PrintStream ps = new PrintStream(json);

    // Create a generator to build the JSON string
    JsonGenerator jgen = JFACTORY.createGenerator(ps, JsonEncoding.UTF8);
    for (String key : key_values.keySet()) {
        jgen.flush();/*  www  . j  a v a2s .  c o  m*/
        // Build JSON Object
        jgen.writeStartObject();
        jgen.writeStringField(Keys.Agenda.ID, agendaID);
        jgen.writeStringField("field", key);
        jgen.writeStringField("value", key_values.get(key));
        jgen.writeEndObject();
        jgen.writeRaw("\f"); // write a form-feed to separate the payloads
    }

    jgen.close();
    // Get JSON Object payload from print stream
    String payload = json.toString("UTF8");
    ps.close();
    // The backend can only update a single field at a time
    String[] payloads = payload.split("\f\\s*"); // split at each form-feed
    Thread t = new Thread(new Thread(new Runnable() {
        @Override
        public void run() {
            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
                e.getLocalizedMessage();
            }
        }
    }));
    String response = "";
    for (String p : payloads) {
        t.run();
        response = updateHelper(p);
    }
    return MAPPER.readTree(response);
}

From source file:com.meetingninja.csse.database.ProjectDatabaseAdapter.java

public static void updateProject(Project p) throws IOException {
    ByteArrayOutputStream json = new ByteArrayOutputStream();
    // this type of print stream allows us to get a string easily
    PrintStream ps = new PrintStream(json);
    JsonGenerator jgen = JFACTORY.createGenerator(ps, JsonEncoding.UTF8);
    // Build JSON Object for Title
    jgen.writeStartObject();/*w ww.  j av  a  2s. c o  m*/
    jgen.writeStringField(Keys.Project.ID, p.getProjectID());
    jgen.writeStringField("field", Keys.Project.TITLE);
    jgen.writeStringField("value", p.getProjectTitle());
    jgen.writeEndObject();
    jgen.close();
    String payloadTitle = json.toString("UTF8");
    ps.close();

    json = new ByteArrayOutputStream();
    // this type of print stream allows us to get a string easily
    ps = new PrintStream(json);
    jgen = JFACTORY.createGenerator(ps, JsonEncoding.UTF8);
    // Build JSON Object for Project members
    jgen.writeStartObject();
    jgen.writeStringField(Keys.Project.ID, p.getProjectID());
    jgen.writeStringField("field", Keys.Project.MEETINGS);
    jgen.writeArrayFieldStart("value");
    for (Meeting meeting : p.getMeetings()) {
        jgen.writeStartObject();
        jgen.writeStringField(Keys.Meeting.ID, meeting.getID());
        jgen.writeEndObject();

    }
    jgen.writeEndArray();
    jgen.writeEndObject();
    jgen.close();
    String payloadMeetings = json.toString("UTF8");
    ps.close();

    json = new ByteArrayOutputStream();
    // this type of print stream allows us to get a string easily
    ps = new PrintStream(json);
    jgen = JFACTORY.createGenerator(ps, JsonEncoding.UTF8);
    // Build JSON Object for Project members
    jgen.writeStartObject();
    jgen.writeStringField(Keys.Project.ID, p.getProjectID());
    jgen.writeStringField("field", Keys.Project.NOTES);
    jgen.writeArrayFieldStart("value");
    for (Note note : p.getNotes()) {
        jgen.writeStartObject();
        jgen.writeStringField(Keys.Note.ID, note.getID());
        jgen.writeEndObject();

    }
    jgen.writeEndArray();
    jgen.writeEndObject();
    jgen.close();
    String payloadNotes = json.toString("UTF8");
    ps.close();

    json = new ByteArrayOutputStream();
    // this type of print stream allows us to get a string easily
    ps = new PrintStream(json);
    jgen = JFACTORY.createGenerator(ps, JsonEncoding.UTF8);
    // Build JSON Object for Project members
    jgen.writeStartObject();
    jgen.writeStringField(Keys.Project.ID, p.getProjectID());
    jgen.writeStringField("field", Keys.Project.MEMBERS);
    jgen.writeArrayFieldStart("value");
    for (User member : p.getMembers()) {
        jgen.writeStartObject();
        jgen.writeStringField(Keys.User.ID, member.getID());
        jgen.writeEndObject();

    }
    jgen.writeEndArray();
    jgen.writeEndObject();
    jgen.close();
    String payloadMembers = json.toString("UTF8");
    ps.close();

    // Establish connection
    updateHelper(payloadTitle);
    updateHelper(payloadMeetings);
    updateHelper(payloadNotes);
    System.out.println(updateHelper(payloadMembers));

}

From source file:com.meetingninja.csse.database.NotesDatabaseAdapter.java

public static String createNote(Note n) throws Exception {
    // Server URL setup
    String _url = getBaseUri().build().toString();

    // Establish connection
    URL url = new URL(_url);
    HttpURLConnection conn = (HttpURLConnection) url.openConnection();

    // add request header
    conn.setRequestMethod(IRequest.POST);
    //addRequestHeader(conn, true);

    // prepare POST payload
    ByteArrayOutputStream json = new ByteArrayOutputStream();
    // this type of print stream allows us to get a string easily
    PrintStream ps = new PrintStream(json);
    // Create a generator to build the JSON string
    JsonGenerator jgen = JFACTORY.createGenerator(ps, JsonEncoding.UTF8);

    // Build JSON Object
    jgen.writeStartObject();// w w  w  . j a  v a  2s.com
    jgen.writeStringField(Keys.Note.CREATED_BY, n.getCreatedBy());
    jgen.writeStringField(Keys.Note.TITLE, n.getTitle());
    jgen.writeStringField(Keys.Note.DESC, n.getDescription());
    jgen.writeStringField(Keys.Note.CONTENT, n.getContent());
    jgen.writeStringField(Keys.Note.UPDATED, n.getDateCreated());
    jgen.writeEndObject();
    jgen.close();

    // Get JSON Object payload from print stream
    String payload = json.toString("UTF8");
    Log.d("CREATENOTE_payload", payload);
    ps.close();

    // Send payload
    int responseCode = sendPostPayload(conn, payload);
    String response = getServerResponse(conn);
    Log.d("CREATENOTE_response", response);

    String ID = "";
    if (!response.isEmpty()) {
        JsonNode tree = MAPPER.readTree(response);
        if (!tree.has(Keys.Note.ID))
            ID = "-1";
        else
            ID = tree.get(Keys.Note.ID).asText();
    }

    conn.disconnect();
    return ID;
}

From source file:com.meetingninja.csse.database.GroupDatabaseAdapter.java

public static Group createGroup(Group g) throws IOException, MalformedURLException {
    // Server URL setup
    String _url = getBaseUri().build().toString();

    // establish connection
    URL url = new URL(_url);
    HttpURLConnection conn = (HttpURLConnection) url.openConnection();

    conn.setRequestMethod("POST");
    addRequestHeader(conn, true);/*from  w w w.ja  v  a  2 s .c om*/

    // prepare POST payload
    ByteArrayOutputStream json = new ByteArrayOutputStream();
    // this type of print stream allows us to get a string easily
    PrintStream ps = new PrintStream(json);
    // Create a generator to build the JSON string
    JsonGenerator jgen = JFACTORY.createGenerator(ps, JsonEncoding.UTF8);

    // Build JSON Object
    jgen.writeStartObject();
    jgen.writeStringField(Keys.Group.TITLE, g.getGroupTitle());
    jgen.writeArrayFieldStart(Keys.Group.MEMBERS);
    for (User member : g.getMembers()) {
        jgen.writeStartObject();
        jgen.writeStringField(Keys.User.ID, member.getID());
        jgen.writeEndObject();

    }
    jgen.writeEndArray();
    jgen.writeEndObject();
    jgen.close();

    // Get JSON Object payload from print stream
    String payload = json.toString("UTF8");
    ps.close();

    // send payload
    int responseCode = sendPostPayload(conn, payload);
    String response = getServerResponse(conn);

    // prepare to get the id of the created Meeting
    // Map<String, String> responseMap = new HashMap<String, String>();

    /*
     * result should get valid={"meetingID":"##"}
     */
    String result = new String();
    if (!response.isEmpty()) {
        // responseMap = MAPPER.readValue(response,
        // new TypeReference<HashMap<String, String>>() {
        // });
        JsonNode groupNode = MAPPER.readTree(response);
        if (!groupNode.has(Keys.Group.ID)) {
            result = "invalid";
        } else
            result = groupNode.get(Keys.Group.ID).asText();
    }

    if (!result.equalsIgnoreCase("invalid"))
        g.setID(result);

    conn.disconnect();
    return g;
}