Search in sources :

Example 1 with Json

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");
    });
}
Also used : GsonBuilder(com.google.gson.GsonBuilder) Gson(com.google.gson.Gson) AbstractJsonMapperTest(org.jdbi.v3.json.AbstractJsonMapperTest) Test(org.junit.jupiter.api.Test)

Example 2 with Json

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));
}
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)

Example 3 with Json

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)));
}
Also used : JsonConfig(org.jdbi.v3.json.JsonConfig) Arguments(org.jdbi.v3.core.argument.Arguments) UnableToCreateStatementException(org.jdbi.v3.core.statement.UnableToCreateStatementException)

Example 4 with Json

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");
    });
}
Also used : Moshi(com.squareup.moshi.Moshi) MultiDatabaseBuilder(de.softwareforge.testing.postgres.junit5.MultiDatabaseBuilder) AbstractJsonMapperTest(org.jdbi.v3.json.AbstractJsonMapperTest) Test(org.junit.jupiter.api.Test)

Example 5 with Json

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);
}
Also used : Handle(org.jdbi.v3.core.Handle) Test(org.junit.jupiter.api.Test)

Aggregations

Test (org.junit.jupiter.api.Test)3 Gson (com.google.gson.Gson)2 UnableToProduceResultException (org.jdbi.v3.core.result.UnableToProduceResultException)2 AbstractJsonMapperTest (org.jdbi.v3.json.AbstractJsonMapperTest)2 JsonConfig (org.jdbi.v3.json.JsonConfig)2 FortuneRepository (com.example.helloworld.db.jdbi.FortuneRepository)1 WorldRepository (com.example.helloworld.db.jdbi.WorldRepository)1 FortuneResource (com.example.helloworld.resources.FortuneResource)1 WorldResource (com.example.helloworld.resources.WorldResource)1 ObjectReader (com.fasterxml.jackson.databind.ObjectReader)1 GsonBuilder (com.google.gson.GsonBuilder)1 Moshi (com.squareup.moshi.Moshi)1 MultiDatabaseBuilder (de.softwareforge.testing.postgres.junit5.MultiDatabaseBuilder)1 JdbiFactory (io.dropwizard.jdbi3.JdbiFactory)1 IOException (java.io.IOException)1 Response (javax.ws.rs.core.Response)1 AuthUser (org.broadinstitute.consent.http.models.AuthUser)1 LibraryCard (org.broadinstitute.consent.http.models.LibraryCard)1 User (org.broadinstitute.consent.http.models.User)1 Handle (org.jdbi.v3.core.Handle)1