Skip to content

Package: SerializerUtils

SerializerUtils

nameinstructionbranchcomplexitylinemethod
SerializerUtils()
M: 7 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
createTypedTermDefinition(String, String, String)
M: 0 C: 15
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 4
100%
M: 0 C: 1
100%
createdTypedValueNode(String, String, String)
M: 0 C: 15
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 4
100%
M: 0 C: 1
100%
isAnnotationReference(Object, SerializationContext)
M: 0 C: 15
100%
M: 0 C: 6
100%
M: 0 C: 4
100%
M: 0 C: 2
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.serialization.serializer;
19:
20: import cz.cvut.kbss.jsonld.JsonLd;
21: import cz.cvut.kbss.jsonld.common.BeanAnnotationProcessor;
22: import cz.cvut.kbss.jsonld.common.BeanClassProcessor;
23: import cz.cvut.kbss.jsonld.serialization.JsonNodeFactory;
24: import cz.cvut.kbss.jsonld.serialization.model.JsonNode;
25: import cz.cvut.kbss.jsonld.serialization.model.ObjectNode;
26: import cz.cvut.kbss.jsonld.serialization.traversal.SerializationContext;
27:
28: /**
29: * Utilities for serializers.
30: */
31: public class SerializerUtils {
32:
33: private SerializerUtils() {
34: throw new AssertionError("No instances for you!");
35: }
36:
37: /**
38: * Checks whether the specified value in the specified context is an annotation property value referencing an
39: * individual (resource).
40: *
41: * @param value Value to examine
42: * @param ctx Serialization context
43: * @return {@code true} if the value is an annotation property value reference, {@code false} otherwise
44: */
45: public static boolean isAnnotationReference(Object value, SerializationContext<?> ctx) {
46:• return BeanAnnotationProcessor.isAnnotationProperty(ctx.getField()) && BeanClassProcessor.isIdentifierType(
47: value.getClass()) && !(value instanceof String);
48: }
49:
50: /**
51: * Creates a term definition node containing identifier and type attributes.
52: * @param term Term whose definition to create
53: * @param id Mapped term identifier (IRI)
54: * @param type Type of the mapped term
55: * @return Term definition node
56: */
57: public static ObjectNode createTypedTermDefinition(String term, String id, String type) {
58: final ObjectNode termDef = JsonNodeFactory.createObjectNode(term);
59: termDef.addItem(JsonNodeFactory.createLiteralNode(JsonLd.ID, id));
60: termDef.addItem(JsonNodeFactory.createLiteralNode(JsonLd.TYPE, type));
61: return termDef;
62: }
63:
64: /**
65: * Serializes the specified value as a JSON object with value ({@link JsonLd#VALUE}) and type ({@link
66: * JsonLd#TYPE}).
67: *
68: * @param term Term to identify the object in the enclosing object
69: * @param value Value to serialize
70: * @param type Value type to use
71: * @return Resulting JSON node
72: */
73: public static JsonNode createdTypedValueNode(String term, String value, String type) {
74: final ObjectNode node = JsonNodeFactory.createObjectNode(term);
75: node.addItem(JsonNodeFactory.createLiteralNode(JsonLd.TYPE, type));
76: node.addItem(JsonNodeFactory.createLiteralNode(JsonLd.VALUE, value));
77: return node;
78: }
79: }