Skip to content

Package: ValueUtils

ValueUtils

nameinstructionbranchcomplexitylinemethod
ValueUtils()
M: 6 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
getValue(JsonValue)
M: 0 C: 21
100%
M: 1 C: 3
75%
M: 1 C: 2
67%
M: 0 C: 4
100%
M: 0 C: 1
100%
literalValue(JsonValue)
M: 8 C: 20
71%
M: 2 C: 4
67%
M: 2 C: 4
67%
M: 2 C: 5
71%
M: 0 C: 1
100%
stringValue(JsonValue)
M: 2 C: 8
80%
M: 1 C: 1
50%
M: 1 C: 1
50%
M: 0 C: 1
100%
M: 0 C: 1
100%

Coverage

1: /*
2: * JB4JSON-LD
3: * Copyright (C) 2024 Czech Technical University in Prague
4: *
5: * This library is free software; you can redistribute it and/or
6: * modify it under the terms of the GNU Lesser General Public
7: * License as published by the Free Software Foundation; either
8: * version 3.0 of the License, or (at your option) any later version.
9: *
10: * This library is distributed in the hope that it will be useful,
11: * but WITHOUT ANY WARRANTY; without even the implied warranty of
12: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13: * Lesser General Public License for more details.
14: *
15: * You should have received a copy of the GNU Lesser General Public
16: * License along with this library.
17: */
18: package cz.cvut.kbss.jsonld.deserialization.util;
19:
20: import cz.cvut.kbss.jsonld.JsonLd;
21: import cz.cvut.kbss.jsonld.exception.JsonLdDeserializationException;
22: import jakarta.json.JsonNumber;
23: import jakarta.json.JsonString;
24: import jakarta.json.JsonValue;
25:
26: public class ValueUtils {
27:
28: private ValueUtils() {
29: throw new AssertionError();
30: }
31:
32: /**
33: * Extracts the value of the {@link JsonLd#VALUE} attribute from the specified JSON object.
34: *
35: * @param jsonNode JSON object from which to extract the value
36: * @return Extracted value
37: * @throws JsonLdDeserializationException If the specified node is not a JSON object or if it does not contain a
38: * {@code @value} attribute
39: */
40: public static JsonValue getValue(JsonValue jsonNode) {
41:• if (jsonNode.getValueType() != JsonValue.ValueType.OBJECT || !jsonNode.asJsonObject()
42:• .containsKey(JsonLd.VALUE)) {
43: throw new JsonLdDeserializationException("Cannot deserialize node " + jsonNode + "as literal. " +
44: "It is missing attribute '" + JsonLd.VALUE + "'.");
45: }
46: return jsonNode.asJsonObject().get(JsonLd.VALUE);
47: }
48:
49: /**
50: * Returns a string representation of the specified JSON value.
51: *
52: * @param value Value to stringify
53: * @return String value
54: */
55: public static String stringValue(JsonValue value) {
56:• return value instanceof JsonString ? ((JsonString) value).getString() : value.toString();
57: }
58:
59: public static Object literalValue(JsonValue value) {
60:• switch(value.getValueType()) {
61: case STRING:
62: return ((JsonString) value).getString();
63: case NUMBER:
64: return ((JsonNumber) value).numberValue();
65: case TRUE:
66: return true;
67: case FALSE:
68: return false;
69: case NULL:
70: return null;
71: default:
72: throw new IllegalArgumentException("Value " + value + " is not a literal.");
73: }
74: }
75: }