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

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

Introduction

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

Prototype

public final void writeArrayFieldStart(String fieldName) throws IOException, JsonGenerationException 

Source Link

Document

Convenience method for outputting a field entry ("member") (that will contain a JSON Array value), and the START_ARRAY marker.

Usage

From source file:com.google.openrtb.json.OpenRtbJsonUtils.java

/**
 * Writes an int array if not empty.//from  w  ww .java  2s .c om
 */
public static void writeInts(String fieldName, List<Integer> data, JsonGenerator gen) throws IOException {
    if (!data.isEmpty()) {
        gen.writeArrayFieldStart(fieldName);
        for (Integer d : data) {
            gen.writeNumber(d);
        }
        gen.writeEndArray();
    }
}

From source file:com.google.openrtb.json.OpenRtbJsonUtils.java

/**
 * Writes a long array if not empty, using quotes for values that are too big.
 *
 * @see #writeLong(long, JsonGenerator)/*  w  ww.ja  v a 2  s .c o  m*/
 */
public static void writeLongs(String fieldName, List<Long> data, JsonGenerator gen) throws IOException {
    if (!data.isEmpty()) {
        gen.writeArrayFieldStart(fieldName);
        for (long d : data) {
            writeLong(d, gen);
        }
        gen.writeEndArray();
    }
}

From source file:com.google.openrtb.json.OpenRtbJsonUtils.java

/**
 * Writes a enum array if not empty./*w w  w.  ja  v  a  2s  . c  om*/
 *
 * @see #writeEnum(ProtocolMessageEnum, JsonGenerator)
 */
public static void writeEnums(String fieldName, List<? extends ProtocolMessageEnum> enums, JsonGenerator gen)
        throws IOException {
    if (!enums.isEmpty()) {
        gen.writeArrayFieldStart(fieldName);
        for (ProtocolMessageEnum e : enums) {
            writeEnum(e, gen);
        }
        gen.writeEndArray();
    }
}

From source file:eu.project.ttc.readers.TermSuiteJsonCasSerializer.java

private static void writeIntFSArrayField(JsonGenerator jg, String fieldName, FSArray words) throws IOException {
    if (words == null)
        return;/*from  ww w  .  j  a  v a2 s . c  o m*/
    jg.writeArrayFieldStart(fieldName);

    for (int i = 0; i < words.size(); i++) {
        WordAnnotation wa = (WordAnnotation) words.get(i);
        jg.writeStartArray();
        jg.writeNumber(wa.getBegin());
        jg.writeNumber(wa.getEnd());
        jg.writeEndArray();
    }
    jg.writeEndArray();
}

From source file:com.netflix.hystrix.serial.SerialHystrixRequestEvents.java

private static void convertExecutionToJson(JsonGenerator json,
        HystrixRequestEvents.ExecutionSignature executionSignature, List<Integer> latencies)
        throws IOException {
    json.writeStartObject();// w  w  w  .  j  a va  2 s .  c o  m
    json.writeStringField("name", executionSignature.getCommandName());
    json.writeArrayFieldStart("events");
    ExecutionResult.EventCounts eventCounts = executionSignature.getEventCounts();
    for (HystrixEventType eventType : HystrixEventType.values()) {
        if (!eventType.equals(HystrixEventType.COLLAPSED)) {
            if (eventCounts.contains(eventType)) {
                int eventCount = eventCounts.getCount(eventType);
                if (eventCount > 1) {
                    json.writeStartObject();
                    json.writeStringField("name", eventType.name());
                    json.writeNumberField("count", eventCount);
                    json.writeEndObject();
                } else {
                    json.writeString(eventType.name());
                }
            }
        }
    }
    json.writeEndArray();
    json.writeArrayFieldStart("latencies");
    for (int latency : latencies) {
        json.writeNumber(latency);
    }
    json.writeEndArray();
    if (executionSignature.getCachedCount() > 0) {
        json.writeNumberField("cached", executionSignature.getCachedCount());
    }
    if (executionSignature.getEventCounts().contains(HystrixEventType.COLLAPSED)) {
        json.writeObjectFieldStart("collapsed");
        json.writeStringField("name", executionSignature.getCollapserKey().name());
        json.writeNumberField("count", executionSignature.getCollapserBatchSize());
        json.writeEndObject();
    }
    json.writeEndObject();
}

From source file:com.netflix.hystrix.contrib.requests.stream.HystrixRequestEventsJsonStream.java

private static void convertExecutionToJson(JsonGenerator json,
        HystrixRequestEvents.ExecutionSignature executionSignature, List<Integer> latencies)
        throws IOException {
    json.writeStartObject();//from   ww  w . j  a  v a2 s.co  m
    json.writeStringField("name", executionSignature.getCommandName());
    json.writeArrayFieldStart("events");
    ExecutionResult.EventCounts eventCounts = executionSignature.getEventCounts();
    for (HystrixEventType eventType : HystrixEventType.values()) {
        if (eventType != HystrixEventType.COLLAPSED) {
            if (eventCounts.contains(eventType)) {
                int eventCount = eventCounts.getCount(eventType);
                if (eventCount > 1) {
                    json.writeStartObject();
                    json.writeStringField("name", eventType.name());
                    json.writeNumberField("count", eventCount);
                    json.writeEndObject();
                } else {
                    json.writeString(eventType.name());
                }
            }
        }
    }
    json.writeEndArray();
    json.writeArrayFieldStart("latencies");
    for (int latency : latencies) {
        json.writeNumber(latency);
    }
    json.writeEndArray();
    if (executionSignature.getCachedCount() > 0) {
        json.writeNumberField("cached", executionSignature.getCachedCount());
    }
    if (executionSignature.getEventCounts().contains(HystrixEventType.COLLAPSED)) {
        json.writeObjectFieldStart("collapsed");
        json.writeStringField("name", executionSignature.getCollapserKey().name());
        json.writeNumberField("count", executionSignature.getCollapserBatchSize());
        json.writeEndObject();
    }
    json.writeEndObject();
}

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 . com
    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: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 va2s .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:net.echinopsii.ariane.community.core.directory.wat.json.ds.organisational.CompanyJSON.java

public final static void company2JSON(Company company, JsonGenerator jgenerator) throws IOException {
    jgenerator.writeStartObject();/*from   w ww .  jav  a  2s.  com*/
    jgenerator.writeNumberField(CMP_ID, company.getId());
    jgenerator.writeNumberField(CMP_VERSION, company.getVersion());
    jgenerator.writeStringField(CMP_NAME, company.getName());
    jgenerator.writeStringField(CMP_DESCRIPTION, company.getDescription());
    jgenerator.writeArrayFieldStart(CMP_APP_ID);
    for (Application app : company.getApplications())
        jgenerator.writeNumber(app.getId());
    jgenerator.writeEndArray();
    jgenerator.writeArrayFieldStart(CMP_OST_ID);
    for (OSType ost : company.getOsTypes())
        jgenerator.writeNumber(ost.getId());
    jgenerator.writeEndArray();
    jgenerator.writeEndObject();
}

From source file:net.echinopsii.ariane.community.plugin.rabbitmq.directory.json.RabbitmqClusterJSON.java

public final static void rabbitmqCluster2JSON(RabbitmqCluster cluster, JsonGenerator jgenerator)
        throws IOException {
    jgenerator.writeStartObject();/*from ww w .ja  va  2s.c o m*/
    jgenerator.writeNumberField(RMQC_ID, cluster.getId());
    jgenerator.writeNumberField(RMQC_VERSION, cluster.getVersion());
    jgenerator.writeStringField(RMQC_NAME, cluster.getName());
    jgenerator.writeStringField(RMQC_DESCRIPTION, cluster.getDescription());
    jgenerator.writeArrayFieldStart(RMQC_NODES_ID);
    for (RabbitmqNode node : cluster.getNodes())
        jgenerator.writeNumber(node.getId());
    jgenerator.writeEndArray();
    jgenerator.writeEndObject();
}