https://github.com/goodforgod/avro-json-optional-decoder
⚙️ Java AVRO Decoder with JSON optional/default field support.
https://github.com/goodforgod/avro-json-optional-decoder
avro avro-decoder avro-default avro-optional avro-schema default default-values field java optional
Last synced: 6 months ago
JSON representation
⚙️ Java AVRO Decoder with JSON optional/default field support.
- Host: GitHub
- URL: https://github.com/goodforgod/avro-json-optional-decoder
- Owner: GoodforGod
- License: mit
- Created: 2020-10-21T20:07:52.000Z (almost 5 years ago)
- Default Branch: master
- Last Pushed: 2021-11-25T20:39:11.000Z (almost 4 years ago)
- Last Synced: 2025-03-30T07:01:48.219Z (6 months ago)
- Topics: avro, avro-decoder, avro-default, avro-optional, avro-schema, default, default-values, field, java, optional
- Language: Java
- Homepage:
- Size: 185 KB
- Stars: 1
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Avro Json Optional Decoder

[](https://sonarcloud.io/dashboard?id=GoodforGod_avro-json-optional-decoder)
[](https://sonarcloud.io/dashboard?id=GoodforGod_avro-json-optional-decoder)
[](https://sonarcloud.io/dashboard?id=GoodforGod_avro-json-optional-decoder)Avro Decoder with support optional fields in JSON.
[Based on Celos fork.](https://github.com/Celos/avro-json-decoder)## Dependency
**Gradle**
```groovy
dependencies {
implementation 'com.github.goodforgod:avro-json-optional-decoder:1.1.6'
}
```**Maven**
```xmlcom.github.goodforgod
avro-json-optional-decoder
1.1.6```
## Compatibility
Library is Java 1.8 compatible.
Library is compatible with different Apache Avro versions. Please use compatible library version for your Apache Avro version.
| [Apache Avro](https://mvnrepository.com/artifact/org.apache.avro/avro-compiler) Version | [Library](https://mvnrepository.com/artifact/com.github.goodforgod/avro-json-optional-decoder) Version |
| ---- | ---- |
| [1.9.2](https://mvnrepository.com/artifact/org.apache.avro/avro-compiler/1.9.2) | [1.1.6](https://mvnrepository.com/artifact/com.github.goodforgod/avro-json-optional-decoder/1.1.4) |
| [1.8.2](https://mvnrepository.com/artifact/org.apache.avro/avro-compiler/1.8.2) | [1.0.1](https://mvnrepository.com/artifact/com.github.goodforgod/avro-json-optional-decoder/1.0.1) |## Optional Field Problem
For given AVRO Schema.
```json
{
"type" : "record",
"name" : "Person",
"fields" : [ {
"name" : "username",
"type" : "string"
}, {
"name" : "name",
"type" : [ "null", "string" ]
} ]
}
```Such JSON will be treated correctly.
```json
{"username":"user1","name":null}
```However, such JSON (name field is missing).
```json
{"username":"user1"}
```Will fail with:
```log
org.apache.avro.AvroTypeException: Expected field name not found: name
```## Solution
**JsonOptionalDecoder** provided by library allow correct JSON validation in both cases,
decoding JSON that doesn't specify optional values, provided they have defaults.Check [guides](https://www.baeldung.com/java-apache-avro#2-deserialization) on how-to-use Avro Decoders.
Be aware JsonOptionalDecoder is not thread-safe.
### Optional Record Problem
For given AVRO Schema.
```json
{
"name": "Test",
"type": "record",
"fields": [
{
"name": "required",
"type": "string"
},
{
"name": "inner",
"type": [
"null",
{
"name": "inner",
"type": "record",
"fields": [
{
"name": "req",
"type": "string"
}
]
}
]
}
]
}
```This input will be correct:
```json
{"required":"u", "inner": {"req": "q"}}
```As this input will be correct:
```json
{"required":"u"}
```#### By Default
If property *default* is not specified, then missing field will be treated as [avro *null*](https://avro.apache.org/docs/1.9.2/spec.html#schema_primitive) value.
```json
{
"name" : "name",
"type" : [ "null", "string" ]
}
```You can specify default value as per AVRO specification.
Keep in mind you *mind putting type corresponding to default value first*, due to AVRO incorrect union type validation.
```json
{
"name" : "name",
"type" : [ "string", "null" ],
"default": "bob"
}
```## How To Use
Change
```java
Decoder decoder = DecoderFactory.get().jsonDecoder(SCHEMA, INPUT_STREAM_OR_STRING);
```With
```java
Decoder decoder = new JsonOptionalDecoder(SCHEMA, INPUT_STREAM_OR_STRING);
```## License
This project licensed under the MIT - see the [LICENSE](LICENSE) file for details.