@Override public Schema apply(JsonNode node) { return JsonUtil.inferSchemaWithMaps(node, "Test"); } }));
@Test public void testSchemaInferenceMap() throws Exception { Schema recordSchema = SchemaBuilder.record("Test").fields() .requiredString("aString") .name("aMap").type().map().values().stringType().noDefault() .endRecord(); String jsonSample = "{" + "\"aString\": \"triangle\"," + "\"aMap\": { \"left\": \"timid\", \"right\": \"dictionary\" }" + "}"; JsonNode datum = JsonUtil.parse(jsonSample); Assert.assertEquals("Should produce expected schema", recordSchema, JsonUtil.inferSchemaWithMaps(datum, "Test")); Map<String, Object> aMap = Maps.newLinkedHashMap(); aMap.put("left", "timid"); aMap.put("right", "dictionary"); GenericData.Record expected = new GenericData.Record(recordSchema); expected.put("aString", "triangle"); expected.put("aMap", aMap); Assert.assertEquals("Should convert to record", expected, convertGeneric(datum, recordSchema)); }
@Test public void testSchemaInferenceNullableMap() throws Exception { Schema recordSchema = SchemaBuilder.record("Test").fields() .requiredString("aString") .name("aMap").type().map().values() .unionOf().nullType().and().stringType().endUnion().noDefault() .endRecord(); String jsonSample = "{" + "\"aString\": \"triangle\"," + "\"aMap\": { \"left\": null, \"right\": \"dictionary\" }" + "}"; JsonNode datum = JsonUtil.parse(jsonSample); Assert.assertEquals("Should produce expected schema", recordSchema, JsonUtil.inferSchemaWithMaps(datum, "Test")); Map<String, Object> aMap = Maps.newLinkedHashMap(); aMap.put("left", null); aMap.put("right", "dictionary"); GenericData.Record expected = new GenericData.Record(recordSchema); expected.put("aString", "triangle"); expected.put("aMap", aMap); Assert.assertEquals("Should convert to record", expected, convertGeneric(datum, recordSchema)); }