use of com.fasterxml.jackson.core.JsonFactory in project dhis2-core by dhis2.
the class OrganisationUnitController method getGeoJson.
@RequestMapping(value = "", method = RequestMethod.GET, produces = { "application/json+geo", "application/json+geojson" })
public void getGeoJson(@RequestParam(value = "level", required = false) List<Integer> rpLevels, @RequestParam(value = "parent", required = false) List<String> rpParents, @RequestParam(value = "properties", required = false, defaultValue = "true") boolean rpProperties, User currentUser, HttpServletResponse response) throws IOException {
rpLevels = rpLevels != null ? rpLevels : new ArrayList<>();
rpParents = rpParents != null ? rpParents : new ArrayList<>();
List<OrganisationUnit> parents = manager.getByUid(OrganisationUnit.class, rpParents);
if (rpLevels.isEmpty()) {
rpLevels.add(1);
}
if (parents.isEmpty()) {
parents.addAll(organisationUnitService.getRootOrganisationUnits());
}
List<OrganisationUnit> organisationUnits = organisationUnitService.getOrganisationUnitsAtLevels(rpLevels, parents);
response.setContentType("application/json");
JsonFactory jsonFactory = new JsonFactory();
JsonGenerator generator = jsonFactory.createGenerator(response.getOutputStream());
generator.writeStartObject();
generator.writeStringField("type", "FeatureCollection");
generator.writeArrayFieldStart("features");
for (OrganisationUnit organisationUnit : organisationUnits) {
writeFeature(generator, organisationUnit, rpProperties, currentUser);
}
generator.writeEndArray();
generator.writeEndObject();
generator.close();
}
use of com.fasterxml.jackson.core.JsonFactory in project jmxtrans by jmxtrans.
the class LibratoWriter2Test method metricsAreFormattedCorrectly.
@Test
public void metricsAreFormattedCorrectly() throws IOException {
LibratoWriter2 libratoWriter = new LibratoWriter2(new JsonFactory(), ImmutableList.<String>of());
StringWriter writer = new StringWriter();
libratoWriter.write(writer, dummyServer(), dummyQuery(), dummyResults());
String json = writer.toString();
assertThatJson(json).node("counters").isArray().ofLength(0);
assertThatJson(json).node("gauges").isArray().ofLength(1);
assertThatJson(json).node("gauges[0].name").isEqualTo("MemoryAlias.ObjectPendingFinalizationCount").node("gauges[0].source").isEqualTo("host_example_net").node("gauges[0].measure_time").isEqualTo(0).node("gauges[0].value").isEqualTo(10);
}
use of com.fasterxml.jackson.core.JsonFactory in project elasticsearch by elastic.
the class ElasticsearchHostsSnifferTests method buildSniffResponse.
private static SniffResponse buildSniffResponse(ElasticsearchHostsSniffer.Scheme scheme) throws IOException {
int numNodes = RandomNumbers.randomIntBetween(getRandom(), 1, 5);
List<HttpHost> hosts = new ArrayList<>(numNodes);
JsonFactory jsonFactory = new JsonFactory();
StringWriter writer = new StringWriter();
JsonGenerator generator = jsonFactory.createGenerator(writer);
generator.writeStartObject();
if (getRandom().nextBoolean()) {
generator.writeStringField("cluster_name", "elasticsearch");
}
if (getRandom().nextBoolean()) {
generator.writeObjectFieldStart("bogus_object");
generator.writeEndObject();
}
generator.writeObjectFieldStart("nodes");
for (int i = 0; i < numNodes; i++) {
String nodeId = RandomStrings.randomAsciiOfLengthBetween(getRandom(), 5, 10);
generator.writeObjectFieldStart(nodeId);
if (getRandom().nextBoolean()) {
generator.writeObjectFieldStart("bogus_object");
generator.writeEndObject();
}
if (getRandom().nextBoolean()) {
generator.writeArrayFieldStart("bogus_array");
generator.writeStartObject();
generator.writeEndObject();
generator.writeEndArray();
}
boolean isHttpEnabled = rarely() == false;
if (isHttpEnabled) {
String host = "host" + i;
int port = RandomNumbers.randomIntBetween(getRandom(), 9200, 9299);
HttpHost httpHost = new HttpHost(host, port, scheme.toString());
hosts.add(httpHost);
generator.writeObjectFieldStart("http");
if (getRandom().nextBoolean()) {
generator.writeArrayFieldStart("bound_address");
generator.writeString("[fe80::1]:" + port);
generator.writeString("[::1]:" + port);
generator.writeString("127.0.0.1:" + port);
generator.writeEndArray();
}
if (getRandom().nextBoolean()) {
generator.writeObjectFieldStart("bogus_object");
generator.writeEndObject();
}
generator.writeStringField("publish_address", httpHost.toHostString());
if (getRandom().nextBoolean()) {
generator.writeNumberField("max_content_length_in_bytes", 104857600);
}
generator.writeEndObject();
}
if (getRandom().nextBoolean()) {
String[] roles = { "master", "data", "ingest" };
int numRoles = RandomNumbers.randomIntBetween(getRandom(), 0, 3);
Set<String> nodeRoles = new HashSet<>(numRoles);
for (int j = 0; j < numRoles; j++) {
String role;
do {
role = RandomPicks.randomFrom(getRandom(), roles);
} while (nodeRoles.add(role) == false);
}
generator.writeArrayFieldStart("roles");
for (String nodeRole : nodeRoles) {
generator.writeString(nodeRole);
}
generator.writeEndArray();
}
int numAttributes = RandomNumbers.randomIntBetween(getRandom(), 0, 3);
Map<String, String> attributes = new HashMap<>(numAttributes);
for (int j = 0; j < numAttributes; j++) {
attributes.put("attr" + j, "value" + j);
}
if (numAttributes > 0) {
generator.writeObjectFieldStart("attributes");
}
for (Map.Entry<String, String> entry : attributes.entrySet()) {
generator.writeStringField(entry.getKey(), entry.getValue());
}
if (numAttributes > 0) {
generator.writeEndObject();
}
generator.writeEndObject();
}
generator.writeEndObject();
generator.writeEndObject();
generator.close();
return SniffResponse.buildResponse(writer.toString(), hosts);
}
use of com.fasterxml.jackson.core.JsonFactory in project elasticsearch by elastic.
the class JsonXContentTests method testBigInteger.
public void testBigInteger() throws Exception {
ByteArrayOutputStream os = new ByteArrayOutputStream();
JsonGenerator generator = new JsonFactory().createGenerator(os);
doTestBigInteger(generator, os);
}
use of com.fasterxml.jackson.core.JsonFactory in project elasticsearch by elastic.
the class JacksonLocationTests method testLocationExtraction.
public void testLocationExtraction() throws IOException {
// {
// "index" : "test",
// "source" : {
// value : "something"
// }
// }
BytesStreamOutput os = new BytesStreamOutput();
JsonGenerator gen = new JsonFactory().createGenerator(os);
gen.writeStartObject();
gen.writeStringField("index", "test");
gen.writeFieldName("source");
gen.writeStartObject();
gen.writeStringField("value", "something");
gen.writeEndObject();
gen.writeEndObject();
gen.close();
JsonParser parser = new JsonFactory().createParser(os.bytes().streamInput());
assertThat(parser.nextToken(), equalTo(JsonToken.START_OBJECT));
// "index"
assertThat(parser.nextToken(), equalTo(JsonToken.FIELD_NAME));
assertThat(parser.nextToken(), equalTo(JsonToken.VALUE_STRING));
// "source"
assertThat(parser.nextToken(), equalTo(JsonToken.FIELD_NAME));
// JsonLocation location1 = parser.getCurrentLocation();
// parser.skipChildren();
// JsonLocation location2 = parser.getCurrentLocation();
//
// byte[] sourceData = new byte[(int) (location2.getByteOffset() - location1.getByteOffset())];
// System.arraycopy(data, (int) location1.getByteOffset(), sourceData, 0, sourceData.length);
//
// JsonParser sourceParser = new JsonFactory().createJsonParser(new FastByteArrayInputStream(sourceData));
// assertThat(sourceParser.nextToken(), equalTo(JsonToken.START_OBJECT));
// assertThat(sourceParser.nextToken(), equalTo(JsonToken.FIELD_NAME)); // "value"
// assertThat(sourceParser.nextToken(), equalTo(JsonToken.VALUE_STRING));
// assertThat(sourceParser.nextToken(), equalTo(JsonToken.END_OBJECT));
}
Aggregations