SchemaDeserializer extends JsonDeserializer<Schema> { @Override public Schema deserialize(JsonParser parser, DeserializationContext context) throws IOException, JsonProcessingException { Parser schemaParser = new Schema.Parser(); schemaParser.setValidateDefaults(true); schemaParser.setValidate(true); return schemaParser.parse(parser.readValueAsTree().toString()); }  @Override Schema deserialize(JsonParser parser, DeserializationContext context); @Override Class<?> handledType();  }
@Test public void deserialize() throws JsonProcessingException, IOException { when(parser.readValueAsTree()).thenReturn(jsonNode); Schema result = new SchemaDeserializer().deserialize(parser, null); assertThat(result, is(schema)); verify(parser).readValueAsTree(); }
@Test(expected = AvroTypeException.class) public void detectsInvalidDefault() throws JsonProcessingException, IOException { jsonNode = new ObjectMapper().readTree( "{\"type\":\"record\",\"name\":\"mine\",\"fields\":[{\"name\":\"str\",\"type\":\"int\",\"default\":\"0\"}]}"); when(parser.readValueAsTree()).thenReturn(jsonNode); new SchemaDeserializer().deserialize(parser, null); }