-
Notifications
You must be signed in to change notification settings - Fork 2
/
ValueUtils.java
58 lines (52 loc) · 2.13 KB
/
ValueUtils.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package cz.cvut.kbss.jsonld.deserialization.util;
import cz.cvut.kbss.jsonld.JsonLd;
import cz.cvut.kbss.jsonld.exception.JsonLdDeserializationException;
import jakarta.json.JsonNumber;
import jakarta.json.JsonString;
import jakarta.json.JsonValue;
public class ValueUtils {
private ValueUtils() {
throw new AssertionError();
}
/**
* Extracts the value of the {@link JsonLd#VALUE} attribute from the specified JSON object.
*
* @param jsonNode JSON object from which to extract the value
* @return Extracted value
* @throws JsonLdDeserializationException If the specified node is not a JSON object or if it does not contain a
* {@code @value} attribute
*/
public static JsonValue getValue(JsonValue jsonNode) {
if (jsonNode.getValueType() != JsonValue.ValueType.OBJECT || !jsonNode.asJsonObject()
.containsKey(JsonLd.VALUE)) {
throw new JsonLdDeserializationException("Cannot deserialize node " + jsonNode + "as literal. " +
"It is missing attribute '" + JsonLd.VALUE + "'.");
}
return jsonNode.asJsonObject().get(JsonLd.VALUE);
}
/**
* Returns a string representation of the specified JSON value.
*
* @param value Value to stringify
* @return String value
*/
public static String stringValue(JsonValue value) {
return value instanceof JsonString ? ((JsonString) value).getString() : value.toString();
}
public static Object literalValue(JsonValue value) {
switch(value.getValueType()) {
case STRING:
return ((JsonString) value).getString();
case NUMBER:
return ((JsonNumber) value).numberValue();
case TRUE:
return true;
case FALSE:
return false;
case NULL:
return null;
default:
throw new IllegalArgumentException("Value " + value + " is not a literal.");
}
}
}