0
private static final String json = "{" + "\"name\":\"Frank\"," + "\"age\":\"47\"" + "}";
private static final Schema schema1 = new Schema.Parser().parse("{ \"type\":\"record\", \"namespace\":\"foo\", \"name\":\"Person\", \"fields\":[ { \"name\":\"name\", \"type\":\"string\" }, { \"name\":\"age\", \"type\":\"string\" } ] }");
private static final Schema schema2 = new Schema.Parser().setValidate(true).parse("{ \"type\":\"record\", \"namespace\":\"foo\", \"name\":\"Person\", \"fields\":[ { \"name\":\"name\", \"type\":\"string\" }, { \"name\":\"age\", \"type\":\"int\" } ] }");

1)if we going to validate above json with schema1 it should say true

2)if we going to validate above json with schema2 it should say false due to "age" field is int according to schema, but in json it is string

public static boolean validateJson(byte[] data, Schema schema) throws Exception {
    InputStream input = new ByteArrayInputStream(data);
    DataInputStream din = new DataInputStream(input);
    GenericDatumReader<Object> reader = null;
    Object datum = null;
    BinaryDecoder binaryDecoder =
            DecoderFactory.get().binaryDecoder(input, null);
    try {
        reader = new GenericDatumReader<Object>(schema);
        DatumWriter<Object> writer = new GenericDatumWriter<Object>(schema);
        Decoder decoder = DecoderFactory.get().binaryDecoder(input, null);
        reader.read(din, decoder);
        return true;
    } catch (AvroTypeException e) {
        System.out.println(e.getMessage());
        return false;
    }
}

public static byte[] jsonToAvro(String json, Schema schema) throws IOException {
    DatumReader<Object> reader = new GenericDatumReader<>(schema);
    GenericDatumWriter<Object> writer = new GenericDatumWriter<>(schema);
    ByteArrayOutputStream output = new ByteArrayOutputStream();
    Decoder decoder = DecoderFactory.get().jsonDecoder(schema, json);
    Encoder encoder = EncoderFactory.get().binaryEncoder(output, null);
    Object datum = reader.read(null, decoder);
    writer.write(datum, encoder);
    encoder.flush();
    return output.toByteArray();
}
Doss
  • 31
  • 1
  • 14
  • And? What do you get for 2 ? Do you use Avro 1.8 libs ? – Harold Jun 27 '18 at 11:58
  • Thanks Harold, always getting true.when i print json data(json & schema2)getting age value is 2 as int,but when i print data(json & schema1)age value is 47 as string – Doss Jun 27 '18 at 15:32
  • using Avro 1.8.2 lib – Doss Jun 27 '18 at 15:37
  • Can't reproduce the problem with my Avro encoder . I've got `AvroTypeException, Expected int. Got VALUE_STRING`. Try changing the record type of Schema2 to "Person2". – Harold Jun 27 '18 at 15:54
  • can you plz share ur code snap.. – Doss Jun 27 '18 at 16:27
  • have added and updated code – Doss Jun 27 '18 at 16:38
  • You get 2 when it's an int because you're parsing the Avro binary data containing the UTF8 length of the string (2 characters)... Try larger ages, I think you'll notice a pattern – OneCricketeer Jun 28 '18 at 04:27
  • Thanks,my problem is not why am getting 2,the problem is " want to validate above json with schema2 it should say false due to "age" field is int according to schema, but in json it is string " – Doss Jun 28 '18 at 06:16
  • @Doss, Can't share my code here, but I've just pushed part of it to github there https://github.com/hbraux/java-utils/blob/master/src/main/java/fr/braux/tests/AvroEncoder.java and added a small Junit – Harold Jun 28 '18 at 07:30
  • Thanks for your effort,but i think you are using same schema,my question want to decode with different schema.my sample code is pushed part of it in github there https://github.com/bmdoss/java-utils/blob/master/Json2Avro.java have a take a look – Doss Jun 28 '18 at 10:02

0 Answers0