[ 
https://issues.apache.org/jira/browse/AVRO-1687?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=14600069#comment-14600069
 ] 

Dean Ericson commented on AVRO-1687:
------------------------------------

[[email protected]], I am not seeing any good examples of parsing native JSON 
into Avro binary when i try and look through the KITE repository.  Are you 
aware of a specific test suite or just generally aware that they're trying to 
accomplish the same task? 

> Converting JSON to avro fails 
> ------------------------------
>
>                 Key: AVRO-1687
>                 URL: https://issues.apache.org/jira/browse/AVRO-1687
>             Project: Avro
>          Issue Type: Bug
>          Components: java
>    Affects Versions: 1.7.6, 1.7.7, 1.8.0
>            Reporter: Dean Ericson
>
> I am trying to validate a JSON string that is sent to me.  Unfortunately, I 
> get a stacktrace indicating that my schema and JSON are not compatable: 
> {code}org.apache.avro.AvroTypeException: Unknown union branch hour{code} This 
> does not seem right to me since the JSON being passed to me was created from 
> the same schema I am using to validate it.
> Here are my tests
> {code}
>       @BeforeClass
>       public static void testGenerateSchema() throws IOException {
>                    /************************************************
>                     * GENERATE SCHEMA
>                     ************************************************/
>               // get the reflected schema for packets
>               File schemaFile = new File("avro.schema");
>               schema = ReflectData.AllowNull.get().getSchema(Packet.class);
>               try (DataFileWriter<Schema> schemaWiter = new DataFileWriter<>(
>                               new GenericDatumWriter<Schema>())) {
>                       schemaWiter.create(schema, schemaFile);
>                       schemaWiter.close();
>               }
>               assertNotNull(schemaFile);
>               assertTrue(schemaFile.length() > 0);
>       }
>       @Test
>       public void testAvroJsonToBinarySerialization() throws Exception {
>                    /************************************************
>                     * SERIALIZE JSON
>                     ************************************************/
>               String json = "{\"cost\": 0, \"stamp\": {\"hour\": 12, 
> \"second\": 0}}";
>               Schema schema = new 
> Schema.Parser().parse(getSchema().toString());
>               System.out.println(schema);
>               System.out.println(jsonToAvro(json, schema));
>     }
>         // Code borrowed from the Avro-Tools project
>       private static String jsonToAvro(String json, Schema schema)
>                       throws IOException {
>               ByteArrayOutputStream baos = new ByteArrayOutputStream();
>               PrintStream out = new PrintStream(new 
> BufferedOutputStream(baos));
>               GenericDatumReader<Object> reader = new 
> GenericDatumReader<Object>(schema);
>               InputStream input = new 
> ByteArrayInputStream(json.getBytes("utf-8"));
>               JsonDecoder jsonDecoder = 
> DecoderFactory.get().jsonDecoder(schema,input);
>               GenericDatumWriter<Object> writer = new 
> GenericDatumWriter<Object>(schema);
>               Encoder e = EncoderFactory.get().binaryEncoder(out, null);
>               Object datum = null;
>               while (true) {
>                       try {
>                               datum = reader.read(datum, jsonDecoder);
>                       } catch (EOFException eofException) {
>                               break;
>                       }
>                       writer.write(datum, e);
>                       e.flush();
>               }
>               return baos.toString("utf-8");
>       }
> {code}



--
This message was sent by Atlassian JIRA
(v6.3.4#6332)

Reply via email to