Skip to contentMethod: serialize(Object)
1: /**
2: * Copyright (C) 2017 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.Configuration;
18: import cz.cvut.kbss.jsonld.common.Configurable;
19: import cz.cvut.kbss.jsonld.serialization.model.JsonNode;
20: import cz.cvut.kbss.jsonld.serialization.traversal.ObjectGraphTraverser;
21:
22: import java.util.Objects;
23:
24: /**
25: * Base class for all JSON-LD serializers.
26: * <p>
27: * The serializers will mostly differ in the form of the generated JSON. E.g. the output can be expanded, using contexts
28: * etc.
29: */
30: public abstract class JsonLdSerializer implements Configurable {
31:
32: private final Configuration configuration;
33:
34: final ObjectGraphTraverser traverser = new ObjectGraphTraverser();
35:
36: final JsonGenerator jsonGenerator;
37:
38: protected JsonLdSerializer(JsonGenerator jsonGenerator) {
39: this.jsonGenerator = Objects.requireNonNull(jsonGenerator);
40: this.configuration = new Configuration();
41: }
42:
43: public JsonLdSerializer(JsonGenerator jsonGenerator, Configuration configuration) {
44: this.jsonGenerator = Objects.requireNonNull(jsonGenerator);
45: this.configuration = Objects.requireNonNull(configuration);
46: }
47:
48: @Override
49: public Configuration configure() {
50: return configuration;
51: }
52:
53: /**
54: * Serializes object graph with the specified root.
55: * <p>
56: * The serialization builds a JSON-LD tree model and then writes it using a {@link JsonGenerator}, which was
57: * passed to this instance in constructor.
58: *
59: * @param root Object graph root
60: */
61: public void serialize(Object root) {
62: Objects.requireNonNull(root);
63: final JsonNode jsonRoot = buildJsonTree(root);
64: jsonRoot.write(jsonGenerator);
65: }
66:
67: /**
68: * Builds the JSON-LD tree model.
69: *
70: * @param root Object graph root
71: * @return {@link JsonNode} corresponding to the JSON-LD's tree root
72: */
73: abstract JsonNode buildJsonTree(Object root);
74:
75: public static JsonLdSerializer createCompactedJsonLdSerializer(JsonGenerator jsonWriter) {
76: return new CompactedJsonLdSerializer(jsonWriter);
77: }
78:
79: public static JsonLdSerializer createCompactedJsonLdSerializer(JsonGenerator jsonWriter,
80: Configuration configuration) {
81: return new CompactedJsonLdSerializer(jsonWriter, configuration);
82: }
83: }