/** * Write a java.util.Map out as a JSON Object. Keys are serialized using the toString method of * the object and values are serialized using primitives conversions. If a value in the map is a * List or Map object, it will be serialized as JSON Array or JSON Object respectively. * * @param map a java.util.Map object to be serialized as a JSON Object */ public JSONBuilder writeMap(final Map map) { this.object(); for (final Object k : map.keySet()) { this.key(k.toString()); this.value(map.get(k)); } return this.endObject(); }
/** * Utility method that encode an attributes map, only the attribute name local part will be * used. Attributes with a NULL value will not be encoded. This method assumes that it is * already in an array context. */ private void encodeAttributes(Map<NameImpl, String> attributes) { attributes.forEach( (name, value) -> { if (value != null) { // encode attribute, we don't take namespace into account jsonWriter.object(); jsonWriter.key(name.getLocalPart()).value(value); jsonWriter.endObject(); } }); }
private void writeCrsLegacy(final GeoJSONBuilder jsonWriter, CoordinateReferenceSystem crs) { // Coordinate Reference System, currently only if the namespace is // EPSG if (crs != null) { Set<ReferenceIdentifier> ids = crs.getIdentifiers(); // WKT defined crs might not have identifiers at all if (ids != null && ids.size() > 0) { NamedIdentifier namedIdent = (NamedIdentifier) ids.iterator().next(); String csStr = namedIdent.getCodeSpace().toUpperCase(); if (csStr.equals("EPSG")) { jsonWriter.key("crs"); jsonWriter.object(); jsonWriter.key("type").value(csStr); jsonWriter.key("properties"); jsonWriter.object(); jsonWriter.key("code"); jsonWriter.value(namedIdent.getCode()); jsonWriter.endObject(); // end properties jsonWriter.endObject(); // end crs } } } }
@Override public RawMap build(WMSMapContent mapContent) throws IOException { jsonWriter.endArray(); // end features jsonWriter.endObject();// end root object writer.flush(); writer.close(); out.close(); long length; RawMap map; if (out.isInMemory()) { byte[] data = out.getData(); length = data.length; map = new RawMap(mapContent, data, MIME_TYPE); } else { File f = out.getFile(); length = f.length(); map = new DeferredFileOutputStreamWebMap(mapContent, out, MIME_TYPE); } map.setResponseHeader("Content-Length", String.valueOf(length)); return map; } }
@Override public RawMap build(WMSMapContent mapContent) throws IOException { jsonWriter.endArray(); // end features jsonWriter.endObject(); // end root object writer.flush(); writer.close(); out.close(); long length; RawMap map; if (out.isInMemory()) { byte[] data = out.getData(); length = data.length; map = new RawMap(mapContent, data, MIME_TYPE); } else { File f = out.getFile(); length = f.length(); map = new DeferredFileOutputStreamWebMap(mapContent, out, MIME_TYPE); } map.setResponseHeader("Content-Length", String.valueOf(length)); return map; } }
/** Encode a complex attribute as a JSON object. */ private void encodeComplexAttribute( ComplexAttribute attribute, Map<NameImpl, String> attributes) { // get the attribute name and start a JSON object String name = attribute.getName().getLocalPart(); jsonWriter.key(name); if (attributes != null && !attributes.isEmpty()) { // we have some attributes to encode jsonWriter.array(); } // let's see if we have actually some properties to encode if (attribute.getProperties() != null && !attribute.getProperties().isEmpty()) { jsonWriter.object(); // encode the object properties, since this is not a top feature or a // chained feature we don't need to explicitly handle the geometry attribute encodeProperties(null, attribute.getType(), attribute.getProperties()); // end the attribute JSON object jsonWriter.endObject(); } if (attributes != null && !attributes.isEmpty()) { // encode the attributes list encodeAttributes(attributes); jsonWriter.endArray(); } }
@Override public void addFeature(String layerName, String featureId, String geometryName, Geometry aGeom, Map<String, Object> properties) { if (precisionReducerFilter != null) { aGeom.apply(precisionReducerFilter); } jsonWriter.object(); jsonWriter.key("type").value("Feature"); jsonWriter.key("id").value(featureId); jsonWriter.key("geometry"); // Write the geometry, whether it is a null or not jsonWriter.writeGeom(aGeom); jsonWriter.key("geometry_name").value(geometryName); jsonWriter.key("properties"); jsonWriter.object(); for (Map.Entry<String, Object> e : properties.entrySet()) { String attributeName = e.getKey(); Object value = e.getValue(); jsonWriter.key(attributeName); if (value == null) { jsonWriter.value(null); } else { jsonWriter.value(value); } } jsonWriter.endObject(); // end the properties jsonWriter.endObject(); // end the feature }
/** Encodes a list of features (chained features) as a JSON array. */ private void encodeChainedFeatures(String attributeName, List<Feature> chainedFeatures) { // start the JSON object jsonWriter.key(attributeName); jsonWriter.array(); for (Feature feature : chainedFeatures) { // encode each chained feature jsonWriter.object(); encodeProperties(null, feature.getType(), feature.getProperties()); jsonWriter.endObject(); } // end the JSON chained features array jsonWriter.endArray(); }
/** Encode a feature in GeoJSON. */ protected void encodeFeature(Feature feature) { // start the feature JSON object jsonWriter.object(); jsonWriter.key("type").value("Feature"); // encode the feature identifier if available Identifier identifier = feature.getIdentifier(); if (identifier != null) { jsonWriter.key("id").value(identifier.getID()); } // geometry attribute has some special handling Property geometryAttribute = encodeGeometry(feature); // start the JSON object that will contain all the others properties jsonWriter.key("properties"); jsonWriter.object(); // encode object properties, we pass the geometry attribute to avoid duplicate encodings encodeProperties(geometryAttribute, feature.getType(), feature.getProperties()); // close the feature JSON object jsonWriter.endObject(); // close the properties JSON object jsonWriter.endObject(); }
protected void writeLink( GeoJSONBuilder jw, String title, String mimeType, String rel, String href) { if (href != null) { jw.object(); jw.key("title").value(title); jw.key("type").value(mimeType); jw.key("rel").value(rel); jw.key("href").value(href); jw.endObject(); } }
writeExtraCollectionProperties(featureCollection, operation, jsonWriter); jsonWriter.endObject(); // end featurecollection
private void writeCrs(final GeoJSONBuilder jsonWriter, CoordinateReferenceSystem crs) throws FactoryException { if (crs != null) { String identifier = null; Integer code = CRS.lookupEpsgCode(crs, true); if (code != null) { if (code != null) { identifier = SrsSyntax.OGC_URN.getPrefix() + code; } } else { identifier = CRS.lookupIdentifier(crs, true); } jsonWriter.key("crs"); jsonWriter.object(); jsonWriter.key("type").value("name"); jsonWriter.key("properties"); jsonWriter.object(); jsonWriter.key("name"); jsonWriter.value(identifier); jsonWriter.endObject(); // end properties jsonWriter.endObject(); // end crs } else { jsonWriter.key("crs"); jsonWriter.value(null); } }
private static void describeProperty( String name, AttributeDescriptor ad, GeoJSONBuilder jw, boolean isGeometry) { jw.object(); jw.key("name").value(name); jw.key("maxOccurs").value(ad.getMaxOccurs()); jw.key("minOccurs").value(ad.getMinOccurs()); jw.key("nillable").value((ad.getMinOccurs() > 0) ? false : true); Class<?> binding = ad.getType().getBinding(); if (isGeometry) { jw.key("type").value("gml:" + mapToJsonType(binding)); } else { jw.key("type").value("xsd:" + mapToJsonType(binding)); } jw.key("localType").value(mapToJsonType(binding)); jw.endObject(); // end of attribute description }
return this.endObject();