List of usage examples for com.fasterxml.jackson.core JsonGenerator writeFieldName
public abstract void writeFieldName(SerializableString name) throws IOException, JsonGenerationException;
From source file:com.cedarsoft.couchdb.io.RowSerializer.java
public <K, V, D> void serialize(@Nonnull Row<K, V, D> row, @Nonnull JacksonSerializer<? super K> keySerializer, @Nonnull JacksonSerializer<? super V> valueSerializer, @Nullable JacksonSerializer<? super D> documentSerializer, @Nonnull JsonGenerator generator) throws IOException { generator.writeStartObject();/*from w w w .java 2 s.co m*/ @Nullable DocId id = row.getId(); if (id != null) { generator.writeStringField(PROPERTY_ID, id.asString()); } //The key generator.writeFieldName(PROPERTY_KEY); keySerializer.serialize(row.getKey(), generator); //The Value generator.writeFieldName(PROPERTY_VALUE); V value = row.getValue(); if (value == null) { generator.writeNull(); } else { valueSerializer.serialize(value, generator); } //The doc CouchDoc<? extends D> doc = row.getDoc(); if (doc != null) { if (documentSerializer == null) { throw new NullPointerException("documentSerializer must not be null when serializing a doc"); } generator.writeFieldName("doc"); couchDocSerializer.serialize(doc, documentSerializer, generator); } generator.writeEndObject(); }
From source file:com.bluelinelabs.logansquare.typeconverters.IntentConverter.java
@Override public void serialize(Intent intent, String fieldName, boolean writeFieldNameForObject, JsonGenerator jsonGenerator) throws IOException { android.util.Log.d("json2notification", "IntentConverter:serialize"); if (intent == null) { android.util.Log.d("json2notification", "intent: null"); return;//from w w w .j a v a 2s . c om } SimpleIntent simpleIntent = new SimpleIntent(); simpleIntent.uri = intent.getData(); simpleIntent.action = intent.getAction(); if (writeFieldNameForObject) jsonGenerator.writeFieldName(fieldName); SimpleIntent$$JsonObjectMapper._serialize((SimpleIntent) simpleIntent, jsonGenerator, true); }
From source file:net.solarnetwork.web.support.JSONView.java
private void generateJavaBeanObject(JsonGenerator json, String key, Object bean, PropertyEditorRegistrar registrar) throws JsonGenerationException, IOException { if (key != null) { json.writeFieldName(key); }/* w ww . ja v a2 s . co m*/ if (bean == null) { json.writeNull(); return; } BeanWrapper wrapper = getPropertyAccessor(bean, registrar); PropertyDescriptor[] props = wrapper.getPropertyDescriptors(); json.writeStartObject(); for (PropertyDescriptor prop : props) { String name = prop.getName(); if (this.getJavaBeanIgnoreProperties() != null && this.getJavaBeanIgnoreProperties().contains(name)) { continue; } if (wrapper.isReadableProperty(name)) { Object propVal = wrapper.getPropertyValue(name); if (propVal != null) { // test for SerializeIgnore Method getter = prop.getReadMethod(); if (getter != null && getter.isAnnotationPresent(SerializeIgnore.class)) { continue; } if (getPropertySerializerRegistrar() != null) { propVal = getPropertySerializerRegistrar().serializeProperty(name, propVal.getClass(), bean, propVal); } else { // Spring does not apply PropertyEditors on read methods, so manually handle PropertyEditor editor = wrapper.findCustomEditor(null, name); if (editor != null) { editor.setValue(propVal); propVal = editor.getAsText(); } } if (propVal instanceof Enum<?> || getJavaBeanTreatAsStringValues() != null && getJavaBeanTreatAsStringValues().contains(propVal.getClass())) { propVal = propVal.toString(); } writeJsonValue(json, name, propVal, registrar); } } } json.writeEndObject(); }
From source file:org.neo4j.ontology.server.unmanaged.AnnotationResource.java
@GET @Produces(MediaType.APPLICATION_JSON)/* w w w . j a v a 2 s . c o m*/ @Path("/{userName}") public Response getAnnotationSets(final @PathParam("userName") String userName, final @DefaultValue("0") @QueryParam("objectification") int objectification) { StreamingOutput stream = new StreamingOutput() { @Override public void write(OutputStream os) throws IOException, WebApplicationException { Map<Long, List<Long>> associatedDataSets = new HashMap<>(); Label annotationLabel = DynamicLabel.label("AnnotationSets" + capitalize(userName)); JsonGenerator jg = objectMapper.getFactory().createGenerator(os, JsonEncoding.UTF8); jg.writeStartObject(); jg.writeFieldName("nodes"); if (objectification > 0) { jg.writeStartObject(); } else { jg.writeStartArray(); } try (Transaction tx = graphDb.beginTx(); ResourceIterator<Node> users = graphDb.findNodes(USER, "name", userName)) { if (users.hasNext()) { getDirectAnnotationTerms(getAccessibleDataSets(users.next()), associatedDataSets); } tx.success(); } try (Transaction tx = graphDb.beginTx(); ResourceIterator<Node> terms = graphDb.findNodes(annotationLabel)) { while (terms.hasNext()) { Node term = terms.next(); if (objectification > 0) { jg.writeFieldName(term.getProperty("uri").toString()); } if (objectification > 1) { if (associatedDataSets.containsKey(term.getId())) { writeJsonNodeObjectifiedObject(jg, term, annotationLabel, associatedDataSets.get(term.getId())); } else { writeJsonNodeObjectifiedObject(jg, term, annotationLabel); } } else { if (associatedDataSets.containsKey(term.getId())) { writeJsonNodeObject(jg, term, annotationLabel, associatedDataSets.get(term.getId())); } else { writeJsonNodeObject(jg, term, annotationLabel); } } } tx.success(); } if (objectification > 0) { jg.writeEndObject(); } else { jg.writeEndArray(); } jg.writeEndObject(); jg.flush(); jg.close(); } }; return Response.ok().entity(stream).type(MediaType.APPLICATION_JSON).build(); }
From source file:org.springframework.data.couchbase.core.convert.translation.JacksonTranslationService.java
/** * Recursively iterates through the sources and adds it to the JSON generator. * * @param source the source document//from w w w. j a va 2 s . c o m * @param generator the JSON generator. * * @throws IOException */ private void encodeRecursive(final CouchbaseStorable source, final JsonGenerator generator) throws IOException { generator.writeStartObject(); for (Map.Entry<String, Object> entry : ((CouchbaseDocument) source).export().entrySet()) { String key = entry.getKey(); Object value = entry.getValue(); generator.writeFieldName(key); if (value instanceof CouchbaseDocument) { encodeRecursive((CouchbaseDocument) value, generator); continue; } final Class<?> clazz = value.getClass(); if (simpleTypeHolder.isSimpleType(clazz) && !isEnumOrClass(clazz)) { generator.writeObject(value); } else { objectMapper.writeValue(generator, value); } } generator.writeEndObject(); }
From source file:TDS.Shared.Messages.MessageJson.java
public String create() throws ReturnStatusException { StringWriter sw = new StringWriter(); JsonFactory jsonFactory = new JsonFactory(); JsonGenerator jsonWriter; try {// w ww. jav a2 s . c o m if (_messageSystem == null) return "{}"; jsonWriter = jsonFactory.createGenerator(sw); jsonWriter.writeStartObject(); jsonWriter.writeStringField("c_l", _language); // "c_l": _language jsonWriter.writeFieldName("c_a"); // "c_a" : jsonWriter.writeStartArray(); // [ // get all the message contexts across all the context types List<MessageContext> messageContexts = new ArrayList<MessageContext>(); for (MessageContextType contextType : _messageSystem.getContextTypes()) { messageContexts.addAll(contextType.getContexts()); } for (MessageContext messageContext : messageContexts) { writeContextElement(messageContext, jsonWriter); } jsonWriter.writeEndArray(); // ] jsonWriter.writeEndObject(); // } jsonWriter.close(); sw.close(); } catch (IOException e) { ReturnStatus rs = new ReturnStatus("failed", "Serialization failed: " + e.getMessage()); throw new ReturnStatusException(rs); } return sw.getBuffer().toString(); }
From source file:com.cedarsoft.serialization.jackson.AbstractJacksonSerializer.java
public <T> void serialize(@Nullable T object, @Nonnull Class<T> type, @Nonnull String propertyName, @Nonnull JsonGenerator serializeTo, @Nonnull Version formatVersion) throws JsonProcessingException, IOException { serializeTo.writeFieldName(propertyName); //Fast exit if the value is null if (object == null) { serializeTo.writeNull();// w w w . j a v a 2 s .co m return; } JacksonSerializer<? super T> serializer = getSerializer(type); Version delegateVersion = delegatesMappings.getVersionMappings().resolveVersion(type, formatVersion); if (serializer.isObjectType()) { serializeTo.writeStartObject(); } serializer.serialize(serializeTo, object, delegateVersion); if (serializer.isObjectType()) { serializeTo.writeEndObject(); } }
From source file:ratpack.codahale.metrics.internal.WebSocketReporter.java
@SuppressWarnings("rawtypes") private void writeGauges(JsonGenerator json, SortedMap<String, Gauge> gauges) throws IOException { json.writeArrayFieldStart("gauges"); for (Map.Entry<String, Gauge> entry : gauges.entrySet()) { Gauge gauge = entry.getValue();/*w w w. j a v a 2s.c o m*/ json.writeStartObject(); json.writeStringField("name", entry.getKey()); try { json.writeFieldName("value"); json.writeObject(gauge.getValue()); } catch (Exception e) { LOGGER.log(Level.FINE, "Exception encountered while reporting [" + entry.getKey() + "]: " + e.getLocalizedMessage()); json.writeNull(); } json.writeEndObject(); } json.writeEndArray(); }
From source file:de.fraunhofer.iosb.ilt.sta.serialize.EntitySetResultSerializer.java
@Override public void serialize(EntitySetResult value, JsonGenerator gen, SerializerProvider serializers) throws IOException, JsonProcessingException { gen.writeStartObject();//from w ww. j a v a2s .co m long count = value.getValues().getCount(); if (count >= 0) { gen.writeNumberField("@iot.count", count); } String nextLink = value.getValues().getNextLink(); if (nextLink != null) { gen.writeStringField("@iot.nextLink", nextLink); } // TODO begin/end array, iterate over content gen.writeFieldName("value"); gen.writeObject(value.getValues()); gen.writeEndObject(); }
From source file:models.GroupSerializer.java
@Override public void serialize(Group group, JsonGenerator jgen, SerializerProvider provider) throws IOException { jgen.writeStartObject();/*from w w w . j ava2 s . c o m*/ jgen.writeStringField("id", group.getId()); jgen.writeStringField("comment", parseReferencesInComment(group)); jgen.writeStringField("label", group.getLabel()); jgen.writeFieldName("unit"); jgen.writeObject(group.getUnit()); jgen.writeFieldName("type"); jgen.writeObject(group.getType()); jgen.writeFieldName("commonKeywords"); jgen.writeObject(group.getCommonKeywords()); jgen.writeFieldName("dimensions"); jgen.writeStartArray(); for (Dimension dim : group.getDimSet().dimensions) { jgen.writeStartObject(); jgen.writeFieldName("id"); jgen.writeObject(dim.getId()); jgen.writeFieldName("keywords"); jgen.writeObject(dim.keywords); jgen.writeFieldName("keywordsPosition"); jgen.writeObject(dim.keywordsPosition); jgen.writeFieldName("orientation"); jgen.writeObject(group.getDimSet().getDimensionOrientation(dim)); jgen.writeEndObject(); } jgen.writeEndArray(); jgen.writeFieldName("overlap"); jgen.writeStartArray(); for (Group otherGroup : group.getOverlappingGroups()) { jgen.writeStartObject(); jgen.writeStringField("id", otherGroup.getId()); jgen.writeStringField("label", otherGroup.getLabel()); jgen.writeEndObject(); } jgen.writeEndArray(); jgen.writeFieldName("references"); jgen.writeStartArray(); for (Reference ref : group.getReferences()) { jgen.writeObject(ref); } jgen.writeEndArray(); jgen.writeFieldName("sourceRelations"); jgen.writeStartArray(); for (SourceRelation sourceRelation : group.getSourceRelations()) { jgen.writeObject(sourceRelation); } jgen.writeEndArray(); jgen.writeFieldName("elementsURI"); jgen.writeStartObject(); for (SingleElement element : group.getElements()) { jgen.writeStringField(StringUtils.join(element.getKeywords(), "+") + "+" + element.getUnit().getId(), element.getId()); } jgen.writeEndObject(); if (group.getType() == Type.COEFFICIENT) { jgen.writeFieldName("elementsValue"); jgen.writeStartObject(); for (SingleElement element : group.getElements()) { Coefficient coeff = (Coefficient) element; jgen.writeFieldName(StringUtils.join(element.getKeywords(), "+") + "+" + element.getUnit().getId()); jgen.writeStartObject(); jgen.writeNumberField("value", coeff.getValue().value); jgen.writeNumberField("uncertainty", coeff.getValue().uncertainty); jgen.writeEndObject(); } jgen.writeEndObject(); } else { jgen.writeFieldName("elementsImpactsAndFlows"); jgen.writeStartObject(); for (SingleElement element : group.getElements()) { Process process = (Process) element; jgen.writeFieldName(StringUtils.join(element.getKeywords(), "+") + "+" + element.getUnit().getId()); jgen.writeStartObject(); for (ElementaryFlow flow : process.getCalculatedFlows().values()) { jgen.writeFieldName(flow.getType().getId()); jgen.writeStartObject(); jgen.writeNumberField("value", flow.getValue().value); jgen.writeNumberField("uncertainty", flow.getValue().uncertainty); jgen.writeEndObject(); } for (Impact impact : process.getImpacts().values()) { jgen.writeFieldName(impact.getType().getId()); jgen.writeStartObject(); jgen.writeNumberField("value", impact.getValue().value); jgen.writeNumberField("uncertainty", impact.getValue().uncertainty); jgen.writeEndObject(); } jgen.writeEndObject(); } jgen.writeEndObject(); } jgen.writeEndObject(); }