Search in sources :

Example 46 with JsonParser

use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.core.JsonParser in project teiid by teiid.

the class TestODataIntegration method testExpand.

@Test
public void testExpand() throws Exception {
    HardCodedExecutionFactory hc = new HardCodedExecutionFactory();
    hc.addData("SELECT Customers.id, Customers.name FROM Customers", Arrays.asList(Arrays.asList(1, "customer1"), Arrays.asList(2, "customer2"), Arrays.asList(3, "customer3"), Arrays.asList(4, "customer4")));
    hc.addData("SELECT Orders.customerid, Orders.id, Orders.place FROM Orders", Arrays.asList(Arrays.asList(1, 1, "town"), Arrays.asList(1, 2, "state"), Arrays.asList(1, 3, "country"), Arrays.asList(1, 4, "abroad"), Arrays.asList(2, 5, "state"), Arrays.asList(2, 6, "country"), Arrays.asList(3, 7, "town"), Arrays.asList(3, 8, "town")));
    hc.addData("SELECT Orders.place, Orders.customerid, Orders.id FROM Orders", Arrays.asList(Arrays.asList("town", 1, 1), Arrays.asList("state", 1, 2), Arrays.asList("country", 1, 3), Arrays.asList("abroad", 1, 4), Arrays.asList("state", 2, 5), Arrays.asList("country", 2, 6), Arrays.asList("town", 3, 7), Arrays.asList("town", 3, 8)));
    teiid.addTranslator("x12", hc);
    try {
        ModelMetaData mmd = new ModelMetaData();
        mmd.setName("m");
        mmd.addSourceMetadata("ddl", "CREATE FOREIGN TABLE Customers (\n" + "  id integer PRIMARY KEY OPTIONS (NAMEINSOURCE 'id'),\n" + "  name varchar(10));\n" + "CREATE FOREIGN TABLE Orders (\n" + "  id integer PRIMARY KEY OPTIONS (NAMEINSOURCE 'id'),\n" + "  customerid integer,\n" + "  place varchar(10),\n" + "  FOREIGN KEY (customerid) REFERENCES Customers(id));");
        mmd.addSourceMapping("x12", "x12", null);
        teiid.deployVDB("northwind", mmd);
        localClient = getClient(teiid.getDriver(), "northwind", new Properties());
        ContentResponse response = null;
        response = http.newRequest(baseURL + "/northwind/m/Customers?$expand=Orders_FK0&$count=true").method("GET").send();
        assertEquals(200, response.getStatus());
        assertEquals("{\"@odata.context\":\"$metadata#Customers\"," + "\"@odata.count\":4,\"" + "value\":[" + "{\"id\":1,\"name\":\"customer1\"," + "\"Orders_FK0\":[" + "{\"id\":1,\"customerid\":1,\"place\":\"town\"}," + "{\"id\":2,\"customerid\":1,\"place\":\"state\"}," + "{\"id\":3,\"customerid\":1,\"place\":\"country\"}," + "{\"id\":4,\"customerid\":1,\"place\":\"abroad\"}" + "]}," + "{\"id\":2,\"name\":\"customer2\"," + "\"Orders_FK0\":[" + "{\"id\":5,\"customerid\":2,\"place\":\"state\"}," + "{\"id\":6,\"customerid\":2,\"place\":\"country\"}" + "]}," + "{\"id\":3,\"name\":\"customer3\"," + "\"Orders_FK0\":[" + "{\"id\":7,\"customerid\":3,\"place\":\"town\"}," + "{\"id\":8,\"customerid\":3,\"place\":\"town\"}" + "]}," + "{\"id\":4,\"name\":\"customer4\"," + "\"Orders_FK0\":[" + "]}]}", response.getContentAsString());
        response = http.newRequest(baseURL + "/northwind/m/Customers?$expand=Orders_FK0&$count=true&$skip=3").method("GET").send();
        assertEquals(200, response.getStatus());
        assertEquals("{\"@odata.context\":\"$metadata#Customers\"," + "\"@odata.count\":4,\"value\":[" + "{\"id\":4,\"name\":\"customer4\"," + "\"Orders_FK0\":[]" + "}]}", response.getContentAsString());
        response = http.newRequest(baseURL + "/northwind/m/Customers?$expand=Orders_FK0&$skip=2").method("GET").header("Prefer", "odata.maxpagesize=1").send();
        assertEquals(200, response.getStatus());
        assertTrue(response.getContentAsString().startsWith("{\"@odata.context\":\"$metadata#Customers\"," + "\"value\":[{\"id\":3,\"name\":\"customer3\"," + "\"Orders_FK0\":[" + "{\"id\":7,\"customerid\":3,\"place\":\"town\"}," + "{\"id\":8,\"customerid\":3,\"place\":\"town\"}" + "]}]," + "\"@odata.nextLink\":\"http://localhost:"));
        assertTrue(response.getContentAsString(), response.getContentAsString().endsWith(",1\"}"));
        JsonParser parser = new JsonFactory(new ObjectMapper().configure(DeserializationFeature.FAIL_ON_READING_DUP_TREE_KEY, true)).createParser(response.getContentAsString());
        JsonNode node = parser.getCodec().readTree(parser);
        response = http.newRequest(node.get("@odata.nextLink").asText()).method("GET").send();
        assertEquals(200, response.getStatus());
        assertEquals("{\"@odata.context\":\"$metadata#Customers\"," + "\"value\":[" + "{\"id\":4,\"name\":\"customer4\"," + "\"Orders_FK0\":[]" + "}]}", response.getContentAsString());
        // system options
        response = http.newRequest(baseURL + "/northwind/m/Customers?$expand=Orders_FK0($skip=2)&$count=true").method("GET").send();
        assertEquals(200, response.getStatus());
        assertEquals("{\"@odata.context\":\"$metadata#Customers\"," + "\"@odata.count\":4,\"" + "value\":[" + "{\"id\":1,\"name\":\"customer1\"," + "\"Orders_FK0\":[" + "{\"id\":3,\"customerid\":1,\"place\":\"country\"}," + "{\"id\":4,\"customerid\":1,\"place\":\"abroad\"}" + "]}," + "{\"id\":2,\"name\":\"customer2\"," + "\"Orders_FK0\":[" + "]}," + "{\"id\":3,\"name\":\"customer3\"," + "\"Orders_FK0\":[" + "]}," + "{\"id\":4,\"name\":\"customer4\"," + "\"Orders_FK0\":[" + "]}]}", response.getContentAsString());
        response = http.newRequest(baseURL + "/northwind/m/Customers?$expand=Orders_FK0($top=2;$count=true)&$count=true").method("GET").send();
        assertEquals(200, response.getStatus());
        assertEquals("{\"@odata.context\":\"$metadata#Customers\"," + "\"@odata.count\":4,\"" + "value\":[" + "{\"id\":1,\"name\":\"customer1\"," + "\"Orders_FK0@odata.count\":4," + "\"Orders_FK0\":[" + "{\"id\":1,\"customerid\":1,\"place\":\"town\"}," + "{\"id\":2,\"customerid\":1,\"place\":\"state\"}" + "]}," + "{\"id\":2,\"name\":\"customer2\"," + "\"Orders_FK0@odata.count\":2," + "\"Orders_FK0\":[" + "{\"id\":5,\"customerid\":2,\"place\":\"state\"}," + "{\"id\":6,\"customerid\":2,\"place\":\"country\"}" + "]}," + "{\"id\":3,\"name\":\"customer3\"," + "\"Orders_FK0@odata.count\":2," + "\"Orders_FK0\":[" + "{\"id\":7,\"customerid\":3,\"place\":\"town\"}," + "{\"id\":8,\"customerid\":3,\"place\":\"town\"}" + "]}," + "{\"id\":4,\"name\":\"customer4\"," + "\"Orders_FK0@odata.count\":0," + "\"Orders_FK0\":[" + "]}]}", response.getContentAsString());
        response = http.newRequest(baseURL + "/northwind/m/Customers?$expand=Orders_FK0($top=1;$select=place)&$count=true").method("GET").send();
        assertEquals(200, response.getStatus());
        assertEquals("{\"@odata.context\":\"$metadata#Customers(Orders_FK0(place))\"," + "\"@odata.count\":4,\"" + "value\":[" + "{\"id\":1,\"name\":\"customer1\"," + "\"Orders_FK0\":[" + "{\"@odata.id\":\"" + baseURL + "/northwind/m/Orders(1)\",\"place\":\"town\"}" + "]}," + "{\"id\":2,\"name\":\"customer2\"," + "\"Orders_FK0\":[" + "{\"@odata.id\":\"" + baseURL + "/northwind/m/Orders(5)\",\"place\":\"state\"}" + "]}," + "{\"id\":3,\"name\":\"customer3\"," + "\"Orders_FK0\":[" + "{\"@odata.id\":\"" + baseURL + "/northwind/m/Orders(7)\",\"place\":\"town\"}" + "]}," + "{\"id\":4,\"name\":\"customer4\"," + "\"Orders_FK0\":[" + "]}]}", response.getContentAsString());
        response = http.newRequest(baseURL + "/northwind/m/Customers?$expand=Orders_FK0($filter=" + Encoder.encode("place eq ") + "'town')").method("GET").send();
        assertEquals(200, response.getStatus());
        assertEquals("{\"@odata.context\":\"$metadata#Customers\"," + "\"value\":[{\"id\":1,\"name\":\"customer1\"," + "\"Orders_FK0\":[{\"id\":1,\"customerid\":1,\"place\":\"town\"}]}," + "{\"id\":2,\"name\":\"customer2\",\"Orders_FK0\":[]}," + "{\"id\":3,\"name\":\"customer3\"," + "\"Orders_FK0\":[{\"id\":7,\"customerid\":3,\"place\":\"town\"}," + "{\"id\":8,\"customerid\":3,\"place\":\"town\"}]}," + "{\"id\":4,\"name\":\"customer4\",\"Orders_FK0\":[]}]}", response.getContentAsString());
        response = http.newRequest(baseURL + "/northwind/m/Customers?$expand=Orders_FK0($top=0;$count=true)&$count=true").method("GET").send();
        assertEquals(200, response.getStatus());
        assertEquals("{\"@odata.context\":\"$metadata#Customers\"," + "\"@odata.count\":4,\"" + "value\":[" + "{\"id\":1,\"name\":\"customer1\"," + "\"Orders_FK0@odata.count\":4," + "\"Orders_FK0\":[]}," + "{\"id\":2,\"name\":\"customer2\"," + "\"Orders_FK0@odata.count\":2," + "\"Orders_FK0\":[]}," + "{\"id\":3,\"name\":\"customer3\"," + "\"Orders_FK0@odata.count\":2," + "\"Orders_FK0\":[]}," + "{\"id\":4,\"name\":\"customer4\"," + "\"Orders_FK0@odata.count\":0," + "\"Orders_FK0\":[" + "]}]}", response.getContentAsString());
    } finally {
        localClient = null;
        teiid.undeployVDB("northwind");
    }
}
Also used : ContentResponse(org.eclipse.jetty.client.api.ContentResponse) JsonFactory(com.fasterxml.jackson.core.JsonFactory) HardCodedExecutionFactory(org.teiid.runtime.HardCodedExecutionFactory) JsonNode(com.fasterxml.jackson.databind.JsonNode) Properties(java.util.Properties) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) ModelMetaData(org.teiid.adminapi.impl.ModelMetaData) JsonParser(com.fasterxml.jackson.core.JsonParser) Test(org.junit.Test)

Example 47 with JsonParser

use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.core.JsonParser in project divolte-collector by divolte.

the class MincodeFactoryTest method testReadingFromPartOfCharArray.

@Test
public void testReadingFromPartOfCharArray() throws Exception {
    // Create an array with our characters to decode in the middle somewhere.
    final char[] needle = MINCODE.toCharArray();
    final char[] haystack = new char[needle.length + 780];
    System.arraycopy(needle, 0, haystack, 45, needle.length);
    final JsonParser parser = mincodeMapper.getFactory().createParser(haystack, 45, needle.length);
    assertEquals(EXPECTED_STRING, mincodeMapper.readValue(parser, String.class));
}
Also used : JsonParser(com.fasterxml.jackson.core.JsonParser) Test(org.junit.Test)

Example 48 with JsonParser

use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.core.JsonParser in project divolte-collector by divolte.

the class MincodeParserSamplesTest method testSampleEquivalence.

@Test
public void testSampleEquivalence() throws IOException {
    // Test that samples, when parsed, generate the same sequence of parser states.
    final MincodeParser mincodeParser = factory.createParser(sampleMincode);
    final JsonParser jsonParser = sampleJson.traverse();
    // Initial state: parsers should have no current state.
    assertFalse(mincodeParser.hasCurrentToken());
    assertFalse(jsonParser.hasCurrentToken());
    for (; ; ) {
        final JsonToken jsonToken = jsonParser.nextToken();
        final JsonToken mincodeToken = mincodeParser.nextToken();
        assertEquals(jsonToken, mincodeToken);
        if (null == jsonToken) {
            // Both parsers are finished.
            break;
        }
        assertEquals(jsonToken, jsonParser.getCurrentToken());
        assertEquals(mincodeToken, mincodeParser.getCurrentToken());
        assertEquals(jsonParser.getCurrentName(), mincodeParser.getCurrentName());
        final Object currentJsonValue = getCurrentValue(jsonParser);
        final Object currentMincodeValue = getCurrentValue(mincodeParser);
        assertEquals(currentJsonValue, currentMincodeValue);
    }
}
Also used : JsonToken(com.fasterxml.jackson.core.JsonToken) JsonParser(com.fasterxml.jackson.core.JsonParser) Test(org.junit.Test)

Example 49 with JsonParser

use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.core.JsonParser in project divolte-collector by divolte.

the class MincodeParserTest method testLargeStringMincodeDecoding.

@Test
public void testLargeStringMincodeDecoding() throws IOException {
    // Build a fixture by repeating a record multiple times.
    // (The template will be the same length as records.)
    final String propertyNameTemplate = "property%04X";
    final String propertyValueTemplate = "still more %04X values";
    final String recordTemplate = 's' + propertyNameTemplate + '!' + propertyValueTemplate + '!';
    // Check that the record length and buffer size are relatively prime; this
    // ensures the record will be read at all possible offsets relative to the
    // internal buffer.
    assertEquals("Text size should be relatively prime to the buffer size.", BigInteger.ONE, BigInteger.valueOf(recordTemplate.length()).gcd(BigInteger.valueOf(JACKSON_BUFFER_SIZE)));
    final StringBuilder sb = new StringBuilder(2 + JACKSON_BUFFER_SIZE * recordTemplate.length());
    sb.append('(');
    for (int i = 0; i < JACKSON_BUFFER_SIZE; ++i) {
        sb.append(String.format(recordTemplate, i, i));
    }
    sb.append(')');
    // Time for the test itself.
    final JsonParser parser = createParser(sb.toString());
    // Before starting, everything should be clear.
    assertNull(parser.getCurrentToken());
    assertJsonParserTextUnavailable(parser);
    // First token is an object.
    assertEquals(JsonToken.START_OBJECT, parser.nextToken());
    assertJsonParserText(parser, "{");
    // Now we're going to loop over the strings.
    for (int i = 0; i < JACKSON_BUFFER_SIZE; ++i) {
        final String expectedPropertyName = String.format(propertyNameTemplate, i);
        final String expectedPropertyValue = String.format(propertyValueTemplate, i);
        // First we test the property name.
        assertEquals(JsonToken.FIELD_NAME, parser.nextToken());
        assertEquals(expectedPropertyName, parser.getCurrentName());
        // Field names are always available via text.
        assertJsonParserText(parser, expectedPropertyName);
        // Next test the property value.
        assertEquals(JsonToken.VALUE_STRING, parser.nextToken());
        assertEquals(expectedPropertyValue, parser.getValueAsString());
        assertJsonParserText(parser, expectedPropertyValue);
        assertTrue(parser.hasTextCharacters());
        // While looking at the value, the name is still available.
        assertEquals(expectedPropertyName, parser.getCurrentName());
    }
    // Next we expect the end of the array.
    assertEquals(JsonToken.END_OBJECT, parser.nextToken());
    assertJsonParserText(parser, "}");
    // Reading beyond the end yields no more tokens...
    assertNull(parser.nextToken());
    assertJsonParserTextUnavailable(parser);
    // ... even if we keep trying.
    assertNull(parser.nextToken());
    assertJsonParserTextUnavailable(parser);
}
Also used : JsonParser(com.fasterxml.jackson.core.JsonParser) Test(org.junit.Test)

Example 50 with JsonParser

use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.core.JsonParser in project jackson-core by FasterXML.

the class CoreJDKSerializabilityTest method testParseException.

public void testParseException() throws Exception {
    JsonFactory jf = new JsonFactory();
    JsonParser p = jf.createParser(ObjectReadContext.empty(), "  { garbage! }");
    JsonParseException exc = null;
    try {
        p.nextToken();
        p.nextToken();
        fail("Should not get here");
    } catch (JsonParseException e) {
        exc = e;
    }
    p.close();
    byte[] stuff = jdkSerialize(exc);
    JsonParseException result = jdkDeserialize(stuff);
    assertNotNull(result);
}
Also used : JsonFactory(com.fasterxml.jackson.core.json.JsonFactory) JsonParser(com.fasterxml.jackson.core.JsonParser)

Aggregations

JsonParser (com.fasterxml.jackson.core.JsonParser)587 KriptonRuntimeException (com.abubusoft.kripton.exception.KriptonRuntimeException)258 JacksonWrapperParser (com.abubusoft.kripton.persistence.JacksonWrapperParser)258 KriptonJsonContext (com.abubusoft.kripton.KriptonJsonContext)257 ArrayList (java.util.ArrayList)171 IOException (java.io.IOException)126 JsonFactory (com.fasterxml.jackson.core.JsonFactory)76 Test (org.junit.Test)57 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)51 JsonNode (com.fasterxml.jackson.databind.JsonNode)46 HashSet (java.util.HashSet)43 JsonToken (com.fasterxml.jackson.core.JsonToken)41 LinkedHashSet (java.util.LinkedHashSet)39 HashMap (java.util.HashMap)35 LinkedList (java.util.LinkedList)26 JsonParseException (com.fasterxml.jackson.core.JsonParseException)23 JsonUtil.createJsonParser (com.facebook.presto.util.JsonUtil.createJsonParser)21 List (java.util.List)21 DeserializationContext (com.fasterxml.jackson.databind.DeserializationContext)20 InputStream (java.io.InputStream)20