Skip to content

Package: JsonLdSerializer

JsonLdSerializer

nameinstructionbranchcomplexitylinemethod
JsonLdSerializer(JsonGenerator)
M: 0 C: 13
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 4
100%
M: 0 C: 1
100%
createCompactedJsonLdSerializer(JsonGenerator)
M: 5 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
serialize(Object)
M: 0 C: 12
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 4
100%
M: 0 C: 1
100%

Coverage

1: /**
2: * Copyright (C) 2016 Czech Technical University in Prague
3: *
4: * This program is free software: you can redistribute it and/or modify it under
5: * the terms of the GNU General Public License as published by the Free Software
6: * Foundation, either version 3 of the License, or (at your option) any
7: * later version.
8: *
9: * This program is distributed in the hope that it will be useful, but WITHOUT
10: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11: * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
12: * details. You should have received a copy of the GNU General Public License
13: * along with this program. If not, see <http://www.gnu.org/licenses/>.
14: */
15: package cz.cvut.kbss.jsonld.serialization;
16:
17: import cz.cvut.kbss.jsonld.serialization.model.JsonNode;
18: import cz.cvut.kbss.jsonld.serialization.traversal.ObjectGraphTraverser;
19:
20: import java.util.Objects;
21:
22: /**
23: * Base class for all JSON-LD serializers.
24: * <p>
25: * The serializers will mostly differ in the form of the generated JSON. E.g. the output can be expanded, using contexts
26: * etc.
27: */
28: public abstract class JsonLdSerializer {
29:
30: final ObjectGraphTraverser traverser = new ObjectGraphTraverser();
31:
32: final JsonGenerator jsonGenerator;
33:
34: protected JsonLdSerializer(JsonGenerator jsonGenerator) {
35: this.jsonGenerator = Objects.requireNonNull(jsonGenerator);
36: }
37:
38: /**
39: * Serializes object graph with the specified root.
40: * <p>
41: * The serialization builds a JSON-LD tree model and then writes it using a {@link JsonGenerator}, which was
42: * passed to this instance in constructor.
43: *
44: * @param root Object graph root
45: */
46: public void serialize(Object root) {
47: Objects.requireNonNull(root);
48: final JsonNode jsonRoot = buildJsonTree(root);
49: jsonRoot.write(jsonGenerator);
50: }
51:
52: /**
53: * Builds the JSON-LD tree model.
54: *
55: * @param root Object graph root
56: * @return {@link JsonNode} corresponding to the JSON-LD's tree root
57: */
58: abstract JsonNode buildJsonTree(Object root);
59:
60: public static JsonLdSerializer createCompactedJsonLdSerializer(JsonGenerator jsonWriter) {
61: return new CompactedJsonLdSerializer(jsonWriter);
62: }
63: }