use of org.jdbi.v3.json.Json in project jdbi by jdbi.
the class TestGson2Plugin method typeCanBeOverridden.
@Test
public void typeCanBeOverridden() {
pgExtension.getJdbi().useHandle(h -> {
h.createUpdate("create table users(usr json)").execute();
Gson gson = new GsonBuilder().registerTypeAdapter(SuperUser.class, new SuperUserAdapter()).registerTypeAdapter(SubUser.class, new SubUserAdapter()).create();
h.getConfig(Gson2Config.class).setGson(gson);
h.createUpdate("insert into users(usr) values(:user)").bindByType("user", new SubUser(), QualifiedType.of(SuperUser.class).with(Json.class)).execute();
User subuser = h.createQuery("select usr from users").mapTo(QualifiedType.of(User.class).with(Json.class)).one();
assertThat(subuser.name).describedAs("instead of being bound via getClass(), the object was bound according to the type param").isEqualTo("super");
});
}
use of org.jdbi.v3.json.Json 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));
}
use of org.jdbi.v3.json.Json in project jdbi by jdbi.
the class JsonArgumentFactory method build.
@Override
public Optional<Argument> build(Type type, Object value, ConfigRegistry config) {
String nullableJson = value == null ? null : config.get(JsonConfig.class).getJsonMapper().toJson(type, value, config);
// json null -> sql null
String json = "null".equals(nullableJson) ? null : nullableJson;
Arguments a = config.get(Arguments.class);
// look for specialized json support first, revert to simple String binding if absent
return Optional.of(JdbiOptionals.findFirstPresent(() -> a.findFor(QualifiedType.of(String.class).with(EncodedJson.class), json), () -> a.findFor(String.class, json)).orElseThrow(() -> new UnableToCreateStatementException(JSON_NOT_STORABLE)));
}
use of org.jdbi.v3.json.Json in project jdbi by jdbi.
the class TestMoshiPlugin method typeCanBeOverridden.
@Test
public void typeCanBeOverridden() {
pgExtension.getJdbi().useHandle(h -> {
h.createUpdate("create table users(usr json)").execute();
Moshi moshi = new Moshi.Builder().add(new OptionalAdapter()).add(SuperUser.class, new SuperUserAdapter()).add(SubUser.class, new SubUserAdapter()).build();
h.getConfig(MoshiConfig.class).setMoshi(moshi);
h.createUpdate("insert into users(usr) values(:user)").bindByType("user", new SubUser(), QualifiedType.of(SuperUser.class).with(Json.class)).execute();
User subuser = h.createQuery("select usr from users").mapTo(QualifiedType.of(User.class).with(Json.class)).one();
assertThat(subuser.name).describedAs("instead of being bound via getClass(), the object was bound according to the qualified type param").isEqualTo("super");
});
}
use of org.jdbi.v3.json.Json in project jdbi by jdbi.
the class TestScript method testPostgresJsonExtractTextOperator.
@Test
public void testPostgresJsonExtractTextOperator() {
Handle h = pgExtension.openHandle();
Script script = h.createScript(getResourceOnClasspath("script/postgres-json-operator.sql"));
script.execute();
assertThat(h.select("select * from something").mapToMap()).hasSize(1);
}
Aggregations