mirror of https://github.com/apache/druid.git
JSON: Fix incorrect translation of null to "null". (#4939)
This commit is contained in:
parent
57a4038379
commit
928b083a7a
|
@ -22,17 +22,63 @@ package io.druid.data.input.impl;
|
||||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
import com.google.common.collect.ImmutableList;
|
import com.google.common.collect.ImmutableList;
|
||||||
import io.druid.TestObjectMapper;
|
import io.druid.TestObjectMapper;
|
||||||
|
import io.druid.java.util.common.parsers.JSONPathFieldSpec;
|
||||||
|
import io.druid.java.util.common.parsers.JSONPathFieldType;
|
||||||
|
import io.druid.java.util.common.parsers.JSONPathSpec;
|
||||||
|
import io.druid.java.util.common.parsers.Parser;
|
||||||
import org.junit.Assert;
|
import org.junit.Assert;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
public class JSONParseSpecTest
|
public class JSONParseSpecTest
|
||||||
{
|
{
|
||||||
private final ObjectMapper jsonMapper = new TestObjectMapper();
|
private final ObjectMapper jsonMapper = new TestObjectMapper();
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testParseRow()
|
||||||
|
{
|
||||||
|
final JSONParseSpec parseSpec = new JSONParseSpec(
|
||||||
|
new TimestampSpec("timestamp", "iso", null),
|
||||||
|
new DimensionsSpec(DimensionsSpec.getDefaultSchemas(ImmutableList.of("bar", "foo")), null, null),
|
||||||
|
new JSONPathSpec(
|
||||||
|
true,
|
||||||
|
ImmutableList.of(
|
||||||
|
new JSONPathFieldSpec(JSONPathFieldType.ROOT, "root_baz", "baz"),
|
||||||
|
new JSONPathFieldSpec(JSONPathFieldType.ROOT, "root_baz2", "baz2"),
|
||||||
|
new JSONPathFieldSpec(JSONPathFieldType.PATH, "path_omg", "$.o.mg"),
|
||||||
|
new JSONPathFieldSpec(JSONPathFieldType.PATH, "path_omg2", "$.o.mg2"),
|
||||||
|
new JSONPathFieldSpec(JSONPathFieldType.JQ, "jq_omg", ".o.mg"),
|
||||||
|
new JSONPathFieldSpec(JSONPathFieldType.JQ, "jq_omg2", ".o.mg2")
|
||||||
|
)
|
||||||
|
),
|
||||||
|
null
|
||||||
|
);
|
||||||
|
|
||||||
|
final Map<String, Object> expected = new HashMap<>();
|
||||||
|
expected.put("foo", "x");
|
||||||
|
expected.put("baz", 4L);
|
||||||
|
expected.put("root_baz", 4L);
|
||||||
|
expected.put("root_baz2", null);
|
||||||
|
expected.put("path_omg", 1L);
|
||||||
|
expected.put("path_omg2", null);
|
||||||
|
expected.put("jq_omg", 1L);
|
||||||
|
expected.put("jq_omg2", null);
|
||||||
|
|
||||||
|
final Parser<String, Object> parser = parseSpec.makeParser();
|
||||||
|
final Map<String, Object> parsedRow = parser.parse("{\"bar\":null,\"foo\":\"x\",\"baz\":4,\"o\":{\"mg\":1}}");
|
||||||
|
Assert.assertNotNull(parsedRow);
|
||||||
|
Assert.assertEquals(expected, parsedRow);
|
||||||
|
Assert.assertNull(parsedRow.get("bar"));
|
||||||
|
Assert.assertNull(parsedRow.get("buzz"));
|
||||||
|
Assert.assertNull(parsedRow.get("root_baz2"));
|
||||||
|
Assert.assertNull(parsedRow.get("jq_omg2"));
|
||||||
|
Assert.assertNull(parsedRow.get("path_omg2"));
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testSerde() throws IOException
|
public void testSerde() throws IOException
|
||||||
{
|
{
|
||||||
|
|
|
@ -100,7 +100,7 @@ public class JSONFlattenerMaker implements ObjectFlatteners.FlattenerMaker<JsonN
|
||||||
@Nullable
|
@Nullable
|
||||||
private Object valueConversionFunction(JsonNode val)
|
private Object valueConversionFunction(JsonNode val)
|
||||||
{
|
{
|
||||||
if (val == null) {
|
if (val == null || val.isNull()) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -78,7 +78,7 @@ public class ObjectFlatteners
|
||||||
@Override
|
@Override
|
||||||
public int size()
|
public int size()
|
||||||
{
|
{
|
||||||
throw new UnsupportedOperationException();
|
return keySet().size();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
Loading…
Reference in New Issue