List of usage examples for com.fasterxml.jackson.core JsonGenerator writeStartArray
public abstract void writeStartArray() throws IOException, JsonGenerationException;
From source file:org.teiid.olingo.TeiidODataJsonSerializer.java
public SerializerResult complexCollection(final ServiceMetadata metadata, final EdmComplexType type, final Property result, final ContextURL contextURL, final URI nextLink) throws SerializerException { CircleStreamBuffer buffer = new CircleStreamBuffer(); try {// w w w .j a v a 2 s. c om JsonGenerator json = new JsonFactory().createGenerator(buffer.getOutputStream()); json.writeStartObject(); if (contextURL != null && (isODataMetadataFull || !isODataMetadataNone)) { json.writeStringField(Constants.JSON_CONTEXT, ContextURLBuilder.create(contextURL).toASCIIString()); } json.writeFieldName(Constants.VALUE); json.writeStartArray(); for (Object value : result.asCollection()) { json.writeStartObject(); writeComplexValue(metadata, type, ((ComplexValue) value).getValue(), null, json, null, (ComplexValue) value, null, result.getName()); json.writeEndObject(); } json.writeEndArray(); if (nextLink != null) { json.writeStringField(Constants.JSON_NEXT_LINK, nextLink.toASCIIString()); } json.close(); } catch (final IOException e) { throw new SerializerException("An I/O exception occurred.", e, SerializerException.MessageKeys.IO_EXCEPTION); } return SerializerResultImpl.with().content(buffer.getInputStream()).build(); }
From source file:org.teiid.olingo.TeiidODataJsonSerializer.java
public SerializerResult complexCollection(final ServiceMetadata metadata, final List<List<ComplexReturnType>> result, final ContextURL contextURL, final URI nextLink) throws SerializerException { CircleStreamBuffer buffer = new CircleStreamBuffer(); try {//from ww w .jav a 2s . com JsonGenerator json = new JsonFactory().createGenerator(buffer.getOutputStream()); json.writeStartObject(); if (contextURL != null && (isODataMetadataFull || !isODataMetadataNone)) { json.writeStringField(Constants.JSON_CONTEXT, ContextURLBuilder.create(contextURL).toASCIIString()); } json.writeFieldName(Constants.VALUE); json.writeStartArray(); for (List<ComplexReturnType> ct : result) { json.writeStartObject(); for (final ComplexReturnType type : ct) { if (!type.isExpand()) { json.writeStringField(type.getName() + Constants.JSON_NAVIGATION_LINK, type.getEntity().getId().toASCIIString()); } else { json.writeFieldName(type.getName()); writeEntity(metadata, type.getEdmEntityType(), type.getEntity(), null, null, null, null, false, null, type.getName(), json); } } json.writeEndObject(); } json.writeEndArray(); if (nextLink != null) { json.writeStringField(Constants.JSON_NEXT_LINK, nextLink.toASCIIString()); } json.close(); } catch (final IOException | DecoderException e) { throw new SerializerException("An I/O exception occurred.", e, SerializerException.MessageKeys.IO_EXCEPTION); } return SerializerResultImpl.with().content(buffer.getInputStream()).build(); }
From source file:org.emfjson.jackson.streaming.StreamWriter.java
private void serializeContainment(JsonGenerator generator, String key, EReference containment, EObject owner, Object value) throws IOException { if (containment.isMany()) { final Collection<?> values = (Collection<?>) value; generator.writeFieldName(key);//from ww w .ja va 2 s . c o m generator.writeStartArray(); for (Object current : values) { EObject target = (EObject) current; if (isContainmentProxy(owner, target)) { referenceWriter.writeRef(generator, target); } else { generate(generator, target); } } generator.writeEndArray(); } else { generator.writeFieldName(key); if (isContainmentProxy(owner, (EObject) value)) { referenceWriter.writeRef(generator, (EObject) value); } else { generate(generator, (EObject) value); } } }
From source file:data.DefaultExchanger.java
public void exportData(String dbName, String catalogName, final JsonGenerator generator, JdbcTemplate jdbcTemplate) throws IOException { generator.writeFieldName(getTable()); generator.writeStartArray(); final int[] rowCount = { 0 }; jdbcTemplate.query(getSelectSql(), new RowCallbackHandler() { @Override//from w w w . j a v a 2 s. c om public void processRow(ResultSet rs) throws SQLException { try { generator.writeStartObject(); setNode(generator, rs); generator.writeEndObject(); rowCount[0]++; } catch (Exception e) { e.printStackTrace(); throw new RuntimeException(e); } } }); generator.writeEndArray(); play.Logger.info("exported {{}} {}", rowCount[0], getTable()); if (hasSequence()) { String sequenceName = sequenceName(); long sequenceValue = 0; if (dbName.equalsIgnoreCase("MySQL")) { String sql = String.format("SELECT `AUTO_INCREMENT` FROM INFORMATION_SCHEMA.TABLES " + "WHERE TABLE_SCHEMA = '%s' AND TABLE_NAME = '%s'", catalogName, getTable()); sequenceValue = jdbcTemplate.queryForObject(sql, Long.class); } else if (dbName.equalsIgnoreCase("H2")) { sequenceValue = jdbcTemplate.queryForObject("CALL NEXT VALUE FOR " + sequenceName, Long.class); } generator.writeFieldName(sequenceName); generator.writeNumber(sequenceValue); play.Logger.info("exported sequence {{}}", sequenceName()); } }
From source file:org.h2gis.drivers.geojson.GeoJsonWriteDriver.java
/** * Write coordinate array./*from ww w . ja v a 2 s. c o m*/ * * @param coordinates * @param gen * @throws IOException */ private void writeCoordinates(Coordinate[] coordinates, JsonGenerator gen) throws IOException { gen.writeStartArray(); for (Coordinate coord : coordinates) { writeCoordinate(coord, gen); } gen.writeEndArray(); }
From source file:org.h2gis.drivers.geojson.GeoJsonWriteDriver.java
/** * Write coordinate positions.//from w w w . ja v a2 s . c o m * * @param coordinate * @param gen * @throws IOException */ private void writeCoordinate(Coordinate coordinate, JsonGenerator gen) throws IOException { gen.writeStartArray(); gen.writeNumber(coordinate.x); gen.writeNumber(coordinate.y); if (!Double.isNaN(coordinate.z)) { gen.writeNumber(coordinate.z); } gen.writeEndArray(); }
From source file:org.eclipse.winery.repository.resources.AbstractComponentsResource.java
/** * Used by org.eclipse.winery.repository.repository.client and by the * artifactcreationdialog.tag. Especially the "name" field is used there at * the UI//from w w w . j a va2 s. c o m * * @return A list of all ids of all instances of this component type. If the * "name" attribute is required, that name is used as id <br /> * Format: * <code>[({"namespace": "<namespace>", "id": "<id>"},)* ]</code>. A * <code>name<code> field is added if the model allows an additional name attribute */ @GET @Produces(MediaType.APPLICATION_JSON) public String getListOfAllIds() { Class<? extends TOSCAComponentId> idClass = Utils.getComponentIdClassForComponentContainer(this.getClass()); boolean supportsNameAttribute = Util.instanceSupportsNameAttribute(idClass); SortedSet<? extends TOSCAComponentId> allTOSCAcomponentIds = Repository.INSTANCE .getAllTOSCAComponentIds(idClass); JsonFactory jsonFactory = new JsonFactory(); StringWriter sw = new StringWriter(); try { JsonGenerator jg = jsonFactory.createGenerator(sw); // We produce org.eclipse.winery.repository.client.WineryRepositoryClient.NamespaceAndId by hand here // Refactoring could move this class to common and fill it here jg.writeStartArray(); for (TOSCAComponentId id : allTOSCAcomponentIds) { jg.writeStartObject(); jg.writeStringField("namespace", id.getNamespace().getDecoded()); jg.writeStringField("id", id.getXmlId().getDecoded()); if (supportsNameAttribute) { AbstractComponentInstanceResource componentInstaceResource = AbstractComponentsResource .getComponentInstaceResource(id); String name = ((IHasName) componentInstaceResource).getName(); jg.writeStringField("name", name); } jg.writeEndObject(); } jg.writeEndArray(); jg.close(); } catch (Exception e) { AbstractComponentsResource.logger.error(e.getMessage(), e); return "[]"; } return sw.toString(); }
From source file:org.apache.olingo.commons.core.serialization.JsonSerializer.java
private void collection(final JsonGenerator jgen, final EdmTypeInfo typeInfo, final ValueType valueType, final List<?> value) throws IOException, EdmPrimitiveTypeException { jgen.writeStartArray(); for (Object item : value) { final EdmTypeInfo itemTypeInfo = typeInfo == null ? null : new EdmTypeInfo.Builder().setTypeExpression(typeInfo.getFullQualifiedName().toString()) .build();// w w w . ja v a 2 s.c o m switch (valueType) { case COLLECTION_PRIMITIVE: primitiveValue(jgen, itemTypeInfo, item); break; case COLLECTION_GEOSPATIAL: jgen.writeStartObject(); geoSerializer.serialize(jgen, (Geospatial) item); jgen.writeEndObject(); break; case COLLECTION_ENUM: jgen.writeString(item.toString()); break; case COLLECTION_COMPLEX: @SuppressWarnings("unchecked") final List<Property> complexItem = (List<Property>) item; complexValue(jgen, itemTypeInfo, complexItem, null); break; case COLLECTION_LINKED_COMPLEX: final LinkedComplexValue complexItem2 = (LinkedComplexValue) item; complexValue(jgen, itemTypeInfo, complexItem2.getValue(), complexItem2); break; default: } } jgen.writeEndArray(); }
From source file:models.CoefficientSerializer.java
@Override public void serialize(Coefficient coefficient, JsonGenerator jgen, SerializerProvider provider) throws IOException { jgen.writeStartObject();// w w w. j ava 2 s . co m jgen.writeStringField("id", coefficient.getId()); jgen.writeFieldName("unit"); jgen.writeObject(coefficient.getUnit()); jgen.writeFieldName("keywords"); jgen.writeObject(coefficient.getKeywords()); jgen.writeFieldName("value"); jgen.writeObject(coefficient.getValue()); jgen.writeFieldName("groups"); jgen.writeStartArray(); for (Group group : coefficient.getGroups()) { jgen.writeStartObject(); jgen.writeStringField("id", group.getId()); jgen.writeStringField("label", group.getLabel()); jgen.writeEndObject(); } jgen.writeEndArray(); jgen.writeFieldName("relations"); jgen.writeStartArray(); for (DerivedRelation relation : coefficient.getDerivedRelations()) { jgen.writeObject(relation); } jgen.writeEndArray(); jgen.writeEndObject(); }