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

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

Introduction

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

Prototype

public void writeStringField(String fieldName, String value) throws IOException, JsonGenerationException 

Source Link

Document

Convenience method for outputting a field entry ("member") that has a String value.

Usage

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

public static Group updateGroup(Group group) 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();//  ww  w.  j  av a 2  s.c o  m
    jgen.writeStringField(Keys.Group.ID, group.getGroupID());
    jgen.writeStringField("field", Keys.Group.TITLE);
    jgen.writeStringField("value", group.getGroupTitle());
    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 Group members
    jgen.writeStartObject();
    jgen.writeStringField(Keys.Group.ID, group.getGroupID());
    jgen.writeStringField("field", Keys.Group.MEMBERS);
    jgen.writeArrayFieldStart("value");
    for (User member : group.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
    sendSingleEdit(payloadTitle);
    String response = sendSingleEdit(payloadMembers);
    JsonNode groupNode = MAPPER.readTree(response);

    return parseGroup(groupNode, new Group());
}

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

public static Agenda createAgenda(Agenda create) throws IOException {
    Agenda newAgenda = new Agenda(create);
    // 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);//w  w w.ja v  a  2s  .  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(); // start agenda
    jgen.writeStringField(Keys.Agenda.TITLE, create.getTitle());
    jgen.writeStringField(Keys.Agenda.MEETING, create.getAttachedMeetingID());
    jgen.writeArrayFieldStart(Keys.Agenda.TOPIC); // start topics
    MAPPER.writeValue(jgen, create.getTopics()); // recursively does
    // subtopics
    jgen.writeEndArray(); // end topics
    jgen.writeEndObject(); // end agenda
    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);

    newAgenda = parseAgenda(MAPPER.readTree(response));
    return newAgenda;
}

From source file:com.oneops.metrics.es.MetricsElasticsearchModule.java

/**
 * Add OneOps specific metatdata in json
 *
 * @param json json generator/*from  w  w  w .  j a  va2s .co m*/
 * @throws IOException throws if any error while writing the json field.
 */
private static void addOneOpsMetadata(JsonGenerator json) throws IOException {
    json.writeStringField("ipaddress", HOST.getHostAddress());
    json.writeStringField("env", defaultString(ENV.get("ONEOPS_ENVIRONMENT")));
    json.writeStringField("cloud", defaultString(ENV.get("ONEOPS_CLOUD")));
    json.writeStringField("dc", defaultString(ENV.get("DATACENTER")));
    json.writeStringField("status", defaultString(ENV.get("ONEOPS_CLOUD_ADMINSTATUS")));
    json.writeStringField("version", defaultString(context.get("oo.version")));
    json.writeStringField("appName", defaultString(context.get("appName")));
    if (OO_CLOUD_DETAILS) {
        json.writeStringField("hostname", HOST.getHostName());
        json.writeStringField("tenant", defaultString(ENV.get("ONEOPS_CLOUD_TENANT")));
        json.writeStringField("region", defaultString(ENV.get("ONEOPS_CLOUD_REGION")));
        json.writeStringField("availzone", defaultString(ENV.get("ONEOPS_CLOUD_AVAIL_ZONE")));
        json.writeStringField("assembly", defaultString(ENV.get("ONEOPS_ASSEMBLY")));
        json.writeStringField("platform", defaultString(ENV.get("ONEOPS_PLATFORM")));
    }
}

From source file:org.codehaus.modello.plugin.jsonschema.JsonSchemaGenerator.java

private static void writeDescriptionField(JsonGenerator generator, String description) throws IOException {
    if (!StringUtils.isEmpty(description)) {
        generator.writeStringField("description", description);
    }/*from ww w.  ja  va2 s . com*/
}

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

private static String getEditPayload(String meetingID, String field, String value) throws IOException {
    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();/*  w w w  . ja va  2 s  .  c om*/
    jgen.writeStringField(Keys.Meeting.ID, meetingID);
    jgen.writeStringField("field", field);
    jgen.writeStringField("value", value);
    jgen.writeEndObject();
    jgen.close();
    String payload = json.toString("UTF8");
    ps.close();
    return payload;
}

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 ww w . j  a va2 s. c  o  m

    // 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;
}

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 ww.j a va 2  s.co  m*/
    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.ntsync.shared.ContactGroup.java

private static void writeField(JsonGenerator g, String fielName, String field) throws IOException {
    if (field != null && field.length() > 0) {
        g.writeStringField(fielName, field);
    }/*from   w ww. j av  a  2 s . c om*/
}

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

private static String getEditPayload(String taskID, String field, String value) throws IOException {
    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();/*w  w w  .j ava  2  s.c  om*/
    jgen.writeStringField(Keys.Task.ID, taskID);
    jgen.writeStringField("field", field);
    jgen.writeStringField("value", value);
    jgen.writeEndObject();
    jgen.close();
    String payload = json.toString("UTF8");
    ps.close();
    return payload;
}

From source file:com.netflix.hystrix.contrib.sample.stream.HystrixConfigurationJsonStream.java

private static void writeCommandConfigJson(JsonGenerator json, HystrixCommandKey key,
        HystrixCommandConfiguration commandConfig) throws IOException {
    json.writeObjectFieldStart(key.name());
    json.writeStringField("threadPoolKey", commandConfig.getThreadPoolKey().name());
    json.writeStringField("groupKey", commandConfig.getGroupKey().name());
    json.writeObjectFieldStart("execution");
    HystrixCommandConfiguration.HystrixCommandExecutionConfig executionConfig = commandConfig
            .getExecutionConfig();/*from  w  w w .ja  v  a 2s. c  om*/
    json.writeStringField("isolationStrategy", executionConfig.getIsolationStrategy().name());
    json.writeStringField("threadPoolKeyOverride", executionConfig.getThreadPoolKeyOverride());
    json.writeBooleanField("requestCacheEnabled", executionConfig.isRequestCacheEnabled());
    json.writeBooleanField("requestLogEnabled", executionConfig.isRequestLogEnabled());
    json.writeBooleanField("timeoutEnabled", executionConfig.isTimeoutEnabled());
    json.writeBooleanField("fallbackEnabled", executionConfig.isFallbackEnabled());
    json.writeNumberField("timeoutInMilliseconds", executionConfig.getTimeoutInMilliseconds());
    json.writeNumberField("semaphoreSize", executionConfig.getSemaphoreMaxConcurrentRequests());
    json.writeNumberField("fallbackSemaphoreSize", executionConfig.getFallbackMaxConcurrentRequest());
    json.writeBooleanField("threadInterruptOnTimeout", executionConfig.isThreadInterruptOnTimeout());
    json.writeEndObject();
    json.writeObjectFieldStart("metrics");
    HystrixCommandConfiguration.HystrixCommandMetricsConfig metricsConfig = commandConfig.getMetricsConfig();
    json.writeNumberField("healthBucketSizeInMs", metricsConfig.getHealthIntervalInMilliseconds());
    json.writeNumberField("percentileBucketSizeInMilliseconds",
            metricsConfig.getRollingPercentileBucketSizeInMilliseconds());
    json.writeNumberField("percentileBucketCount", metricsConfig.getRollingCounterNumberOfBuckets());
    json.writeBooleanField("percentileEnabled", metricsConfig.isRollingPercentileEnabled());
    json.writeNumberField("counterBucketSizeInMilliseconds",
            metricsConfig.getRollingCounterBucketSizeInMilliseconds());
    json.writeNumberField("counterBucketCount", metricsConfig.getRollingCounterNumberOfBuckets());
    json.writeEndObject();
    json.writeObjectFieldStart("circuitBreaker");
    HystrixCommandConfiguration.HystrixCommandCircuitBreakerConfig circuitBreakerConfig = commandConfig
            .getCircuitBreakerConfig();
    json.writeBooleanField("enabled", circuitBreakerConfig.isEnabled());
    json.writeBooleanField("isForcedOpen", circuitBreakerConfig.isForceOpen());
    json.writeBooleanField("isForcedClosed", circuitBreakerConfig.isForceOpen());
    json.writeNumberField("requestVolumeThreshold", circuitBreakerConfig.getRequestVolumeThreshold());
    json.writeNumberField("errorPercentageThreshold", circuitBreakerConfig.getErrorThresholdPercentage());
    json.writeNumberField("sleepInMilliseconds", circuitBreakerConfig.getSleepWindowInMilliseconds());
    json.writeEndObject();
    json.writeEndObject();
}