diff options
author | HÃ¥kon Hallingstad <hakon.hallingstad@gmail.com> | 2024-02-26 21:33:06 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-02-26 21:33:06 +0100 |
commit | 7e89f23fb55879d7265ae74bd218a29df642fe5d (patch) | |
tree | 7702ce4abe33369298f40d3efc35569202119228 | |
parent | 904b7b0e3aa97ba099e9f76e50556276ca504ebe (diff) | |
parent | c36a7ba78096f3d39b79048d6258e8a5b866ee4f (diff) |
Merge pull request #30411 from vespa-engine/freva/json
Move Json to vespajlib
-rw-r--r-- | container-core/src/main/java/com/yahoo/restapi/RestApiMappers.java | 1 | ||||
-rw-r--r-- | http-client/src/main/java/ai/vespa/hosted/client/AbstractHttpClient.java | 7 | ||||
-rw-r--r-- | http-client/src/main/java/ai/vespa/hosted/client/HttpClient.java | 4 | ||||
-rw-r--r-- | vespajlib/src/main/java/ai/vespa/json/InvalidJsonException.java | 11 | ||||
-rw-r--r-- | vespajlib/src/main/java/ai/vespa/json/Json.java (renamed from container-core/src/main/java/com/yahoo/restapi/Json.java) | 12 | ||||
-rw-r--r-- | vespajlib/src/main/java/ai/vespa/json/package-info.java | 5 | ||||
-rw-r--r-- | vespajlib/src/test/java/ai/vespa/json/JsonTest.java (renamed from container-core/src/test/java/com/yahoo/restapi/JsonTest.java) | 14 |
7 files changed, 42 insertions, 12 deletions
diff --git a/container-core/src/main/java/com/yahoo/restapi/RestApiMappers.java b/container-core/src/main/java/com/yahoo/restapi/RestApiMappers.java index 4ec170b5749..62b46d26ba9 100644 --- a/container-core/src/main/java/com/yahoo/restapi/RestApiMappers.java +++ b/container-core/src/main/java/com/yahoo/restapi/RestApiMappers.java @@ -1,6 +1,7 @@ // Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. package com.yahoo.restapi; +import ai.vespa.json.Json; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; import com.yahoo.container.jdisc.HttpResponse; diff --git a/http-client/src/main/java/ai/vespa/hosted/client/AbstractHttpClient.java b/http-client/src/main/java/ai/vespa/hosted/client/AbstractHttpClient.java index 9b08919600e..f6109f3ead8 100644 --- a/http-client/src/main/java/ai/vespa/hosted/client/AbstractHttpClient.java +++ b/http-client/src/main/java/ai/vespa/hosted/client/AbstractHttpClient.java @@ -4,6 +4,8 @@ package ai.vespa.hosted.client; import ai.vespa.http.HttpURL; import ai.vespa.http.HttpURL.Path; import ai.vespa.http.HttpURL.Query; +import ai.vespa.json.Json; +import com.yahoo.slime.SlimeUtils; import com.yahoo.time.TimeBudget; import org.apache.hc.client5.http.config.RequestConfig; import org.apache.hc.client5.http.impl.classic.CloseableHttpClient; @@ -282,6 +284,11 @@ public abstract class AbstractHttpClient implements HttpClient { } @Override + public Json readAsJson() { + return read(bytes -> Json.of(SlimeUtils.jsonToSlime(bytes))); + } + + @Override public void discard() throws UncheckedIOException, ResponseException { handle((response, __) -> { try (response) { diff --git a/http-client/src/main/java/ai/vespa/hosted/client/HttpClient.java b/http-client/src/main/java/ai/vespa/hosted/client/HttpClient.java index b764b3ad61a..57a7615421d 100644 --- a/http-client/src/main/java/ai/vespa/hosted/client/HttpClient.java +++ b/http-client/src/main/java/ai/vespa/hosted/client/HttpClient.java @@ -4,6 +4,7 @@ package ai.vespa.hosted.client; import ai.vespa.http.HttpURL; import ai.vespa.http.HttpURL.Path; import ai.vespa.http.HttpURL.Query; +import ai.vespa.json.Json; import com.yahoo.time.TimeBudget; import org.apache.hc.client5.http.config.RequestConfig; import org.apache.hc.core5.http.ClassicHttpRequest; @@ -140,6 +141,9 @@ public interface HttpClient extends Closeable { /** Reads and maps the response, or throws if unsuccessful. */ <T> T read(Function<byte[], T> mapper); + /** Reads the response as a {@link Json}, or throws if unsuccessful. */ + Json readAsJson(); + /** Discards the response, but throws if unsuccessful. */ void discard(); diff --git a/vespajlib/src/main/java/ai/vespa/json/InvalidJsonException.java b/vespajlib/src/main/java/ai/vespa/json/InvalidJsonException.java new file mode 100644 index 00000000000..6b1b039966c --- /dev/null +++ b/vespajlib/src/main/java/ai/vespa/json/InvalidJsonException.java @@ -0,0 +1,11 @@ +// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. +package ai.vespa.json; + +/** + * @author freva + */ +public class InvalidJsonException extends IllegalArgumentException { + public InvalidJsonException(String message) { + super(message); + } +} diff --git a/container-core/src/main/java/com/yahoo/restapi/Json.java b/vespajlib/src/main/java/ai/vespa/json/Json.java index 518dade2d22..b88c804c728 100644 --- a/container-core/src/main/java/com/yahoo/restapi/Json.java +++ b/vespajlib/src/main/java/ai/vespa/json/Json.java @@ -1,4 +1,4 @@ -package com.yahoo.restapi; +package ai.vespa.json; import com.yahoo.slime.Cursor; import com.yahoo.slime.Inspector; @@ -26,7 +26,7 @@ import static com.yahoo.slime.Type.ARRAY; import static com.yahoo.slime.Type.STRING; /** - * A {@link Slime} wrapper that throws {@link RestApiException.BadRequest} on missing members or invalid types. + * A {@link Slime} wrapper that throws {@link InvalidJsonException} on missing members or invalid types. * * @author bjorncs */ @@ -146,16 +146,16 @@ public class Json implements Iterable<Json> { private void requirePresent() { if (isMissing()) throw createMissingMemberException(); } - private RestApiException.BadRequest createInvalidTypeException(Type... expected) { + private InvalidJsonException createInvalidTypeException(Type... expected) { var expectedTypesString = Arrays.stream(expected).map(this::toString).collect(Collectors.joining("' or '", "'", "'")); var pathString = path.isEmpty() ? "JSON" : "JSON member '%s'".formatted(path); - return new RestApiException.BadRequest( + return new InvalidJsonException( "Expected %s to be a %s but got '%s'" .formatted(pathString, expectedTypesString, toString(inspector.type()))); } - private RestApiException.BadRequest createMissingMemberException() { - return new RestApiException.BadRequest(path.isEmpty() ? "Missing JSON" : "Missing JSON member '%s'".formatted(path)); + private InvalidJsonException createMissingMemberException() { + return new InvalidJsonException(path.isEmpty() ? "Missing JSON" : "Missing JSON member '%s'".formatted(path)); } private String toString(Type type) { diff --git a/vespajlib/src/main/java/ai/vespa/json/package-info.java b/vespajlib/src/main/java/ai/vespa/json/package-info.java new file mode 100644 index 00000000000..5a2df849d7f --- /dev/null +++ b/vespajlib/src/main/java/ai/vespa/json/package-info.java @@ -0,0 +1,5 @@ +// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. +@ExportPackage +package ai.vespa.json; + +import com.yahoo.osgi.annotation.ExportPackage; diff --git a/container-core/src/test/java/com/yahoo/restapi/JsonTest.java b/vespajlib/src/test/java/ai/vespa/json/JsonTest.java index 0ef4872c908..293e99227a7 100644 --- a/container-core/src/test/java/com/yahoo/restapi/JsonTest.java +++ b/vespajlib/src/test/java/ai/vespa/json/JsonTest.java @@ -1,8 +1,10 @@ -package com.yahoo.restapi; +package ai.vespa.json; import org.junit.jupiter.api.Test; -import static org.junit.jupiter.api.Assertions.*; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; /** * @author bjorncs @@ -65,16 +67,16 @@ class JsonTest { """; var json = Json.of(text); - var exception = assertThrows(RestApiException.BadRequest.class, () -> json.f("unknown").asString()); + var exception = assertThrows(InvalidJsonException.class, () -> json.f("unknown").asString()); assertEquals("Missing JSON member 'unknown'", exception.getMessage()); - exception = assertThrows(RestApiException.BadRequest.class, () -> json.a(0)); + exception = assertThrows(InvalidJsonException.class, () -> json.a(0)); assertEquals("Expected JSON to be a 'array' but got 'object'", exception.getMessage()); - exception = assertThrows(RestApiException.BadRequest.class, () -> json.f("string").f("unknown")); + exception = assertThrows(InvalidJsonException.class, () -> json.f("string").f("unknown")); assertEquals("Expected JSON member 'string' to be a 'object' but got 'string'", exception.getMessage()); - exception = assertThrows(RestApiException.BadRequest.class, () -> json.f("string").asLong()); + exception = assertThrows(InvalidJsonException.class, () -> json.f("string").asLong()); assertEquals("Expected JSON member 'string' to be a 'integer' or 'float' but got 'string'", exception.getMessage()); } |