Skip to content

Method: configuration()

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