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

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

Introduction

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

Prototype

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

Source Link

Document

Convenience method for outputting a field entry ("member") that has JSON literal value null.

Usage

From source file:io.mesosphere.mesos.frameworks.cassandra.scheduler.api.NodeController.java

private Response nodeStatusUpdate(final CassandraFrameworkProtos.CassandraNode cassandraNode) {
    if (cassandraNode == null) {
        return Response.status(Response.Status.NOT_FOUND).build();
    }// w ww .  j  av  a 2 s. c o  m

    return JaxRsUtils.buildStreamingResponse(factory, new StreamingJsonResponse() {
        @Override
        public void write(final JsonGenerator json) throws IOException {
            json.writeStringField("ip", cassandraNode.getIp());
            json.writeStringField("hostname", cassandraNode.getHostname());
            if (!cassandraNode.hasCassandraNodeExecutor()) {
                json.writeNullField("executorId");
            } else {
                json.writeStringField("executorId", cassandraNode.getCassandraNodeExecutor().getExecutorId());
            }
            json.writeStringField("targetRunState", cassandraNode.getTargetRunState().name());
        }
    });
}

From source file:com.predic8.membrane.core.interceptor.administration.AdminRESTInterceptor.java

private void writeExchange(AbstractExchange exc, JsonGenerator gen)
        throws IOException, JsonGenerationException, SQLException {
    gen.writeStartObject();//from   w  w w .java  2  s .  co m
    gen.writeNumberField("id", exc.getId());
    if (exc.getResponse() != null) {
        gen.writeNumberField("statusCode", exc.getResponse().getStatusCode());
        if (exc.getResponseContentLength() != -1) {
            gen.writeNumberField("respContentLength", exc.getResponseContentLength());
        } else {
            gen.writeNullField("respContentLength");
        }
    } else {
        gen.writeNullField("statusCode");
        gen.writeNullField("respContentLength");
    }
    gen.writeStringField("time", ExchangesUtil.getTime(exc));
    gen.writeStringField("proxy", exc.getRule().toString());
    gen.writeNumberField("listenPort", exc.getRule().getKey().getPort());
    gen.writeStringField("method", exc.getRequest().getMethod());
    gen.writeStringField("path", exc.getRequest().getUri());
    gen.writeStringField("client", exc.getRemoteAddr());
    gen.writeStringField("server", exc.getServer());
    gen.writeNumberField("serverPort", getServerPort(exc));
    gen.writeStringField("reqContentType", exc.getRequestContentType());
    if (exc.getRequestContentLength() != -1) {
        gen.writeNumberField("reqContentLength", exc.getRequestContentLength());
    } else {
        gen.writeNullField("reqContentLength");
    }

    gen.writeStringField("respContentType", exc.getResponseContentType());
    gen.writeStringField("respContentLength", exc.getResponseContentType());

    gen.writeNumberField("duration", exc.getTimeResReceived() - exc.getTimeReqSent());
    gen.writeStringField("msgFilePath", JDBCUtil.getFilePath(exc));
    gen.writeEndObject();
}

From source file:io.mesosphere.mesos.frameworks.cassandra.scheduler.api.NodeController.java

private Response nodeUpdateSeed(final String node, final boolean seed) {
    final CassandraFrameworkProtos.CassandraNode cassandraNode = cluster.findNode(node);
    if (cassandraNode == null) {
        return Response.status(404).build();
    }//from ww w. j a v  a2  s  .  c  om

    try {
        final boolean seedChanged = cluster.setNodeSeed(cassandraNode, seed);

        return JaxRsUtils.buildStreamingResponse(factory, new StreamingJsonResponse() {
            @Override
            public void write(final JsonGenerator json) throws IOException {
                json.writeStringField("ip", cassandraNode.getIp());
                json.writeStringField("hostname", cassandraNode.getHostname());
                if (!cassandraNode.hasCassandraNodeExecutor()) {
                    json.writeNullField("executorId");
                } else {
                    json.writeStringField("executorId",
                            cassandraNode.getCassandraNodeExecutor().getExecutorId());
                }
                json.writeBooleanField("oldSeedState", cassandraNode.getSeed());

                if (seedChanged) {
                    json.writeBooleanField("success", true);
                    json.writeBooleanField("seedState", seed);
                } else {
                    json.writeBooleanField("success", false);
                    json.writeBooleanField("seedState", cassandraNode.getSeed());
                }

            }
        });
    } catch (final SeedChangeException e) {
        return JaxRsUtils.buildStreamingResponse(factory, Response.Status.BAD_REQUEST,
                new StreamingJsonResponse() {
                    @Override
                    public void write(final JsonGenerator json) throws IOException {
                        json.writeStringField("ip", cassandraNode.getIp());
                        json.writeStringField("hostname", cassandraNode.getHostname());
                        if (!cassandraNode.hasCassandraNodeExecutor()) {
                            json.writeNullField("executorId");
                        } else {
                            json.writeStringField("executorId",
                                    cassandraNode.getCassandraNodeExecutor().getExecutorId());
                        }
                        json.writeBooleanField("oldSeedState", cassandraNode.getSeed());

                        json.writeBooleanField("success", false);
                        json.writeStringField("error", e.getMessage());
                    }
                });
    }

}

From source file:org.n52.ar.layar.LayarResponse.java

public void toJSON(final JsonGenerator generator) throws IOException {
    generator.writeStartObject();/*from  w ww .j av a2s  . c  om*/

    /*
     * mandatory:
     */
    generator.writeStringField("layer", this.layer);
    if (this.errorCode != CODE_ERROR && this.hotspots.size() < 1) {
        this.errorString = "No POI found. Please increase the search range to try again.";
        this.errorCode = CODE_ERROR;
    }
    generator.writeStringField("errorString", this.errorString);
    generator.writeNumberField("errorCode", this.errorCode);

    // paging is not implemented yet
    generator.writeBooleanField("morePages", this.morePages);
    if (this.nextPageKey != null) {
        generator.writeStringField("nextPageKey", this.nextPageKey);
    } else {
        generator.writeNullField("nextPageKey");
    }
    // generator.writeNumberField("refreshInterval", 3600);
    // generator.writeNumberField("refreshDistance", 50);
    // generator.writeBooleanField("fullRefresh", false);
    // actions for the entire layer: http://layar.com/documentation/browser/api/getpois-response/actions/
    if (this.errorCode == CODE_OK)
        this.showMessage = "You got " + this.hotspots.size() + " hits in the area around you!";
    generator.writeStringField("showMessage", this.showMessage);
    // deletedHotspots
    // animations
    // showDialog: title, description, iconURL, actions
    generator.writeNullField("biwStyle");

    /*
     * hotspots (mandatory):
     */
    createHotspots(generator);

    generator.writeEndObject();
}

From source file:net.opentsdb.meta.UIDMeta.java

/**
 * Formats the JSON output for writing to storage. It drops objects we don't
 * need or want to store (such as the UIDMeta objects or the total dps) to
 * save space. It also serializes in order so that we can make a proper CAS
 * call. Otherwise the POJO serializer may place the fields in any order
 * and CAS calls would fail all the time.
 * @return A byte array to write to storage
 *//*from   w w w  .  jav a2 s .co m*/
private byte[] getStorageJSON() {
    // 256 bytes is a good starting value, assumes default info
    final ByteArrayOutputStream output = new ByteArrayOutputStream(256);
    try {
        final JsonGenerator json = JSON.getFactory().createGenerator(output);
        json.writeStartObject();
        json.writeStringField("type", type.toString());
        json.writeStringField("displayName", display_name);
        json.writeStringField("description", description);
        json.writeStringField("notes", notes);
        json.writeNumberField("created", created);
        if (custom == null) {
            json.writeNullField("custom");
        } else {
            json.writeObjectFieldStart("custom");
            for (Map.Entry<String, String> entry : custom.entrySet()) {
                json.writeStringField(entry.getKey(), entry.getValue());
            }
            json.writeEndObject();
        }

        json.writeEndObject();
        json.close();
        return output.toByteArray();
    } catch (IOException e) {
        throw new RuntimeException("Unable to serialize UIDMeta", e);
    }
}

From source file:net.opentsdb.meta.Annotation.java

/**
 * Serializes the object in a uniform matter for storage. Needed for 
 * successful CAS calls//from  w  w w .jav a  2s.  c  o  m
 * @return The serialized object as a byte array
 */
private byte[] getStorageJSON() {
    // TODO - precalculate size
    final ByteArrayOutputStream output = new ByteArrayOutputStream();
    try {
        final JsonGenerator json = JSON.getFactory().createGenerator(output);
        json.writeStartObject();
        if (tsuid != null && !tsuid.isEmpty()) {
            json.writeStringField("tsuid", tsuid);
        }
        json.writeNumberField("startTime", start_time);
        json.writeNumberField("endTime", end_time);
        json.writeStringField("description", description);
        json.writeStringField("notes", notes);
        if (custom == null) {
            json.writeNullField("custom");
        } else {
            json.writeObjectFieldStart("custom");
            for (Map.Entry<String, String> entry : custom.entrySet()) {
                json.writeStringField(entry.getKey(), entry.getValue());
            }
            json.writeEndObject();
        }

        json.writeEndObject();
        json.close();
        return output.toByteArray();
    } catch (IOException e) {
        throw new RuntimeException("Unable to serialize Annotation", e);
    }
}

From source file:net.opentsdb.meta.TSMeta.java

/**
 * Formats the JSON output for writing to storage. It drops objects we don't
 * need or want to store (such as the UIDMeta objects or the total dps) to
 * save space. It also serializes in order so that we can make a proper CAS
 * call. Otherwise the POJO serializer may place the fields in any order
 * and CAS calls would fail all the time.
 * @return A byte array to write to storage
 *//*from   www. jav a2  s  .  co  m*/
private byte[] getStorageJSON() {
    // 256 bytes is a good starting value, assumes default info
    final ByteArrayOutputStream output = new ByteArrayOutputStream(256);
    try {
        final JsonGenerator json = JSON.getFactory().createGenerator(output);
        json.writeStartObject();
        json.writeStringField("tsuid", tsuid);
        json.writeStringField("displayName", display_name);
        json.writeStringField("description", description);
        json.writeStringField("notes", notes);
        json.writeNumberField("created", created);
        if (custom == null) {
            json.writeNullField("custom");
        } else {
            json.writeObjectFieldStart("custom");
            for (Map.Entry<String, String> entry : custom.entrySet()) {
                json.writeStringField(entry.getKey(), entry.getValue());
            }
            json.writeEndObject();
        }
        json.writeStringField("units", units);
        json.writeStringField("dataType", data_type);
        json.writeNumberField("retention", retention);
        json.writeNumberField("max", max);
        json.writeNumberField("min", min);

        json.writeEndObject();
        json.close();
        return output.toByteArray();
    } catch (IOException e) {
        throw new RuntimeException("Unable to serialize TSMeta", e);
    }
}

From source file:com.msopentech.odatajclient.engine.data.json.DOMTreeUtilsV3.java

/**
 * Serializes DOM content as JSON./*from w ww.jav  a 2  s.  com*/
 *
 * @param client OData client.
 * @param jgen JSON generator.
 * @param content content.
 * @param propType whether to output type information in the way needed for property values or not.
 * @throws IOException in case of write error.
 */
public static void writeSubtree(final ODataClient client, final JsonGenerator jgen, final Node content,
        final boolean propType) throws IOException {

    for (Node child : XMLUtils.getChildNodes(content, Node.ELEMENT_NODE)) {
        final String childName = XMLUtils.getSimpleName(child);

        final Node typeAttr = child.getAttributes().getNamedItem(ODataConstants.ATTR_M_TYPE);
        if (typeAttr != null && EdmSimpleType.isGeospatial(typeAttr.getTextContent())) {
            jgen.writeStringField(
                    propType ? ODataConstants.JSON_TYPE : childName + "@" + ODataConstants.JSON_TYPE,
                    typeAttr.getTextContent());

            jgen.writeObjectFieldStart(childName);
            GeospatialJSONHandler.serialize(jgen, (Element) child, typeAttr.getTextContent());
            jgen.writeEndObject();
        } else if (XMLUtils.hasOnlyTextChildNodes(child)) {
            if (child.hasChildNodes()) {
                final String out;
                if (typeAttr == null) {
                    out = child.getChildNodes().item(0).getNodeValue();
                } else {
                    final EdmSimpleType type = EdmSimpleType.fromValue(typeAttr.getTextContent());
                    final ODataPrimitiveValue value = client.getPrimitiveValueBuilder().setType(type)
                            .setText(child.getChildNodes().item(0).getNodeValue()).build();
                    out = value.toString();

                    jgen.writeStringField(childName + "@" + ODataConstants.JSON_TYPE, type.toString());
                }
                jgen.writeStringField(childName, out);
            } else {
                if (child.getAttributes().getNamedItem(ODataConstants.ATTR_NULL) == null) {
                    if (typeAttr != null && EdmSimpleType.String.toString().equals(typeAttr.getTextContent())) {
                        jgen.writeStringField(childName + "@" + ODataConstants.JSON_TYPE,
                                typeAttr.getTextContent());
                        jgen.writeStringField(childName, StringUtils.EMPTY);
                    } else {
                        jgen.writeArrayFieldStart(childName);
                        jgen.writeEndArray();
                    }
                } else {
                    jgen.writeNullField(childName);
                }
            }
        } else {
            if (XMLUtils.hasElementsChildNode(child)) {
                jgen.writeArrayFieldStart(childName);

                for (Node nephew : XMLUtils.getChildNodes(child, Node.ELEMENT_NODE)) {
                    if (XMLUtils.hasOnlyTextChildNodes(nephew)) {
                        jgen.writeString(nephew.getChildNodes().item(0).getNodeValue());
                    } else {
                        jgen.writeStartObject();
                        DOMTreeUtils.writeSubtree(client, jgen, nephew);
                        jgen.writeEndObject();
                    }
                }

                jgen.writeEndArray();
            } else {
                jgen.writeObjectFieldStart(childName);
                if (typeAttr != null) {
                    jgen.writeStringField(ODataConstants.JSON_TYPE, typeAttr.getTextContent());
                }

                DOMTreeUtils.writeSubtree(client, jgen, child);

                jgen.writeEndObject();
            }
        }
    }
}

From source file:com.msopentech.odatajclient.engine.data.json.DOMTreeUtils.java

/**
 * Serializes DOM content as JSON.//from w  w w .  j a v  a  2 s  .  c  o m
 *
 * @param jgen JSON generator.
 * @param content content.
 * @param propType whether to output type information in the way needed for property values or not.
 * @throws IOException in case of write error.
 */
public static void writeSubtree(final JsonGenerator jgen, final Node content, final boolean propType)
        throws IOException {

    for (Node child : XMLUtils.getChildNodes(content, Node.ELEMENT_NODE)) {
        final String childName = XMLUtils.getSimpleName(child);

        final Node typeAttr = child.getAttributes().getNamedItem(ODataConstants.ATTR_M_TYPE);
        if (typeAttr != null && EdmSimpleType.isGeospatial(typeAttr.getTextContent())) {
            jgen.writeStringField(
                    propType ? ODataConstants.JSON_TYPE : childName + "@" + ODataConstants.JSON_TYPE,
                    typeAttr.getTextContent());

            jgen.writeObjectFieldStart(childName);
            GeospatialJSONHandler.serialize(jgen, (Element) child, typeAttr.getTextContent());
            jgen.writeEndObject();
        } else if (XMLUtils.hasOnlyTextChildNodes(child)) {
            if (child.hasChildNodes()) {
                final String out;
                if (typeAttr == null) {
                    out = child.getChildNodes().item(0).getNodeValue();
                } else {
                    final EdmSimpleType type = EdmSimpleType.fromValue(typeAttr.getTextContent());
                    final ODataPrimitiveValue value = new ODataPrimitiveValue.Builder().setType(type)
                            .setText(child.getChildNodes().item(0).getNodeValue()).build();
                    out = value.toString();

                    jgen.writeStringField(childName + "@" + ODataConstants.JSON_TYPE, type.toString());
                }
                jgen.writeStringField(childName, out);
            } else {
                if (child.getAttributes().getNamedItem(ODataConstants.ATTR_NULL) == null) {
                    if (typeAttr != null && EdmSimpleType.String.toString().equals(typeAttr.getTextContent())) {
                        jgen.writeStringField(childName + "@" + ODataConstants.JSON_TYPE,
                                typeAttr.getTextContent());
                        jgen.writeStringField(childName, StringUtils.EMPTY);
                    } else {
                        jgen.writeArrayFieldStart(childName);
                        jgen.writeEndArray();
                    }
                } else {
                    jgen.writeNullField(childName);
                }
            }
        } else {
            if (XMLUtils.hasElementsChildNode(child)) {
                jgen.writeArrayFieldStart(childName);

                for (Node nephew : XMLUtils.getChildNodes(child, Node.ELEMENT_NODE)) {
                    if (XMLUtils.hasOnlyTextChildNodes(nephew)) {
                        jgen.writeString(nephew.getChildNodes().item(0).getNodeValue());
                    } else {
                        jgen.writeStartObject();
                        writeSubtree(jgen, nephew);
                        jgen.writeEndObject();
                    }
                }

                jgen.writeEndArray();
            } else {
                jgen.writeObjectFieldStart(childName);
                if (typeAttr != null) {
                    jgen.writeStringField(ODataConstants.JSON_TYPE, typeAttr.getTextContent());
                }

                writeSubtree(jgen, child);

                jgen.writeEndObject();
            }
        }
    }
}

From source file:com.msopentech.odatajclient.engine.data.impl.JSONDOMTreeUtils.java

/**
 * Serializes DOM content as JSON.//  w w w .  j a va  2  s  .c  o  m
 *
 * @param client OData client.
 * @param jgen JSON generator.
 * @param content content.
 * @param propType whether to output type information in the way needed for property values or not.
 * @throws IOException in case of write error.
 */
public static void writeSubtree(final ODataClient client, final JsonGenerator jgen, final Node content,
        final boolean propType) throws IOException {

    for (Node child : XMLUtils.getChildNodes(content, Node.ELEMENT_NODE)) {
        final String childName = XMLUtils.getSimpleName(child);

        final Node typeAttr = child.getAttributes().getNamedItem(ODataConstants.ATTR_M_TYPE);
        if (typeAttr != null && EdmSimpleType.isGeospatial(typeAttr.getTextContent())) {
            jgen.writeStringField(
                    propType ? ODataConstants.JSON_TYPE : childName + "@" + ODataConstants.JSON_TYPE,
                    typeAttr.getTextContent());

            jgen.writeObjectFieldStart(childName);
            GeospatialJSONHandler.serialize(client, jgen, (Element) child, typeAttr.getTextContent());
            jgen.writeEndObject();
        } else if (XMLUtils.hasOnlyTextChildNodes(child)) {
            if (child.hasChildNodes()) {
                final String out;
                if (typeAttr == null) {
                    out = child.getChildNodes().item(0).getNodeValue();
                } else {
                    final EdmSimpleType type = EdmSimpleType.fromValue(typeAttr.getTextContent());
                    final ODataPrimitiveValue value = client.getPrimitiveValueBuilder().setType(type)
                            .setText(child.getChildNodes().item(0).getNodeValue()).build();
                    out = value.toString();

                    jgen.writeStringField(childName + "@" + ODataConstants.JSON_TYPE, type.toString());
                }
                jgen.writeStringField(childName, out);
            } else {
                if (child.getAttributes().getNamedItem(ODataConstants.ATTR_NULL) == null) {
                    if (typeAttr != null && EdmSimpleType.String.toString().equals(typeAttr.getTextContent())) {
                        jgen.writeStringField(childName + "@" + ODataConstants.JSON_TYPE,
                                typeAttr.getTextContent());
                        jgen.writeStringField(childName, StringUtils.EMPTY);
                    } else {
                        jgen.writeArrayFieldStart(childName);
                        jgen.writeEndArray();
                    }
                } else {
                    jgen.writeNullField(childName);
                }
            }
        } else {
            if (XMLUtils.hasElementsChildNode(child)) {
                jgen.writeArrayFieldStart(childName);

                for (Node nephew : XMLUtils.getChildNodes(child, Node.ELEMENT_NODE)) {
                    if (XMLUtils.hasOnlyTextChildNodes(nephew)) {
                        jgen.writeString(nephew.getChildNodes().item(0).getNodeValue());
                    } else {
                        jgen.writeStartObject();
                        writeSubtree(client, jgen, nephew);
                        jgen.writeEndObject();
                    }
                }

                jgen.writeEndArray();
            } else {
                jgen.writeObjectFieldStart(childName);
                if (typeAttr != null) {
                    jgen.writeStringField(ODataConstants.JSON_TYPE, typeAttr.getTextContent());
                }

                writeSubtree(client, jgen, child);

                jgen.writeEndObject();
            }
        }
    }
}