Skip to content

Commit 0ea0a02

Browse files
authored
Update README.md
1 parent 0b2599d commit 0ea0a02

File tree

1 file changed

+56
-0
lines changed

1 file changed

+56
-0
lines changed

README.md

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,58 @@
11
# avro-json-decoder
22
JSON decoder for AVRO that infers default values. Based on org.apache.avro.io.JsonDecoder from <a href="https://github.com/apache/avro">AVRO</a> 1.8.2 and org.apache.avro.io.ExtendedJsonDecoder by <a href="https://github.com/zolyfarkas/avro">zolyfarkas</a>.
3+
4+
## why
5+
6+
Given this schema (in AVRO IDL)
7+
8+
```
9+
record User {
10+
string username;
11+
union {null, string} name = null;
12+
}
13+
```
14+
this record is valid and will be decoded properly
15+
```json
16+
{"username":"user1","name":null}
17+
```
18+
whereas this record
19+
```json
20+
{"username":"user1"}
21+
```
22+
will produce something like
23+
```
24+
org.apache.avro.AvroTypeException: Expected field name not found: name
25+
at org.apache.avro.io.JsonDecoder.doAction(JsonDecoder.java:495)
26+
at org.apache.avro.io.parsing.Parser.advance(Parser.java:88)
27+
at org.apache.avro.io.JsonDecoder.advance(JsonDecoder.java:157)
28+
at org.apache.avro.io.JsonDecoder.readIndex(JsonDecoder.java:447)
29+
...
30+
```
31+
or
32+
```
33+
org.apache.avro.AvroTypeException: Expected start-union. Got END_OBJECT
34+
at org.apache.avro.io.JsonDecoder.error(JsonDecoder.java:698)
35+
at org.apache.avro.io.JsonDecoder.readIndex(JsonDecoder.java:441)
36+
at org.apache.avro.io.ResolvingDecoder.doAction(ResolvingDecoder.java:290)
37+
at org.apache.avro.io.parsing.Parser.advance(Parser.java:88)
38+
...
39+
```
40+
41+
The decoder allows decoding JSON that doesn't specify optional values, provided they have defaults.
42+
43+
## how
44+
45+
Replace
46+
47+
```java
48+
Decoder decoder = DecoderFactory.get().jsonDecoder(SCHEMA, INPUT_STREAM_OR_STRING);
49+
```
50+
with
51+
```java
52+
Decoder decoder = new ExtendedJsonDecoder(SCHEMA, INPUT_STREAM_OR_STRING);
53+
```
54+
and pass it to your reader, as usual:
55+
```java
56+
SpecificDatumReader<T> reader = new SpecificDatumReader<>(SCHEMA_OR_CLASS);
57+
reader.read(null, decoder);
58+
```

0 commit comments

Comments
 (0)