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

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

Introduction

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

Prototype

public abstract void writeStartArray() throws IOException, JsonGenerationException;

Source Link

Document

Method for writing starting marker of a JSON Array value (character '['; plus possible white space decoration if pretty-printing is enabled).

Usage

From source file:com.ning.billing.recurly.model.jackson.RecurlyObjectsSerializer.java

@Override
public void serialize(final T values, final JsonGenerator jgen, final SerializerProvider provider)
        throws IOException {
    if (values.isEmpty()) {
        jgen.writeStartArray();
        jgen.writeEndArray();//from   ww  w .  jav  a2  s . c  o m
        return;
    }

    final ToXmlGenerator xmlgen = (ToXmlGenerator) jgen;
    // Nested RecurlyObjects
    final boolean shouldSkipWritingFieldName = xmlgen.getOutputContext()
            .writeFieldName(elementName) == JsonWriteContext.STATUS_EXPECT_VALUE;
    boolean firstValue = true;
    for (final U value : values) {
        if (!shouldSkipWritingFieldName && firstValue) {
            xmlgen.setNextName(new QName(null, elementName));
        } else if (!shouldSkipWritingFieldName) {
            xmlgen.writeFieldName(elementName);
        }
        firstValue = false;

        xmlgen.writeObject(value);
    }
}

From source file:org.tanrabad.survey.service.json.MultiPolygonTypeConverter.java

private void writePolygonArray(JsonGenerator jsonGenerator, List<Location> subPolygon) throws IOException {
    jsonGenerator.writeStartArray();
    for (Location eachLocation : subPolygon) {
        if (eachLocation != null) {
            jsonGenerator.writeStartArray();
            jsonGenerator.writeNumber(eachLocation.getLongitude());
            jsonGenerator.writeNumber(eachLocation.getLatitude());
            jsonGenerator.writeEndArray();
        }//from  w  ww.j a v a 2  s  . c  om
    }
    jsonGenerator.writeEndArray();
}

From source file:de.rallye.mapper.GroupPositionsSerializer.java

@Override
public void serialize(Map<Integer, Node> value, JsonGenerator jgen, SerializerProvider provider)
        throws IOException/*,
                          JsonProcessingException*/ {

    jgen.writeStartArray();

    for (Integer groupId : value.keySet()) {
        int nodeId = value.get(groupId).nodeID;

        jgen.writeStartObject();/*from w ww .  j  a  va2 s.c o m*/

        jgen.writeFieldName(Group.GROUP_ID);
        jgen.writeNumber(groupId);

        jgen.writeFieldName(Node.NODE_ID);
        jgen.writeNumber(nodeId);

        jgen.writeEndObject();

    }

    jgen.writeEndArray();

}

From source file:com.messagehub.samples.servlet.MessageList.java

/**
 * Build message list dependent on the format Message Hub requires. The
 * message list is in the form: [{ "value": base_64_string }, ...]
 *
 * @return {String} String representation of a JSON object.
 * @throws IOException/*from  w w w .j ava2  s  . c  o m*/
 */
public String build() throws IOException {
    final JsonFactory jsonFactory = new JsonFactory();
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    JsonGenerator jsonGenerator = null;

    jsonGenerator = jsonFactory.createGenerator(outputStream);

    jsonGenerator.writeStartArray();

    // Write each message as a JSON object in
    // the form:
    // { "value": base_64_string }
    for (int i = 0; i < this.messages.size(); i++) {
        jsonGenerator.writeStartObject();
        jsonGenerator.writeFieldName("value");
        jsonGenerator.writeObject(this.messages.get(i));
        jsonGenerator.writeEndObject();
    }

    jsonGenerator.writeEndArray();

    // Close underlying streams and return string representation.
    jsonGenerator.close();
    outputStream.close();

    return new String(outputStream.toByteArray());
}

From source file:com.netflix.spectator.tdigest.TDigestWriter.java

/**
 * Write a list of measurements to some underlying storage.
 *///from  ww  w  .j a  v a 2s . c  o  m
void write(List<TDigestMeasurement> measurements) throws IOException {
    JsonGenerator gen = json.newGenerator(out);
    gen.writeStartArray();
    gen.flush();
    int pos = buf.position();
    for (TDigestMeasurement m : measurements) {
        json.encode(commonTags, m, gen);
        gen.flush();

        if (out.overflow()) {
            // Ignore the last entry written to the buffer
            out.setPosition(pos);
            gen.writeEndArray();
            gen.close();
            write(buf);

            // Reuse the buffer and write the current entry
            out.reset();
            gen = json.newGenerator(out);
            gen.writeStartArray();
            json.encode(commonTags, m, gen);
            gen.flush();

            // If a single entry is too big, then drop it
            if (out.overflow()) {
                LOGGER.warn("dropping measurement {}, serialized size exceeds the buffer cap", m.id());
                out.reset();
                gen = json.newGenerator(out);
                gen.writeStartArray();
                gen.flush();
            }

            pos = buf.position();
        } else if (buf.remaining() < MIN_FREE) {
            // Not enough free-space, go ahead and write
            gen.writeEndArray();
            gen.close();
            write(buf);

            // Reuse the buffer
            out.reset();
            gen = json.newGenerator(out);
            gen.writeStartArray();
            gen.flush();
            pos = buf.position();
        } else {
            pos = buf.position();
        }
    }

    // Write any data that is still in the buffer
    if (buf.position() > 1) {
        gen.writeEndArray();
        gen.close();
        write(buf);
    }
}

From source file:com.proofpoint.event.client.JsonEventWriter.java

public <T> void writeEvents(EventGenerator<T> events, @Nullable final String token, OutputStream out)
        throws IOException {
    checkNotNull(events, "events is null");
    checkNotNull(out, "out is null");

    final JsonGenerator jsonGenerator = jsonFactory.createGenerator(out, JsonEncoding.UTF8);

    jsonGenerator.writeStartArray();

    events.generate(new EventClient.EventPoster<T>() {
        @Override/*from   w ww.  ja  v  a 2s. c  o m*/
        public void post(T event) throws IOException {
            JsonSerializer<T> serializer = getSerializer(event, token);
            if (serializer == null) {
                throw new InvalidEventException("Event class [%s] has not been registered as an event",
                        event.getClass().getName());
            }

            serializer.serialize(event, jsonGenerator, null);
        }
    });

    jsonGenerator.writeEndArray();
    jsonGenerator.flush();
}

From source file:org.mongojack.internal.ObjectIdSerializer.java

@Override
public void serialize(Object value, JsonGenerator jgen, SerializerProvider provider)
        throws IOException, JsonProcessingException {
    if (value instanceof Iterable) {
        jgen.writeStartArray();
        for (Object item : (Iterable) value) {
            jgen.writeObject(serialiseObject(item));
        }/*from w ww . j a v  a  2 s  .  c o  m*/
        jgen.writeEndArray();
    } else {
        jgen.writeObject(serialiseObject(value));
    }
}

From source file:com.proofpoint.event.client.JsonEventWriter.java

public <T> Writer createEventWriter(final Iterator<T> eventIterator, final String token, final OutputStream out)
        throws IOException {
    checkNotNull(eventIterator, "eventIterator is null");
    checkNotNull(out, "out is null");

    final JsonGenerator jsonGenerator = jsonFactory.createGenerator(out, JsonEncoding.UTF8);

    jsonGenerator.writeStartArray();

    return new Writer() {
        @Override/*from w  ww.  ja v a2s  .c  om*/
        public void write() throws Exception {
            if (eventIterator.hasNext()) {
                T event = eventIterator.next();
                JsonSerializer<T> serializer = getSerializer(event, token);
                if (serializer == null) {
                    throw new InvalidEventException("Event class [%s] has not been registered as an event",
                            event.getClass().getName());
                }

                serializer.serialize(event, jsonGenerator, null);
            } else {
                jsonGenerator.writeEndArray();
                jsonGenerator.flush();
                out.close();
            }
        }
    };
}

From source file:org.emfjson.jackson.streaming.Values.java

public void serializeMany(JsonGenerator generator, String key, EDataType type, Collection<?> values) {
    try {/* ww w  .  ja  va 2s .com*/
        generator.writeFieldName(key);
        generator.writeStartArray();
        for (Object value : values) {
            writeValue(generator, type, value);
        }
        generator.writeEndArray();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:org.emfjson.jackson.streaming.References.java

public void serializeMany(JsonGenerator generator, String key, Collection<?> values) {
    try {//  w  ww .j ava  2 s .  c  o m
        generator.writeFieldName(key);
        generator.writeStartArray();
        for (Object current : values) {
            if (current instanceof EObject) {
                writeRef(generator, (EObject) current);
            }
        }
        generator.writeEndArray();
    } catch (IOException e) {
        e.printStackTrace();
    }

}