Skip to content

Commit

Permalink
Remove Airlift JSON from clients
Browse files Browse the repository at this point in the history
  • Loading branch information
electrum committed Jun 22, 2021
1 parent d13e7f3 commit 475a048
Show file tree
Hide file tree
Showing 8 changed files with 99 additions and 29 deletions.
11 changes: 6 additions & 5 deletions client/trino-cli/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,6 @@
<artifactId>trino-parser</artifactId>
</dependency>

<dependency>
<groupId>io.airlift</groupId>
<artifactId>json</artifactId>
</dependency>

<dependency>
<groupId>io.airlift</groupId>
<artifactId>log</artifactId>
Expand Down Expand Up @@ -101,6 +96,12 @@
</dependency>

<!-- for testing -->
<dependency>
<groupId>io.airlift</groupId>
<artifactId>json</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>mockwebserver</artifactId>
Expand Down
16 changes: 11 additions & 5 deletions client/trino-client/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,6 @@
</properties>

<dependencies>
<dependency>
<groupId>io.airlift</groupId>
<artifactId>json</artifactId>
</dependency>

<dependency>
<groupId>io.airlift</groupId>
<artifactId>units</artifactId>
Expand All @@ -43,6 +38,11 @@
<artifactId>jackson-databind</artifactId>
</dependency>

<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jdk8</artifactId>
</dependency>

<dependency>
<groupId>com.google.code.findbugs</groupId>
<artifactId>jsr305</artifactId>
Expand Down Expand Up @@ -82,6 +82,12 @@
<scope>test</scope>
</dependency>

<dependency>
<groupId>io.airlift</groupId>
<artifactId>json</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>mockwebserver</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import io.airlift.json.ObjectMapperProvider;

import javax.annotation.concurrent.Immutable;

Expand Down Expand Up @@ -147,7 +146,7 @@ public int hashCode()
public static class ClientTypeSignatureParameterDeserializer
extends JsonDeserializer<ClientTypeSignatureParameter>
{
private static final ObjectMapper MAPPER = new ObjectMapperProvider().get();
private static final ObjectMapper MAPPER = JsonCodec.OBJECT_MAPPER_SUPPLIER.get();

@Override
public ClientTypeSignatureParameter deserialize(JsonParser jp, DeserializationContext ctxt)
Expand Down
70 changes: 70 additions & 0 deletions client/trino-client/src/main/java/io/trino/client/JsonCodec.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.trino.client;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.MapperFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.jdk8.Jdk8Module;

import java.lang.reflect.Type;
import java.util.function.Supplier;

import static java.util.Objects.requireNonNull;

public class JsonCodec<T>
{
// copy of https://github.com/airlift/airlift/blob/master/json/src/main/java/io/airlift/json/ObjectMapperProvider.java
static final Supplier<ObjectMapper> OBJECT_MAPPER_SUPPLIER = () -> new ObjectMapper()
.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES)
.disable(MapperFeature.AUTO_DETECT_CREATORS)
.disable(MapperFeature.AUTO_DETECT_FIELDS)
.disable(MapperFeature.AUTO_DETECT_SETTERS)
.disable(MapperFeature.AUTO_DETECT_GETTERS)
.disable(MapperFeature.AUTO_DETECT_IS_GETTERS)
.disable(MapperFeature.USE_GETTERS_AS_SETTERS)
.disable(MapperFeature.CAN_OVERRIDE_ACCESS_MODIFIERS)
.disable(MapperFeature.INFER_PROPERTY_MUTATORS)
.disable(MapperFeature.ALLOW_FINAL_FIELDS_AS_MUTATORS)
.registerModule(new Jdk8Module());

public static <T> JsonCodec<T> jsonCodec(Class<T> type)
{
return new JsonCodec<>(OBJECT_MAPPER_SUPPLIER.get(), type);
}

private final ObjectMapper mapper;
private final Type type;
private final JavaType javaType;

private JsonCodec(ObjectMapper mapper, Type type)
{
this.mapper = requireNonNull(mapper, "mapper is null");
this.type = requireNonNull(type, "type is null");
this.javaType = mapper.getTypeFactory().constructType(type);
}

public Type getType()
{
return type;
}

public T fromJson(String json)
throws JsonProcessingException
{
return mapper.readerFor(javaType).readValue(json);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
*/
package io.trino.client;

import io.airlift.json.JsonCodec;
import com.fasterxml.jackson.core.JsonProcessingException;
import okhttp3.Headers;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
Expand Down Expand Up @@ -65,7 +65,7 @@ private JsonResponse(int statusCode, String statusMessage, Headers headers, Stri
try {
value = jsonCodec.fromJson(responseBody);
}
catch (IllegalArgumentException e) {
catch (JsonProcessingException e) {
exception = new IllegalArgumentException(format("Unable to create %s from JSON response:\n[%s]", jsonCodec.getType(), responseBody), e);
}
this.hasValue = (exception == null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Sets;
import io.airlift.json.JsonCodec;
import io.airlift.units.Duration;
import okhttp3.Headers;
import okhttp3.HttpUrl;
Expand Down Expand Up @@ -49,7 +48,7 @@
import static com.google.common.base.Preconditions.checkState;
import static com.google.common.net.HttpHeaders.ACCEPT_ENCODING;
import static com.google.common.net.HttpHeaders.USER_AGENT;
import static io.airlift.json.JsonCodec.jsonCodec;
import static io.trino.client.JsonCodec.jsonCodec;
import static io.trino.client.ProtocolHeaders.TRINO_HEADERS;
import static java.lang.String.format;
import static java.net.HttpURLConnection.HTTP_OK;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import io.airlift.json.JsonCodec;
import io.trino.client.JsonCodec;
import io.trino.client.JsonResponse;
import net.jodah.failsafe.Failsafe;
import net.jodah.failsafe.FailsafeException;
Expand All @@ -34,7 +34,7 @@

import static com.google.common.base.MoreObjects.firstNonNull;
import static com.google.common.net.HttpHeaders.USER_AGENT;
import static io.airlift.json.JsonCodec.jsonCodec;
import static io.trino.client.JsonCodec.jsonCodec;
import static io.trino.client.JsonResponse.execute;
import static java.lang.String.format;
import static java.net.HttpURLConnection.HTTP_OK;
Expand Down
17 changes: 6 additions & 11 deletions client/trino-jdbc/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,6 @@
<artifactId>trino-client</artifactId>
</dependency>

<dependency>
<groupId>io.airlift</groupId>
<artifactId>json</artifactId>
<exclusions>
<exclusion>
<groupId>com.google.inject</groupId>
<artifactId>guice</artifactId>
</exclusion>
</exclusions>
</dependency>

<dependency>
<groupId>io.airlift</groupId>
<artifactId>log</artifactId>
Expand Down Expand Up @@ -157,6 +146,12 @@
<scope>test</scope>
</dependency>

<dependency>
<groupId>io.airlift</groupId>
<artifactId>json</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>io.airlift</groupId>
<artifactId>log-manager</artifactId>
Expand Down

0 comments on commit 475a048

Please sign in to comment.