Search in sources :

Example 71 with JsonParser

use of com.fasterxml.jackson.core.JsonParser in project druid by druid-io.

the class AggregationTestHelper method readQueryResultArrayFromString.

private List readQueryResultArrayFromString(String str) throws Exception {
    List result = new ArrayList();
    JsonParser jp = mapper.getFactory().createParser(str);
    if (jp.nextToken() != JsonToken.START_ARRAY) {
        throw new IAE("not an array [%s]", str);
    }
    ObjectCodec objectCodec = jp.getCodec();
    while (jp.nextToken() != JsonToken.END_ARRAY) {
        result.add(objectCodec.readValue(jp, toolChest.getResultTypeReference()));
    }
    return result;
}
Also used : ArrayList(java.util.ArrayList) List(java.util.List) ArrayList(java.util.ArrayList) ObjectCodec(com.fasterxml.jackson.core.ObjectCodec) IAE(io.druid.java.util.common.IAE) JsonParser(com.fasterxml.jackson.core.JsonParser)

Example 72 with JsonParser

use of com.fasterxml.jackson.core.JsonParser in project Gaffer by gchq.

the class RoaringBitmapJsonSerialisationTest method testCanSerialiseAndDeserialise.

@Test
public void testCanSerialiseAndDeserialise() throws IOException {
    final ByteArrayBuilder byteArrayBuilder = new ByteArrayBuilder();
    JsonGenerator jsonGenerator = JSON_FACTORY.createGenerator(byteArrayBuilder);
    RoaringBitmap testBitmap = new RoaringBitmap();
    testBitmap.add(2);
    testBitmap.add(3000);
    testBitmap.add(300000);
    for (int i = 400000; i < 500000; i += 2) {
        testBitmap.add(i);
    }
    SERIALISER.serialize(testBitmap, jsonGenerator, null);
    jsonGenerator.flush();
    byte[] serialisedBitmap = byteArrayBuilder.toByteArray();
    JsonParser parser = JSON_FACTORY.createParser(serialisedBitmap);
    parser.setCodec(new ObjectMapper());
    Object o = DESERIALISER.deserialize(parser, null);
    assertEquals(RoaringBitmap.class, o.getClass());
    assertEquals(testBitmap, o);
}
Also used : JsonGenerator(com.fasterxml.jackson.core.JsonGenerator) ByteArrayBuilder(com.fasterxml.jackson.core.util.ByteArrayBuilder) RoaringBitmap(org.roaringbitmap.RoaringBitmap) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) JsonParser(com.fasterxml.jackson.core.JsonParser) Test(org.junit.Test)

Example 73 with JsonParser

use of com.fasterxml.jackson.core.JsonParser in project Gaffer by gchq.

the class RoaringBitmapJsonSerialisationTest method testCanDeserialiseVersionZeroPointThreePointFourBitmap.

@Test
public void testCanDeserialiseVersionZeroPointThreePointFourBitmap() throws IOException {
    //Bitmap of (2,3000,300000) serialised in 0.3.4 Roaring Bitmap base 64 encoded
    String serialisedBitmap = "{\"roaringBitmap\":{\"value\":\"OTAAAAIAAAAAAAEABAAAAAIAuAvgkw==\"}}";
    RoaringBitmap comparisonBitmap = new RoaringBitmap();
    comparisonBitmap.add(2);
    comparisonBitmap.add(3000);
    comparisonBitmap.add(300000);
    JsonParser parser = JSON_FACTORY.createParser(serialisedBitmap);
    parser.setCodec(new ObjectMapper());
    Object o = DESERIALISER.deserialize(parser, null);
    assertEquals(RoaringBitmap.class, o.getClass());
    assertEquals(comparisonBitmap, o);
    ByteArrayBuilder byteArrayBuilder = new ByteArrayBuilder();
    JsonGenerator jsonGenerator = JSON_FACTORY.createGenerator(byteArrayBuilder);
    SERIALISER.serialize((RoaringBitmap) o, jsonGenerator, null);
    jsonGenerator.flush();
    byte[] bytes = byteArrayBuilder.toByteArray();
    String reSerialisedBitmap = new String(bytes);
    byteArrayBuilder = new ByteArrayBuilder();
    jsonGenerator = JSON_FACTORY.createGenerator(byteArrayBuilder);
    SERIALISER.serialize(comparisonBitmap, jsonGenerator, null);
    jsonGenerator.flush();
    String serialisedComparisonBitmap = new String(byteArrayBuilder.toByteArray());
    assertNotEquals(reSerialisedBitmap, serialisedBitmap);
    assertEquals(reSerialisedBitmap, serialisedComparisonBitmap);
}
Also used : JsonGenerator(com.fasterxml.jackson.core.JsonGenerator) ByteArrayBuilder(com.fasterxml.jackson.core.util.ByteArrayBuilder) RoaringBitmap(org.roaringbitmap.RoaringBitmap) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) JsonParser(com.fasterxml.jackson.core.JsonParser) Test(org.junit.Test)

Example 74 with JsonParser

use of com.fasterxml.jackson.core.JsonParser in project Gaffer by gchq.

the class RoaringBitmapJsonSerialisationTest method testCanSerialiseAndDeserialiseWithRuns.

@Test
public void testCanSerialiseAndDeserialiseWithRuns() throws IOException {
    final ByteArrayBuilder byteArrayBuilder = new ByteArrayBuilder();
    JsonGenerator jsonGenerator = JSON_FACTORY.createGenerator(byteArrayBuilder);
    RoaringBitmap testBitmap = new RoaringBitmap();
    testBitmap.add(2);
    testBitmap.add(3000);
    testBitmap.add(300000);
    for (int i = 400000; i < 500000; i += 2) {
        testBitmap.add(i);
    }
    testBitmap.runOptimize();
    SERIALISER.serialize(testBitmap, jsonGenerator, null);
    jsonGenerator.flush();
    byte[] serialisedBitmap = byteArrayBuilder.toByteArray();
    JsonParser parser = JSON_FACTORY.createParser(serialisedBitmap);
    parser.setCodec(new ObjectMapper());
    Object o = DESERIALISER.deserialize(parser, null);
    assertEquals(RoaringBitmap.class, o.getClass());
    assertEquals(testBitmap, o);
}
Also used : JsonGenerator(com.fasterxml.jackson.core.JsonGenerator) ByteArrayBuilder(com.fasterxml.jackson.core.util.ByteArrayBuilder) RoaringBitmap(org.roaringbitmap.RoaringBitmap) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) JsonParser(com.fasterxml.jackson.core.JsonParser) Test(org.junit.Test)

Example 75 with JsonParser

use of com.fasterxml.jackson.core.JsonParser 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

JsonParser (com.fasterxml.jackson.core.JsonParser)144 IOException (java.io.IOException)43 Test (org.junit.Test)35 JsonFactory (com.fasterxml.jackson.core.JsonFactory)26 StringWriter (java.io.StringWriter)17 JsonNode (com.fasterxml.jackson.databind.JsonNode)15 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)15 ExtensibleJSONWriter (com.instagram.common.json.annotation.processor.support.ExtensibleJSONWriter)15 JsonToken (com.fasterxml.jackson.core.JsonToken)14 JsonUtil.createJsonParser (com.facebook.presto.util.JsonUtil.createJsonParser)12 SqlNullable (com.facebook.presto.spi.function.SqlNullable)11 SqlType (com.facebook.presto.spi.function.SqlType)11 BaseTest (com.fasterxml.jackson.core.BaseTest)11 UTF8DataInputJsonParser (com.fasterxml.jackson.core.json.UTF8DataInputJsonParser)11 JsonGenerator (com.fasterxml.jackson.core.JsonGenerator)10 JsonParseException (com.fasterxml.jackson.core.JsonParseException)9 SimpleParseUUT (com.instagram.common.json.annotation.processor.uut.SimpleParseUUT)7 PrestoException (com.facebook.presto.spi.PrestoException)6 ScalarFunction (com.facebook.presto.spi.function.ScalarFunction)6 ScalarOperator (com.facebook.presto.spi.function.ScalarOperator)5