Skip to contentMethod: serialize(Object)
1: /**
2: * Copyright (C) 2022 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 etc.
29: */
30: public abstract class JsonLdSerializer implements Configurable {
31:
32: private final Configuration configuration;
33:
34: final JsonGenerator jsonGenerator;
35:
36: final ValueSerializers serializers = new CommonValueSerializers();
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 configuration() {
50: return configuration;
51: }
52:
53: /**
54: * Registers a custom serializer for the specified type.
55: * <p>
56: * If a serializer already existed for the type, it is replaced by the new one.
57: *
58: * @param type Type to register the serializer for
59: * @param serializer Serializer to register
60: * @param <T> Serialized type
61: */
62: public <T> void registerSerializer(Class<T> type, ValueSerializer<T> serializer) {
63: Objects.requireNonNull(type);
64: Objects.requireNonNull(serializer);
65: serializers.registerSerializer(type, serializer);
66: }
67:
68: /**
69: * Serializes object graph with the specified root.
70: * <p>
71: * The serialization builds a JSON-LD tree model and then writes it using a {@link JsonGenerator}, which was passed to this instance in
72: * constructor.
73: *
74: * @param root Object graph root
75: */
76: public void serialize(Object root) {
77: Objects.requireNonNull(root);
78: final ObjectGraphTraverser traverser = new ObjectGraphTraverser();
79: traverser.setRequireId(configuration.is(ConfigParam.REQUIRE_ID));
80: final JsonNode jsonRoot = buildJsonTree(root, traverser);
81: jsonRoot.write(jsonGenerator);
82: }
83:
84: /**
85: * Builds the JSON-LD tree model.
86: *
87: * @param root Object graph root
88: * @param graphTraverser Instance capable of traversing the object graph from the specified root
89: * @return {@link JsonNode} corresponding to the JSON-LD's tree root
90: */
91: protected abstract JsonNode buildJsonTree(Object root, ObjectGraphTraverser graphTraverser);
92:
93: public static JsonLdSerializer createCompactedJsonLdSerializer(JsonGenerator jsonWriter) {
94: return new CompactedJsonLdSerializer(jsonWriter);
95: }
96:
97: public static JsonLdSerializer createCompactedJsonLdSerializer(JsonGenerator jsonWriter, Configuration configuration) {
98: return new CompactedJsonLdSerializer(jsonWriter, configuration);
99: }
100: }