T
- Type to bind deserializer for.public interface JsonbDeserializer<T>
Interface representing a custom deserializer for a given type. It provides a low-level API for java object
deserialization from JSON stream using JsonParser
. Unlike JsonbAdapter
,
which acts more as converter from one java type to another, deserializer provides more fine grained control over
deserialization process.
DeserializationContext
acts as JSONB runtime, able to deserialize any java object provided.
Sample of custom Deserializer:
class Box { public BoxInner boxInnerObject; public String name; } BoxDeserializer implements JsonbDeserializer<Box> { public Box deserialize(JsonParser parser, DeserializationContext ctx, Type rtType) { Box = new Box(); while (parser.hasNext()) { Event event = parser.next(); if (event == JsonParser.Event.KEY_NAME && parser.getString().equals("boxInnerObject") { // Deserialize inner object box.boxInnerObject = ctx.deserialize(BoxInner.class, jsonParser); } else if (event == JsonParser.Event.KEY_NAME && parser.getString().equals("name") { // Deserialize name property parser.next(); // move to VALUE box.name = parser.getString(); } } return box; } }
Deserializers are registered using JsonbConfig.withDeserializers(JsonbDeserializer[])
method or using JsonbTypeDeserializer
annotation on type.
JsonbConfig
,
JsonbTypeDeserializer
,
JsonbSerializer
,
JsonbAdapter
Modifier and Type | Method and Description |
---|---|
T |
deserialize(JsonParser parser,
DeserializationContext ctx,
Type rtType)
Deserialize JSON stream into object.
|
T deserialize(JsonParser parser, DeserializationContext ctx, Type rtType)
parser
- Json parser.ctx
- Deserialization context.rtType
- Type of returned object.Copyright © 2018,2020 Eclipse Foundation.
Use is subject to license terms.