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

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

Introduction

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

Prototype

public abstract void writeFieldName(SerializableString name) throws IOException, JsonGenerationException;

Source Link

Document

Method similar to #writeFieldName(String) , main difference being that it may perform better as some of processing (such as quoting of certain characters, or encoding into external encoding if supported by generator) can be done just once and reused for later calls.

Usage

From source file:org.apache.olingo.client.core.serialization.JsonSerializer.java

protected void clientLinks(final Linked linked, final JsonGenerator jgen)
        throws IOException, EdmPrimitiveTypeException {

    final Map<String, List<String>> entitySetLinks = new HashMap<String, List<String>>();
    for (Link link : linked.getNavigationLinks()) {
        for (Annotation annotation : link.getAnnotations()) {
            valuable(jgen, annotation, link.getTitle() + "@" + annotation.getTerm());
        }/*from w w  w.  ja  va2  s  .c  o m*/

        if (isEntitySetNavigation(link)) {
            final List<String> uris;
            if (entitySetLinks.containsKey(link.getTitle())) {
                uris = entitySetLinks.get(link.getTitle());
            } else {
                uris = new ArrayList<String>();
                entitySetLinks.put(link.getTitle(), uris);
            }
            if (StringUtils.isNotBlank(link.getHref())) {
                uris.add(link.getHref());
            }
        } else {
            if (StringUtils.isNotBlank(link.getHref())) {
                jgen.writeStringField(link.getTitle() + Constants.JSON_BIND_LINK_SUFFIX, link.getHref());
            }
        }

        if (link.getInlineEntity() != null) {
            jgen.writeFieldName(link.getTitle());
            new JsonEntitySerializer(serverMode, contentType).doSerialize(link.getInlineEntity(), jgen);
        } else if (link.getInlineEntitySet() != null) {
            jgen.writeArrayFieldStart(link.getTitle());
            final JsonEntitySerializer entitySerializer = new JsonEntitySerializer(serverMode, contentType);
            for (Entity subEntry : link.getInlineEntitySet().getEntities()) {
                entitySerializer.doSerialize(subEntry, jgen);
            }
            jgen.writeEndArray();
        }
    }
    for (Map.Entry<String, List<String>> entitySetLink : entitySetLinks.entrySet()) {
        if (!entitySetLink.getValue().isEmpty()) {
            jgen.writeArrayFieldStart(entitySetLink.getKey() + Constants.JSON_BIND_LINK_SUFFIX);
            for (String uri : entitySetLink.getValue()) {
                jgen.writeString(uri);
            }
            jgen.writeEndArray();
        }
    }
}

From source file:org.apache.olingo.client.core.serialization.JsonSerializer.java

protected void serverLinks(final Linked linked, final JsonGenerator jgen)
        throws IOException, EdmPrimitiveTypeException {
    if (linked instanceof Entity) {
        for (Link link : ((Entity) linked).getMediaEditLinks()) {
            if (StringUtils.isNotBlank(link.getHref())) {
                jgen.writeStringField(/*from   w w w  . j av a  2s  .c  o m*/
                        link.getTitle() + StringUtils.prependIfMissing(Constants.JSON_MEDIA_EDIT_LINK, "@"),
                        link.getHref());
            }
        }
    }

    for (Link link : linked.getAssociationLinks()) {
        if (StringUtils.isNotBlank(link.getHref())) {
            jgen.writeStringField(link.getTitle() + Constants.JSON_ASSOCIATION_LINK, link.getHref());
        }
    }

    for (Link link : linked.getNavigationLinks()) {
        for (Annotation annotation : link.getAnnotations()) {
            valuable(jgen, annotation, link.getTitle() + "@" + annotation.getTerm());
        }

        if (StringUtils.isNotBlank(link.getHref())) {
            jgen.writeStringField(link.getTitle() + Constants.JSON_NAVIGATION_LINK, link.getHref());
        }

        if (link.getInlineEntity() != null) {
            jgen.writeFieldName(link.getTitle());
            new JsonEntitySerializer(serverMode, contentType).doSerialize(link.getInlineEntity(), jgen);
        } else if (link.getInlineEntitySet() != null) {
            jgen.writeArrayFieldStart(link.getTitle());
            JsonEntitySerializer entitySerializer = new JsonEntitySerializer(serverMode, contentType);
            for (Entity subEntry : link.getInlineEntitySet().getEntities()) {
                entitySerializer.doSerialize(subEntry, jgen);
            }
            jgen.writeEndArray();
        }
    }
}

From source file:org.apache.olingo.server.core.serializer.json.ODataJsonSerializer.java

protected void writeProperty(final ServiceMetadata metadata, final EdmProperty edmProperty,
        final Property property, final Set<List<String>> selectedPaths, final JsonGenerator json,
        Set<List<String>> expandedPaths, Linked linked, ExpandOption expand)
        throws IOException, SerializerException {
    boolean isStreamProperty = isStreamProperty(edmProperty);
    writePropertyType(edmProperty, json);
    if (!isStreamProperty) {
        json.writeFieldName(edmProperty.getName());
    }/*  w  ww.jav  a 2  s. c  om*/
    if (property == null || property.isNull()) {
        if (edmProperty.isNullable() == Boolean.FALSE && !isStreamProperty) {
            throw new SerializerException("Non-nullable property not present!",
                    SerializerException.MessageKeys.MISSING_PROPERTY, edmProperty.getName());
        } else {
            if (!isStreamProperty) {
                if (edmProperty.isCollection()) {
                    json.writeStartArray();
                    json.writeEndArray();
                } else {
                    json.writeNull();
                }
            }
        }
    } else {
        writePropertyValue(metadata, edmProperty, property, selectedPaths, json, expandedPaths, linked, expand);
    }
}

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 w  ww .  j a  v a  2s .  c o m*/
    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

@SuppressWarnings("deprecation")
protected void writeContentFields(Content content, JsonGenerator gen) throws IOException {
    if (content.hasId()) {
        gen.writeStringField("id", content.getId());
    }/* ww w .j  a  v  a 2 s. com*/
    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:org.apache.olingo.commons.core.serialization.JsonSerializer.java

protected void serverLinks(final Linked linked, final JsonGenerator jgen)
        throws IOException, EdmPrimitiveTypeException {
    if (linked instanceof Entity) {
        for (Link link : ((Entity) linked).getMediaEditLinks()) {
            if (StringUtils.isNotBlank(link.getHref())) {
                jgen.writeStringField(/* w w w.j  a  v  a  2s  . c  o m*/
                        link.getTitle() + StringUtils.prependIfMissing(
                                version.getJsonName(ODataServiceVersion.JsonKey.MEDIA_EDIT_LINK), "@"),
                        link.getHref());
            }
        }
    }

    for (Link link : linked.getAssociationLinks()) {
        if (StringUtils.isNotBlank(link.getHref())) {
            jgen.writeStringField(
                    link.getTitle() + version.getJsonName(ODataServiceVersion.JsonKey.ASSOCIATION_LINK),
                    link.getHref());
        }
    }

    for (Link link : linked.getNavigationLinks()) {
        for (Annotation annotation : link.getAnnotations()) {
            valuable(jgen, annotation, link.getTitle() + "@" + annotation.getTerm());
        }

        if (StringUtils.isNotBlank(link.getHref())) {
            jgen.writeStringField(
                    link.getTitle() + version.getJsonName(ODataServiceVersion.JsonKey.NAVIGATION_LINK),
                    link.getHref());
        }

        if (link.getInlineEntity() != null) {
            jgen.writeFieldName(link.getTitle());
            new JsonEntitySerializer(version, serverMode).doSerialize(link.getInlineEntity(), jgen);
        } else if (link.getInlineEntitySet() != null) {
            jgen.writeArrayFieldStart(link.getTitle());
            JsonEntitySerializer entitySerializer = new JsonEntitySerializer(version, serverMode);
            for (Entity subEntry : link.getInlineEntitySet().getEntities()) {
                entitySerializer.doSerialize(subEntry, jgen);
            }
            jgen.writeEndArray();
        }
    }
}

From source file:org.apache.olingo.commons.core.serialization.JsonSerializer.java

protected void clientLinks(final Linked linked, final JsonGenerator jgen)
        throws IOException, EdmPrimitiveTypeException {

    final Map<String, List<String>> entitySetLinks = new HashMap<String, List<String>>();
    for (Link link : linked.getNavigationLinks()) {
        for (Annotation annotation : link.getAnnotations()) {
            valuable(jgen, annotation, link.getTitle() + "@" + annotation.getTerm());
        }//ww w . j  a v a 2s  .c om

        ODataLinkType type = null;
        try {
            type = ODataLinkType.fromString(version, link.getRel(), link.getType());
        } catch (IllegalArgumentException e) {
            // ignore
        }

        if (type == ODataLinkType.ENTITY_SET_NAVIGATION) {
            final List<String> uris;
            if (entitySetLinks.containsKey(link.getTitle())) {
                uris = entitySetLinks.get(link.getTitle());
            } else {
                uris = new ArrayList<String>();
                entitySetLinks.put(link.getTitle(), uris);
            }
            if (StringUtils.isNotBlank(link.getHref())) {
                uris.add(link.getHref());
            }
        } else {
            if (StringUtils.isNotBlank(link.getHref())) {
                jgen.writeStringField(link.getTitle() + Constants.JSON_BIND_LINK_SUFFIX, link.getHref());
            }
        }

        if (link.getInlineEntity() != null) {
            jgen.writeFieldName(link.getTitle());
            new JsonEntitySerializer(version, serverMode).doSerialize(link.getInlineEntity(), jgen);
        } else if (link.getInlineEntitySet() != null) {
            jgen.writeArrayFieldStart(link.getTitle());
            final JsonEntitySerializer entitySerializer = new JsonEntitySerializer(version, serverMode);
            for (Entity subEntry : link.getInlineEntitySet().getEntities()) {
                entitySerializer.doSerialize(subEntry, jgen);
            }
            jgen.writeEndArray();
        }
    }
    for (Map.Entry<String, List<String>> entitySetLink : entitySetLinks.entrySet()) {
        if (!entitySetLink.getValue().isEmpty()) {
            jgen.writeArrayFieldStart(entitySetLink.getKey() + Constants.JSON_BIND_LINK_SUFFIX);
            for (String uri : entitySetLink.getValue()) {
                jgen.writeString(uri);
            }
            jgen.writeEndArray();
        }
    }
}

From source file:com.buaa.cfs.conf.Configuration.java

/**
 * Writes out all the parameters and their properties (final and resource) to the given {@link Writer} The format of
 * the output would be { "properties" : [ {key1,value1,key1.isFinal,key1.resource}, {key2,value2,
 * key2.isFinal,key2.resource}... ] } It does not output the parameters of the configuration object which is loaded
 * from an input stream.//from  w w  w. ja v  a  2  s.c  o m
 *
 * @param out the Writer to write to
 *
 * @throws IOException
 */
public static void dumpConfiguration(Configuration config, Writer out) throws IOException {
    JsonFactory dumpFactory = new JsonFactory();
    JsonGenerator dumpGenerator = dumpFactory.createJsonGenerator(out);
    dumpGenerator.writeStartObject();
    dumpGenerator.writeFieldName("properties");
    dumpGenerator.writeStartArray();
    dumpGenerator.flush();
    synchronized (config) {
        for (Entry<Object, Object> item : config.getProps().entrySet()) {
            dumpGenerator.writeStartObject();
            dumpGenerator.writeStringField("key", (String) item.getKey());
            dumpGenerator.writeStringField("value", config.get((String) item.getKey()));
            dumpGenerator.writeBooleanField("isFinal", config.finalParameters.contains(item.getKey()));
            String[] resources = config.updatingResource.get(item.getKey());
            String resource = UNKNOWN_RESOURCE;
            if (resources != null && resources.length > 0) {
                resource = resources[0];
            }
            dumpGenerator.writeStringField("resource", resource);
            dumpGenerator.writeEndObject();
        }
    }
    dumpGenerator.writeEndArray();
    dumpGenerator.writeEndObject();
    dumpGenerator.flush();
}

From source file:org.apache.olingo.server.core.serializer.json.ODataJsonSerializer.java

@Override
public SerializerResult primitiveCollection(final ServiceMetadata metadata, final EdmPrimitiveType type,
        final Property property, final PrimitiveSerializerOptions options) throws SerializerException {
    OutputStream outputStream = null;
    SerializerException cachedException = null;
    try {/*  w  w  w.j  a  v  a 2  s .  c  o m*/
        final ContextURL contextURL = checkContextURL(options == null ? null : options.getContextURL());
        CircleStreamBuffer buffer = new CircleStreamBuffer();
        outputStream = buffer.getOutputStream();
        JsonGenerator json = new JsonFactory().createGenerator(outputStream);
        json.writeStartObject();
        writeContextURL(contextURL, json);
        writeMetadataETag(metadata, json);
        if (isODataMetadataFull) {
            json.writeStringField(constants.getType(),
                    "#Collection(" + type.getFullQualifiedName().getName() + ")");
        }
        writeOperations(property.getOperations(), json);
        json.writeFieldName(Constants.VALUE);
        writePrimitiveCollection(type, property, options == null ? null : options.isNullable(),
                options == null ? null : options.getMaxLength(),
                options == null ? null : options.getPrecision(), options == null ? null : options.getScale(),
                options == null ? null : options.isUnicode(), json);
        json.writeEndObject();

        json.close();
        outputStream.close();
        return SerializerResultImpl.with().content(buffer.getInputStream()).build();
    } catch (final IOException e) {
        cachedException = new SerializerException(IO_EXCEPTION_TEXT, e,
                SerializerException.MessageKeys.IO_EXCEPTION);
        throw cachedException;
    } finally {
        closeCircleStreamBufferOutput(outputStream, cachedException);
    }
}

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.  j av  a 2s  .c  o 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();
    }
}