diff options
author | Valerij Fredriksen <valerijf@vespa.ai> | 2024-02-26 15:49:47 +0100 |
---|---|---|
committer | Valerij Fredriksen <valerijf@vespa.ai> | 2024-02-26 15:49:47 +0100 |
commit | db245fbde8ab6546520747917ab7ab5b94ec17dc (patch) | |
tree | 113c6bb12dadb4a0815de9f9f33ec3c88aab1395 | |
parent | e3cb3fe6e1684cbb54b44b4ae26193d3bdd624a1 (diff) |
Move Json to vespajlib
-rw-r--r-- | container-core/src/main/java/com/yahoo/restapi/RestApiMappers.java | 1 | ||||
-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/test/java/ai/vespa/json/JsonTest.java (renamed from container-core/src/test/java/com/yahoo/restapi/JsonTest.java) | 14 |
4 files changed, 26 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 3204c6c348a..ef3bb1662f2 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/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/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()); } |