Search in sources :

Example 1 with JsonMapper

use of org.jdbi.v3.json.JsonMapper in project jdbi by jdbi.

the class JsonColumnMapperFactory method build.

@Override
public Optional<ColumnMapper<?>> build(Type type, ConfigRegistry config) {
    ColumnMappers cm = config.get(ColumnMappers.class);
    // look for specialized json support first, revert to simple String mapping if absent
    ColumnMapper<String> jsonStringMapper = JdbiOptionals.findFirstPresent(() -> cm.findFor(QualifiedType.of(String.class).with(EncodedJson.class)), () -> cm.findFor(String.class)).orElseThrow(() -> new UnableToProduceResultException(JSON_NOT_RETRIEVABLE));
    final JsonMapper mapper = config.get(JsonConfig.class).getJsonMapper();
    return Optional.of((rs, i, ctx) -> mapper.fromJson(type, Optional.ofNullable(jsonStringMapper.map(rs, i, ctx)).orElse(// sql null -> json null
    "null"), config));
}
Also used : JsonMapper(org.jdbi.v3.json.JsonMapper) JsonConfig(org.jdbi.v3.json.JsonConfig) ColumnMappers(org.jdbi.v3.core.mapper.ColumnMappers) UnableToProduceResultException(org.jdbi.v3.core.result.UnableToProduceResultException) EncodedJson(org.jdbi.v3.json.EncodedJson)

Aggregations

ColumnMappers (org.jdbi.v3.core.mapper.ColumnMappers)1 UnableToProduceResultException (org.jdbi.v3.core.result.UnableToProduceResultException)1 EncodedJson (org.jdbi.v3.json.EncodedJson)1 JsonConfig (org.jdbi.v3.json.JsonConfig)1 JsonMapper (org.jdbi.v3.json.JsonMapper)1