Search in sources :

Example 41 with ObjectMapper

use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.ObjectMapper in project druid by druid-io.

the class ExtractionDimensionSpecTest method testSerdeWithType.

@Test
public void testSerdeWithType() throws Exception {
    final ObjectMapper objectMapper = new DefaultObjectMapper();
    final String oldJson = "{\n" + "    \"type\": \"extraction\",\n" + "    \"outputName\": \"first3Letters\",\n" + "    \"outputType\": \"LONG\",\n" + "    \"dimension\": \"myDim\"," + "    \"extractionFn\": {\n" + "        \"type\": \"regex\",\n" + "        \"expr\": \"(...).*\"\n" + "    }\n" + "}";
    final ExtractionDimensionSpec extractionDimensionSpec = (ExtractionDimensionSpec) objectMapper.readValue(oldJson, DimensionSpec.class);
    Assert.assertEquals("first3Letters", extractionDimensionSpec.getOutputName());
    Assert.assertEquals("myDim", extractionDimensionSpec.getDimension());
    Assert.assertNotNull(extractionDimensionSpec.getExtractionFn());
    Assert.assertEquals(ValueType.LONG, extractionDimensionSpec.getOutputType());
    Assert.assertTrue(extractionDimensionSpec.getExtractionFn() instanceof RegexDimExtractionFn);
    Assert.assertEquals(extractionDimensionSpec, objectMapper.readValue(objectMapper.writeValueAsBytes(extractionDimensionSpec), DimensionSpec.class));
}
Also used : DefaultObjectMapper(io.druid.jackson.DefaultObjectMapper) DefaultObjectMapper(io.druid.jackson.DefaultObjectMapper) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) RegexDimExtractionFn(io.druid.query.extraction.RegexDimExtractionFn) Test(org.junit.Test)

Example 42 with ObjectMapper

use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.ObjectMapper in project druid by druid-io.

the class ExtractionDimensionSpecTest method testSerdeBackwardsCompatibility.

@Test
public void testSerdeBackwardsCompatibility() throws Exception {
    final ObjectMapper objectMapper = new DefaultObjectMapper();
    final String oldJson = "{\n" + "    \"type\": \"extraction\",\n" + "    \"outputName\": \"first3Letters\",\n" + "    \"dimension\": \"myDim\"," + "    \"dimExtractionFn\": {\n" + "        \"type\": \"regex\",\n" + "        \"expr\": \"(...).*\"\n" + "    }\n" + "}";
    final ExtractionDimensionSpec extractionDimensionSpec = (ExtractionDimensionSpec) objectMapper.readValue(oldJson, DimensionSpec.class);
    Assert.assertEquals("first3Letters", extractionDimensionSpec.getOutputName());
    Assert.assertEquals("myDim", extractionDimensionSpec.getDimension());
    Assert.assertNotNull(extractionDimensionSpec.getExtractionFn());
    Assert.assertTrue(extractionDimensionSpec.getExtractionFn() instanceof RegexDimExtractionFn);
    Assert.assertEquals(extractionDimensionSpec, objectMapper.readValue(objectMapper.writeValueAsBytes(extractionDimensionSpec), DimensionSpec.class));
    // new trumps old
    final String oldAndNewJson = "{\n" + "    \"type\": \"extraction\",\n" + "    \"outputName\": \"first3Letters\",\n" + "    \"dimension\": \"myDim\"," + "    \"extractionFn\": {\n" + "        \"type\": \"partial\",\n" + "        \"expr\": \"(...).*\"\n" + "    },\n" + "    \"dimExtractionFn\": {\n" + "        \"type\": \"regex\",\n" + "        \"expr\": \"(...).*\"\n" + "    }\n" + "}";
    Assert.assertTrue(objectMapper.readValue(oldAndNewJson, DimensionSpec.class).getExtractionFn() instanceof MatchingDimExtractionFn);
}
Also used : MatchingDimExtractionFn(io.druid.query.extraction.MatchingDimExtractionFn) DefaultObjectMapper(io.druid.jackson.DefaultObjectMapper) DefaultObjectMapper(io.druid.jackson.DefaultObjectMapper) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) RegexDimExtractionFn(io.druid.query.extraction.RegexDimExtractionFn) Test(org.junit.Test)

Example 43 with ObjectMapper

use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.ObjectMapper in project druid by druid-io.

the class RegexFilteredDimensionSpecTest method testSerde.

@Test
public void testSerde() throws Exception {
    ObjectMapper mapper = TestHelper.getObjectMapper();
    String jsonStr = "{\n" + "  \"type\": \"regexFiltered\",\n" + "  \"delegate\": {\n" + "    \"type\": \"default\",\n" + "    \"dimension\": \"foo\",\n" + "    \"outputName\": \"bar\"\n" + "  },\n" + "  \"pattern\": \"xxx\"\n" + "}";
    RegexFilteredDimensionSpec actual = (RegexFilteredDimensionSpec) mapper.readValue(mapper.writeValueAsString(mapper.readValue(jsonStr, DimensionSpec.class)), DimensionSpec.class);
    RegexFilteredDimensionSpec expected = new RegexFilteredDimensionSpec(new DefaultDimensionSpec("foo", "bar"), "xxx");
    Assert.assertEquals(expected, actual);
}
Also used : ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.Test)

Example 44 with ObjectMapper

use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.ObjectMapper in project druid by druid-io.

the class SubstringDimExtractionFnTest method testSerde.

@Test
public void testSerde() throws Exception {
    final ObjectMapper objectMapper = new DefaultObjectMapper();
    final String json = "{ \"type\" : \"substring\", \"index\" : 1, \"length\" : 3 }";
    final String jsonNoLength = "{ \"type\" : \"substring\", \"index\" : 1 }";
    SubstringDimExtractionFn extractionFn = (SubstringDimExtractionFn) objectMapper.readValue(json, ExtractionFn.class);
    SubstringDimExtractionFn extractionFnNoLength = (SubstringDimExtractionFn) objectMapper.readValue(jsonNoLength, ExtractionFn.class);
    Assert.assertEquals(1, extractionFn.getIndex());
    Assert.assertEquals(new Integer(3), extractionFn.getLength());
    Assert.assertEquals(1, extractionFnNoLength.getIndex());
    Assert.assertEquals(null, extractionFnNoLength.getLength());
    // round trip
    Assert.assertEquals(extractionFn, objectMapper.readValue(objectMapper.writeValueAsBytes(extractionFn), ExtractionFn.class));
    Assert.assertEquals(extractionFnNoLength, objectMapper.readValue(objectMapper.writeValueAsBytes(extractionFnNoLength), ExtractionFn.class));
}
Also used : DefaultObjectMapper(io.druid.jackson.DefaultObjectMapper) DefaultObjectMapper(io.druid.jackson.DefaultObjectMapper) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.Test)

Example 45 with ObjectMapper

use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.ObjectMapper in project druid by druid-io.

the class TimeDimExtractionFnTest method testSerde.

@Test
public void testSerde() throws Exception {
    final ObjectMapper objectMapper = new DefaultObjectMapper();
    final String json = "{ \"type\" : \"time\", \"timeFormat\" : \"MM/dd/yyyy\", \"resultFormat\" : \"QQQ/yyyy\" }";
    TimeDimExtractionFn extractionFn = (TimeDimExtractionFn) objectMapper.readValue(json, ExtractionFn.class);
    Assert.assertEquals("MM/dd/yyyy", extractionFn.getTimeFormat());
    Assert.assertEquals("QQQ/yyyy", extractionFn.getResultFormat());
    // round trip
    Assert.assertEquals(extractionFn, objectMapper.readValue(objectMapper.writeValueAsBytes(extractionFn), ExtractionFn.class));
}
Also used : DefaultObjectMapper(io.druid.jackson.DefaultObjectMapper) DefaultObjectMapper(io.druid.jackson.DefaultObjectMapper) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.Test)

Aggregations

ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)5262 Test (org.junit.Test)1989 IOException (java.io.IOException)837 JsonNode (com.fasterxml.jackson.databind.JsonNode)724 HashMap (java.util.HashMap)375 Map (java.util.Map)367 ArrayList (java.util.ArrayList)346 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)318 File (java.io.File)282 List (java.util.List)222 Test (org.junit.jupiter.api.Test)205 DefaultObjectMapper (org.apache.druid.jackson.DefaultObjectMapper)202 Before (org.junit.Before)202 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)197 InputStream (java.io.InputStream)143 SimpleModule (com.fasterxml.jackson.databind.module.SimpleModule)137 DefaultObjectMapper (io.druid.jackson.DefaultObjectMapper)127 Matchers.containsString (org.hamcrest.Matchers.containsString)127 TypeReference (com.fasterxml.jackson.core.type.TypeReference)108 ArrayNode (com.fasterxml.jackson.databind.node.ArrayNode)99