Search in sources :

Example 41 with JsonFactory

use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.core.JsonFactory 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 42 with JsonFactory

use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.core.JsonFactory in project tutorials by eugenp.

the class JacksonExceptionsUnitTest method givenStringWithSingleQuotes_whenConfigureDeserializing_thenCorrect.

@Test
public void givenStringWithSingleQuotes_whenConfigureDeserializing_thenCorrect() throws JsonProcessingException, IOException {
    final String json = "{'id':1,'name':'John'}";
    final JsonFactory factory = new JsonFactory();
    factory.enable(JsonParser.Feature.ALLOW_SINGLE_QUOTES);
    final ObjectMapper mapper = new ObjectMapper(factory);
    final com.baeldung.jackson.dtos.User user = mapper.reader().forType(com.baeldung.jackson.dtos.User.class).readValue(json);
    assertEquals("John", user.name);
}
Also used : User(com.baeldung.jackson.exception.User) JsonFactory(com.fasterxml.jackson.core.JsonFactory) Matchers.containsString(org.hamcrest.Matchers.containsString) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.Test)

Example 43 with JsonFactory

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

the class MincodeFactoryTest method testDataDetector.

@Test
public void testDataDetector() throws Exception {
    final JsonFactory factory = mincodeMapper.getFactory();
    final DataFormatDetector detector = new DataFormatDetector(factory);
    assertEquals(factory.getFormatName(), detector.findFormat(MINCODE.getBytes(StandardCharsets.UTF_8)).getMatchedFormatName());
    // Zero-length mincode is invalid.
    assertFalse(detector.findFormat(new byte[0]).hasMatch());
    // 'z' is not a valid record type.
    assertFalse(detector.findFormat("zoop".getBytes(StandardCharsets.UTF_8)).hasMatch());
    // '.' is a valid record type, but not as the first record.
    assertFalse(detector.findFormat(".".getBytes(StandardCharsets.UTF_8)).hasMatch());
}
Also used : DataFormatDetector(com.fasterxml.jackson.core.format.DataFormatDetector) JsonFactory(com.fasterxml.jackson.core.JsonFactory) Test(org.junit.Test)

Example 44 with JsonFactory

use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.core.JsonFactory in project pac4j by pac4j.

the class CasOAuthWrapperClientTests method testParsingAttributesCASServerV4_2AndBefore.

@Test
public void testParsingAttributesCASServerV4_2AndBefore() throws IOException {
    final JsonFactory jsonFactory = new JsonFactory(new ObjectMapper());
    final Map<String, Object> attributes = new HashMap<>();
    attributes.put(KEY, VALUE);
    attributes.put(NAME, TOKEN);
    final StringWriter writer = new StringWriter();
    try (final JsonGenerator jsonGenerator = jsonFactory.createJsonGenerator(writer)) {
        jsonGenerator.writeStartObject();
        jsonGenerator.writeStringField("id", ID);
        jsonGenerator.writeArrayFieldStart("attributes");
        for (final Map.Entry<String, Object> entry : attributes.entrySet()) {
            jsonGenerator.writeStartObject();
            jsonGenerator.writeObjectField(entry.getKey(), entry.getValue());
            jsonGenerator.writeEndObject();
        }
        jsonGenerator.writeEndArray();
        jsonGenerator.writeEndObject();
    }
    final String body = writer.toString();
    final CasOAuthWrapperClient client = new CasOAuthWrapperClient();
    client.setKey(KEY);
    client.setSecret(SECRET);
    client.setCasOAuthUrl(CALLBACK_URL);
    client.setCallbackUrl(CALLBACK_URL);
    client.init();
    final CasOAuthWrapperProfile profile = new CasOAuthWrapperProfileDefinition().extractUserProfile(body);
    assertEquals(ID, profile.getId());
    assertEquals(2, profile.getAttributes().size());
    assertEquals(VALUE, profile.getAttribute(KEY));
    assertEquals(TOKEN, profile.getAttribute(NAME));
}
Also used : CasOAuthWrapperProfileDefinition(org.pac4j.oauth.profile.casoauthwrapper.CasOAuthWrapperProfileDefinition) HashMap(java.util.HashMap) JsonFactory(com.fasterxml.jackson.core.JsonFactory) CasOAuthWrapperProfile(org.pac4j.oauth.profile.casoauthwrapper.CasOAuthWrapperProfile) StringWriter(java.io.StringWriter) JsonGenerator(com.fasterxml.jackson.core.JsonGenerator) Map(java.util.Map) HashMap(java.util.HashMap) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.Test)

Example 45 with JsonFactory

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

the class JsonSerializer method serialize.

@Override
public InputStream serialize(Document doc) throws TranslatorException {
    ByteArrayOutputStream outputStream = null;
    try {
        outputStream = new ByteArrayOutputStream(1024 * 10);
        JsonGenerator json = new JsonFactory().createGenerator(outputStream);
        writeDocument(doc, null, json, false);
        json.close();
        return new ByteArrayInputStream(outputStream.toByteArray());
    } catch (IOException | SQLException e) {
        throw new TranslatorException(e);
    } finally {
        if (outputStream != null) {
            try {
                outputStream.close();
            } catch (IOException e) {
            // ignore.
            }
        }
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) SQLException(java.sql.SQLException) JsonFactory(com.fasterxml.jackson.core.JsonFactory) JsonGenerator(com.fasterxml.jackson.core.JsonGenerator) TranslatorException(org.teiid.translator.TranslatorException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException)

Aggregations

JsonFactory (com.fasterxml.jackson.core.JsonFactory)266 JsonGenerator (com.fasterxml.jackson.core.JsonGenerator)102 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)91 JsonParser (com.fasterxml.jackson.core.JsonParser)78 Test (org.junit.Test)65 IOException (java.io.IOException)62 StringWriter (java.io.StringWriter)60 Map (java.util.Map)27 HashMap (java.util.HashMap)26 ArrayList (java.util.ArrayList)25 JsonNode (com.fasterxml.jackson.databind.JsonNode)21 List (java.util.List)18 ExtensibleJSONWriter (com.instagram.common.json.annotation.processor.support.ExtensibleJSONWriter)16 JsonToken (com.fasterxml.jackson.core.JsonToken)15 ByteArrayOutputStream (java.io.ByteArrayOutputStream)15 File (java.io.File)14 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)9 InputStreamReader (java.io.InputStreamReader)9 TypeReference (com.fasterxml.jackson.core.type.TypeReference)8 SimpleParseUUT (com.instagram.common.json.annotation.processor.uut.SimpleParseUUT)8