Search in sources :

Example 51 with JsonFactory

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();
}
Also used : OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) ArrayList(java.util.ArrayList) JsonFactory(com.fasterxml.jackson.core.JsonFactory) JsonGenerator(com.fasterxml.jackson.core.JsonGenerator) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 52 with JsonFactory

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);
}
Also used : StringWriter(java.io.StringWriter) JsonFactory(com.fasterxml.jackson.core.JsonFactory) Test(org.junit.Test)

Example 53 with JsonFactory

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);
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) JsonFactory(com.fasterxml.jackson.core.JsonFactory) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) StringWriter(java.io.StringWriter) HttpHost(org.apache.http.HttpHost) JsonGenerator(com.fasterxml.jackson.core.JsonGenerator) HashMap(java.util.HashMap) Map(java.util.Map) HashSet(java.util.HashSet)

Example 54 with JsonFactory

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);
}
Also used : JsonFactory(com.fasterxml.jackson.core.JsonFactory) JsonGenerator(com.fasterxml.jackson.core.JsonGenerator) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 55 with JsonFactory

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));
}
Also used : JsonFactory(com.fasterxml.jackson.core.JsonFactory) JsonGenerator(com.fasterxml.jackson.core.JsonGenerator) BytesStreamOutput(org.elasticsearch.common.io.stream.BytesStreamOutput) JsonParser(com.fasterxml.jackson.core.JsonParser)

Aggregations

JsonFactory (com.fasterxml.jackson.core.JsonFactory)115 JsonGenerator (com.fasterxml.jackson.core.JsonGenerator)45 Test (org.junit.Test)37 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)35 StringWriter (java.io.StringWriter)34 JsonParser (com.fasterxml.jackson.core.JsonParser)26 IOException (java.io.IOException)16 ExtensibleJSONWriter (com.instagram.common.json.annotation.processor.support.ExtensibleJSONWriter)15 Map (java.util.Map)14 HashMap (java.util.HashMap)11 ArrayList (java.util.ArrayList)9 SimpleParseUUT (com.instagram.common.json.annotation.processor.uut.SimpleParseUUT)8 List (java.util.List)8 Reader (java.io.Reader)6 ServletOutputStream (javax.servlet.ServletOutputStream)6 RemoteSession (org.apache.jackrabbit.oak.remote.RemoteSession)5 FileInputStream (java.io.FileInputStream)4 JSONWriter (org.json.JSONWriter)4 JsonNode (com.fasterxml.jackson.databind.JsonNode)3 BaseJsonHttpResponseHandler (com.loopj.android.http.BaseJsonHttpResponseHandler)3