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:net.opentsdb.tsd.HttpJsonSerializer.java

/**
 * Format the results from a timeseries data query
 * @param data_query The TSQuery object used to fetch the results
 * @param results The data fetched from storage
 * @param globals An optional list of global annotation objects
 * @return A ChannelBuffer object to pass on to the caller
 *///ww  w  .ja v  a  2  s .  c om
public ChannelBuffer formatQueryV1(final TSQuery data_query, final List<DataPoints[]> results,
        final List<Annotation> globals) {

    final boolean as_arrays = this.query.hasQueryStringParam("arrays");
    final String jsonp = this.query.getQueryStringParam("jsonp");

    // todo - this should be streamed at some point since it could be HUGE
    final ChannelBuffer response = ChannelBuffers.dynamicBuffer();
    final OutputStream output = new ChannelBufferOutputStream(response);
    try {
        // don't forget jsonp
        if (jsonp != null && !jsonp.isEmpty()) {
            output.write((jsonp + "(").getBytes(query.getCharset()));
        }
        JsonGenerator json = JSON.getFactory().createGenerator(output);
        json.writeStartArray();

        for (DataPoints[] separate_dps : results) {
            for (DataPoints dps : separate_dps) {
                json.writeStartObject();

                json.writeStringField("metric", dps.metricName());

                json.writeFieldName("tags");
                json.writeStartObject();
                if (dps.getTags() != null) {
                    for (Map.Entry<String, String> tag : dps.getTags().entrySet()) {
                        json.writeStringField(tag.getKey(), tag.getValue());
                    }
                }
                json.writeEndObject();

                json.writeFieldName("aggregateTags");
                json.writeStartArray();
                if (dps.getAggregatedTags() != null) {
                    for (String atag : dps.getAggregatedTags()) {
                        json.writeString(atag);
                    }
                }
                json.writeEndArray();

                if (data_query.getShowTSUIDs()) {
                    json.writeFieldName("tsuids");
                    json.writeStartArray();
                    final List<String> tsuids = dps.getTSUIDs();
                    Collections.sort(tsuids);
                    for (String tsuid : tsuids) {
                        json.writeString(tsuid);
                    }
                    json.writeEndArray();
                }

                if (!data_query.getNoAnnotations()) {
                    final List<Annotation> annotations = dps.getAnnotations();
                    if (annotations != null) {
                        Collections.sort(annotations);
                        json.writeArrayFieldStart("annotations");
                        for (Annotation note : annotations) {
                            json.writeObject(note);
                        }
                        json.writeEndArray();
                    }

                    if (globals != null && !globals.isEmpty()) {
                        Collections.sort(globals);
                        json.writeArrayFieldStart("globalAnnotations");
                        for (Annotation note : globals) {
                            json.writeObject(note);
                        }
                        json.writeEndArray();
                    }
                }

                // now the fun stuff, dump the data
                json.writeFieldName("dps");

                // default is to write a map, otherwise write arrays
                if (as_arrays) {
                    json.writeStartArray();
                    for (final DataPoint dp : dps) {
                        if (dp.timestamp() < data_query.startTime() || dp.timestamp() > data_query.endTime()) {
                            continue;
                        }
                        final long timestamp = data_query.getMsResolution() ? dp.timestamp()
                                : dp.timestamp() / 1000;
                        json.writeStartArray();
                        json.writeNumber(timestamp);
                        if (dp.isInteger()) {
                            json.writeNumber(dp.longValue());
                        } else {
                            json.writeNumber(dp.doubleValue());
                        }
                        json.writeEndArray();
                    }
                    json.writeEndArray();
                } else {
                    json.writeStartObject();
                    for (final DataPoint dp : dps) {
                        if (dp.timestamp() < (data_query.startTime())
                                || dp.timestamp() > (data_query.endTime())) {
                            continue;
                        }
                        final long timestamp = data_query.getMsResolution() ? dp.timestamp()
                                : dp.timestamp() / 1000;
                        if (dp.isInteger()) {
                            json.writeNumberField(Long.toString(timestamp), dp.longValue());
                        } else {
                            json.writeNumberField(Long.toString(timestamp), dp.doubleValue());
                        }
                    }
                    json.writeEndObject();
                }

                // close the results for this particular query
                json.writeEndObject();
            }
        }

        // close
        json.writeEndArray();
        json.close();

        if (jsonp != null && !jsonp.isEmpty()) {
            output.write(")".getBytes());
        }
        return response;
    } catch (IOException e) {
        LOG.error("Unexpected exception", e);
        throw new RuntimeException(e);
    }
}

From source file:com.basho.riak.client.query.MapReduce.java

/**
 * @param jg// ww w.  ja v  a 2 s .c  o  m
 */
private void writeMapReducePhases(JsonGenerator jg) throws IOException {
    int cnt = 0;
    synchronized (phases) {
        final int lastPhase = phases.size();
        for (MapReducePhase phase : phases) {
            cnt++;
            jg.writeStartObject();
            jg.writeFieldName(phase.getType().toString());
            jg.writeStartObject();

            switch (phase.getType()) {
            case MAP:
            case REDUCE:
                MapPhase mapPhase = (MapPhase) phase;
                FunctionToJson.newWriter(mapPhase.getPhaseFunction(), jg).write();
                if (mapPhase.getArg() != null) {
                    jg.writeObjectField("arg", mapPhase.getArg());
                }
                break;
            case LINK:
                jg.writeStringField("bucket", ((LinkPhase) phase).getBucket());
                jg.writeStringField("tag", ((LinkPhase) phase).getTag());
                break;
            }

            //the final phase results should be returned, unless specifically set otherwise
            if (cnt == lastPhase) {
                jg.writeBooleanField("keep", isKeepResult(true, phase.isKeep()));
            } else {
                jg.writeBooleanField("keep", isKeepResult(false, phase.isKeep()));
            }

            jg.writeEndObject();
            jg.writeEndObject();
        }
    }
}

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

protected void writeBidFields(Bid bid, JsonGenerator gen) throws IOException {
    gen.writeStringField("id", bid.getId());
    gen.writeStringField("impid", bid.getImpid());
    gen.writeNumberField("price", bid.getPrice());
    if (bid.hasAdid()) {
        gen.writeStringField("adid", bid.getAdid());
    }//from ww w .  j  av a2  s . c  om
    if (bid.hasNurl()) {
        gen.writeStringField("nurl", bid.getNurl());
    }
    switch (bid.getAdmOneofCase()) {
    case ADM:
        gen.writeStringField("adm", bid.getAdm());
        break;
    case ADM_NATIVE:
        gen.writeFieldName("adm");
        if (factory().isForceNativeAsObject()) {
            nativeWriter().writeNativeResponse(bid.getAdmNative(), gen);
        } else {
            gen.writeString(nativeWriter().writeNativeResponse(bid.getAdmNative()));
        }
        break;
    case ADMONEOF_NOT_SET:
        checkRequired(false);
    }
    writeStrings("adomain", bid.getAdomainList(), gen);
    if (bid.hasBundle()) {
        gen.writeStringField("bundle", bid.getBundle());
    }
    if (bid.hasIurl()) {
        gen.writeStringField("iurl", bid.getIurl());
    }
    if (bid.hasCid()) {
        gen.writeStringField("cid", bid.getCid());
    }
    if (bid.hasCrid()) {
        gen.writeStringField("crid", bid.getCrid());
    }
    writeContentCategories("cat", bid.getCatList(), gen);
    writeEnums("attr", bid.getAttrList(), gen);
    if (bid.hasDealid()) {
        gen.writeStringField("dealid", bid.getDealid());
    }
    if (bid.hasW()) {
        gen.writeNumberField("w", bid.getW());
    }
    if (bid.hasH()) {
        gen.writeNumberField("h", bid.getH());
    }
    if (bid.hasApi()) {
        gen.writeNumberField("api", bid.getApi().getNumber());
    }
    if (bid.hasProtocol()) {
        gen.writeNumberField("protocol", bid.getProtocol().getNumber());
    }
    if (bid.hasQagmediarating()) {
        gen.writeNumberField("qagmediarating", bid.getQagmediarating().getNumber());
    }
    if (bid.hasExp()) {
        gen.writeNumberField("exp", bid.getExp());
    }
}

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

protected void writeUserFields(User user, JsonGenerator gen) throws IOException {
    if (user.hasId()) {
        gen.writeStringField("id", user.getId());
    }//from   w w w.  jav a2 s  .co  m
    if (user.hasBuyeruid()) {
        gen.writeStringField("buyeruid", user.getBuyeruid());
    }
    if (user.hasYob()) {
        gen.writeNumberField("yob", user.getYob());
    }
    if (user.hasGender() && Gender.forCode(user.getGender()) != null) {
        gen.writeStringField("gender", user.getGender());
    }
    if (user.hasKeywords()) {
        gen.writeStringField("keywords", user.getKeywords());
    }
    if (user.hasCustomdata()) {
        gen.writeStringField("customdata", user.getCustomdata());
    }
    if (user.hasGeo()) {
        gen.writeFieldName("geo");
        writeGeo(user.getGeo(), gen);
    }
    if (user.getDataCount() != 0) {
        gen.writeArrayFieldStart("data");
        for (Data data : user.getDataList()) {
            writeData(data, gen);
        }
        gen.writeEndArray();
    }
}

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

protected void writeAppFields(App app, JsonGenerator gen) throws IOException {
    if (app.hasId()) {
        gen.writeStringField("id", app.getId());
    }//w  w  w  .  j av  a 2  s  .c  om
    if (app.hasName()) {
        gen.writeStringField("name", app.getName());
    }
    if (app.hasBundle()) {
        gen.writeStringField("bundle", app.getBundle());
    }
    if (app.hasDomain()) {
        gen.writeStringField("domain", app.getDomain());
    }
    if (app.hasStoreurl()) {
        gen.writeStringField("storeurl", app.getStoreurl());
    }
    writeContentCategories("cat", app.getCatList(), gen);
    writeContentCategories("sectioncat", app.getSectioncatList(), gen);
    writeContentCategories("pagecat", app.getPagecatList(), gen);
    if (app.hasVer()) {
        gen.writeStringField("ver", app.getVer());
    }
    if (app.hasPrivacypolicy()) {
        writeIntBoolField("privacypolicy", app.getPrivacypolicy(), gen);
    }
    if (app.hasPaid()) {
        writeIntBoolField("paid", app.getPaid(), gen);
    }
    if (app.hasPublisher()) {
        gen.writeFieldName("publisher");
        writePublisher(app.getPublisher(), gen);
    }
    if (app.hasContent()) {
        gen.writeFieldName("content");
        writeContent(app.getContent(), gen);
    }
    if (app.hasKeywords()) {
        gen.writeStringField("keywords", app.getKeywords());
    }
}

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

protected void writeSiteFields(Site site, JsonGenerator gen) throws IOException {
    if (site.hasId()) {
        gen.writeStringField("id", site.getId());
    }/* w  w  w  .j a  v a  2s.  c  o m*/
    if (site.hasName()) {
        gen.writeStringField("name", site.getName());
    }
    if (site.hasDomain()) {
        gen.writeStringField("domain", site.getDomain());
    }
    writeContentCategories("cat", site.getCatList(), gen);
    writeContentCategories("sectioncat", site.getSectioncatList(), gen);
    writeContentCategories("pagecat", site.getPagecatList(), gen);
    if (site.hasPage()) {
        gen.writeStringField("page", site.getPage());
    }
    if (site.hasRef()) {
        gen.writeStringField("ref", site.getRef());
    }
    if (site.hasSearch()) {
        gen.writeStringField("search", site.getSearch());
    }
    if (site.hasMobile()) {
        writeIntBoolField("mobile", site.getMobile(), gen);
    }
    if (site.hasPrivacypolicy()) {
        writeIntBoolField("privacypolicy", site.getPrivacypolicy(), gen);
    }
    if (site.hasPublisher()) {
        gen.writeFieldName("publisher");
        writePublisher(site.getPublisher(), gen);
    }
    if (site.hasContent()) {
        gen.writeFieldName("content");
        writeContent(site.getContent(), gen);
    }
    if (site.hasKeywords()) {
        gen.writeStringField("keywords", site.getKeywords());
    }
}

From source file:de.escalon.hypermedia.spring.hydra.LinkListSerializer.java

private void writeActionDescriptors(JsonGenerator jgen, String currentVocab,
        List<ActionDescriptor> actionDescriptors) throws IOException, IntrospectionException {
    for (ActionDescriptor actionDescriptor : actionDescriptors) {
        if ("GET".equals(actionDescriptor.getHttpMethod())) {
            continue;
        }/*  w  w  w  . j a  v  a 2  s .  co  m*/

        jgen.writeStartObject(); // begin a hydra:Operation

        final String semanticActionType = actionDescriptor.getSemanticActionType();
        if (semanticActionType != null) {
            jgen.writeStringField("@type", semanticActionType);
        }
        jgen.writeStringField("hydra:method", actionDescriptor.getHttpMethod());

        final ActionInputParameter requestBodyInputParameter = actionDescriptor.getRequestBody();
        if (requestBodyInputParameter != null) {

            jgen.writeObjectFieldStart("hydra:expects"); // begin hydra:expects

            final Class<?> clazz = requestBodyInputParameter.getParameterType();
            final Expose classExpose = clazz.getAnnotation(Expose.class);
            final String typeName;
            if (classExpose != null) {
                typeName = classExpose.value();
            } else {
                typeName = requestBodyInputParameter.getParameterType().getSimpleName();
            }
            jgen.writeStringField("@type", typeName);

            jgen.writeArrayFieldStart("hydra:supportedProperty"); // begin hydra:supportedProperty
            // TODO check need for allRootParameters and requestBodyInputParameter here:
            recurseSupportedProperties(jgen, currentVocab, clazz, actionDescriptor, requestBodyInputParameter,
                    requestBodyInputParameter.getValue(), "");
            jgen.writeEndArray(); // end hydra:supportedProperty

            jgen.writeEndObject(); // end hydra:expects
        }

        jgen.writeEndObject(); // end hydra:Operation
    }
}

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

@SuppressWarnings("deprecation")
protected void writeContentFields(Content content, JsonGenerator gen) throws IOException {
    if (content.hasId()) {
        gen.writeStringField("id", content.getId());
    }//w  w  w. j a  va 2 s  .c  om
    if (content.hasEpisode()) {
        gen.writeNumberField("episode", content.getEpisode());
    }
    if (content.hasTitle()) {
        gen.writeStringField("title", content.getTitle());
    }
    if (content.hasSeries()) {
        gen.writeStringField("series", content.getSeries());
    }
    if (content.hasSeason()) {
        gen.writeStringField("season", content.getSeason());
    }
    if (content.hasProducer()) {
        gen.writeFieldName("producer");
        writeProducer(content.getProducer(), gen);
    }
    if (content.hasUrl()) {
        gen.writeStringField("url", content.getUrl());
    }
    writeContentCategories("cat", content.getCatList(), gen);
    if (content.hasVideoquality()) {
        gen.writeNumberField("videoquality", content.getVideoquality().getNumber());
    }
    if (content.hasContext()) {
        gen.writeNumberField("context", content.getContext().getNumber());
    }
    if (content.hasContentrating()) {
        gen.writeStringField("contentrating", content.getContentrating());
    }
    if (content.hasUserrating()) {
        gen.writeStringField("userrating", content.getUserrating());
    }
    if (content.hasQagmediarating()) {
        gen.writeNumberField("qagmediarating", content.getQagmediarating().getNumber());
    }
    if (content.hasKeywords()) {
        gen.writeStringField("keywords", content.getKeywords());
    }
    if (content.hasLivestream()) {
        writeIntBoolField("livestream", content.getLivestream(), gen);
    }
    if (content.hasSourcerelationship()) {
        writeIntBoolField("sourcerelationship", content.getSourcerelationship(), gen);
    }
    if (content.hasLen()) {
        gen.writeNumberField("len", content.getLen());
    }
    if (content.hasLanguage()) {
        gen.writeStringField("language", content.getLanguage());
    }
    if (content.hasEmbeddable()) {
        writeIntBoolField("embeddable", content.getEmbeddable(), gen);
    }
    if (content.hasArtist()) {
        gen.writeStringField("artist", content.getArtist());
    }
    if (content.hasGenre()) {
        gen.writeStringField("genre", content.getGenre());
    }
    if (content.hasAlbum()) {
        gen.writeStringField("album", content.getAlbum());
    }
    if (content.hasIsrc()) {
        gen.writeStringField("isrc", content.getIsrc());
    }
    if (content.hasProdq()) {
        gen.writeNumberField("prodq", content.getProdq().getNumber());
    }
}

From source file:com.evolveum.midpoint.prism.lex.json.AbstractJsonLexicalProcessor.java

@SuppressWarnings("unused") // TODO
private String serializeNsIfNeeded(QName subNodeName, String globalNamespace, JsonGenerator generator)
        throws IOException {
    if (subNodeName == null) {
        return globalNamespace;
    }//from w  ww .j  a v  a  2 s .co  m
    String subNodeNs = subNodeName.getNamespaceURI();
    if (StringUtils.isNotBlank(subNodeNs)) {
        if (!subNodeNs.equals(globalNamespace)) {
            globalNamespace = subNodeNs;
            generator.writeStringField(PROP_NAMESPACE, globalNamespace);

        }
    }
    return globalNamespace;
}

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

protected void writeDeviceFields(Device device, JsonGenerator gen) throws IOException {
    if (device.hasUa()) {
        gen.writeStringField("ua", device.getUa());
    }//  w  w  w .j  a v a 2  s . co m
    if (device.hasGeo()) {
        gen.writeFieldName("geo");
        writeGeo(device.getGeo(), gen);
    }
    if (device.hasDnt()) {
        writeIntBoolField("dnt", device.getDnt(), gen);
    }
    if (device.hasLmt()) {
        writeIntBoolField("lmt", device.getLmt(), gen);
    }
    if (device.hasIp()) {
        gen.writeStringField("ip", device.getIp());
    }
    if (device.hasIpv6()) {
        gen.writeStringField("ipv6", device.getIpv6());
    }
    if (device.hasDevicetype()) {
        gen.writeNumberField("devicetype", device.getDevicetype().getNumber());
    }
    if (device.hasMake()) {
        gen.writeStringField("make", device.getMake());
    }
    if (device.hasModel()) {
        gen.writeStringField("model", device.getModel());
    }
    if (device.hasOs()) {
        gen.writeStringField("os", device.getOs());
    }
    if (device.hasOsv()) {
        gen.writeStringField("osv", device.getOsv());
    }
    if (device.hasHwv()) {
        gen.writeStringField("hwv", device.getHwv());
    }
    if (device.hasW()) {
        gen.writeNumberField("w", device.getW());
    }
    if (device.hasH()) {
        gen.writeNumberField("h", device.getH());
    }
    if (device.hasPpi()) {
        gen.writeNumberField("ppi", device.getPpi());
    }
    if (device.hasPxratio()) {
        gen.writeNumberField("pxratio", device.getPxratio());
    }
    if (device.hasJs()) {
        writeIntBoolField("js", device.getJs(), gen);
    }
    if (device.hasFlashver()) {
        gen.writeStringField("flashver", device.getFlashver());
    }
    if (device.hasLanguage()) {
        gen.writeStringField("language", device.getLanguage());
    }
    if (device.hasCarrier()) {
        gen.writeStringField("carrier", device.getCarrier());
    }
    if (device.hasConnectiontype()) {
        gen.writeNumberField("connectiontype", device.getConnectiontype().getNumber());
    }
    if (device.hasIfa()) {
        gen.writeStringField("ifa", device.getIfa());
    }
    if (device.hasDidsha1()) {
        gen.writeStringField("didsha1", device.getDidsha1());
    }
    if (device.hasDidmd5()) {
        gen.writeStringField("didmd5", device.getDidmd5());
    }
    if (device.hasDpidsha1()) {
        gen.writeStringField("dpidsha1", device.getDpidsha1());
    }
    if (device.hasDpidmd5()) {
        gen.writeStringField("dpidmd5", device.getDpidmd5());
    }
    if (device.hasMacsha1()) {
        gen.writeStringField("macsha1", device.getMacsha1());
    }
    if (device.hasMacmd5()) {
        gen.writeStringField("macmd5", device.getMacmd5());
    }
    if (device.hasGeofetch()) {
        writeIntBoolField("geofetch", device.getGeofetch(), gen);
    }
}