Package: JsonLdSerializer
JsonLdSerializer
name | instruction | branch | complexity | line | method | ||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
JsonLdSerializer(JsonGenerator) |
|
|
|
|
|
||||||||||||||||||||
JsonLdSerializer(JsonGenerator, Configuration) |
|
|
|
|
|
||||||||||||||||||||
configuration() |
|
|
|
|
|
||||||||||||||||||||
createCompactedJsonLdSerializer(JsonGenerator) |
|
|
|
|
|
||||||||||||||||||||
createCompactedJsonLdSerializer(JsonGenerator, Configuration) |
|
|
|
|
|
||||||||||||||||||||
createContextBuildingJsonLdSerializer(JsonGenerator) |
|
|
|
|
|
||||||||||||||||||||
createContextBuildingJsonLdSerializer(JsonGenerator, Configuration) |
|
|
|
|
|
||||||||||||||||||||
registerSerializer(Class, ValueSerializer) |
|
|
|
|
|
||||||||||||||||||||
serialize(Object) |
|
|
|
|
|
Coverage
1: /**
2: * Copyright (C) 2022 Czech Technical University in Prague
3: * <p>
4: * This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public
5: * License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later
6: * version.
7: * <p>
8: * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
9: * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
10: * details. You should have received a copy of the GNU General Public License along with this program. If not, see
11: * <http://www.gnu.org/licenses/>.
12: */
13: package cz.cvut.kbss.jsonld.serialization;
14:
15: import cz.cvut.kbss.jsonld.Configuration;
16: import cz.cvut.kbss.jsonld.common.Configured;
17: import cz.cvut.kbss.jsonld.serialization.model.JsonNode;
18: import cz.cvut.kbss.jsonld.serialization.serializer.ValueSerializer;
19: import cz.cvut.kbss.jsonld.serialization.serializer.ValueSerializers;
20:
21: import java.util.Objects;
22:
23: /**
24: * Base class for all JSON-LD serializers.
25: * <p>
26: * The serializers will mostly differ in the form of the generated JSON. E.g. the output can be expanded, using contexts etc.
27: */
28: public abstract class JsonLdSerializer implements Configured {
29:
30: private final Configuration configuration;
31:
32: protected final JsonGenerator jsonGenerator;
33:
34: protected final ValueSerializers serializers;
35:
36: protected JsonLdSerializer(JsonGenerator jsonGenerator) {
37: this.jsonGenerator = Objects.requireNonNull(jsonGenerator);
38: this.configuration = new Configuration();
39: this.serializers = initSerializers();
40: }
41:
42: public JsonLdSerializer(JsonGenerator jsonGenerator, Configuration configuration) {
43: this.jsonGenerator = Objects.requireNonNull(jsonGenerator);
44: this.configuration = Objects.requireNonNull(configuration);
45: this.serializers = initSerializers();
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: serializers.configure(configuration);
79: final JsonNode jsonRoot = buildJsonTree(root);
80: jsonRoot.write(jsonGenerator);
81: }
82:
83: protected abstract ValueSerializers initSerializers();
84:
85: /**
86: * Builds the JSON-LD tree model.
87: *
88: * @param root Object graph root
89: * @return {@link JsonNode} corresponding to the JSON-LD's tree root
90: */
91: protected abstract JsonNode buildJsonTree(Object root);
92:
93: public static JsonLdSerializer createCompactedJsonLdSerializer(JsonGenerator jsonWriter) {
94: return new CompactedJsonLdSerializer(jsonWriter);
95: }
96:
97: public static JsonLdSerializer createCompactedJsonLdSerializer(JsonGenerator jsonWriter,
98: Configuration configuration) {
99: return new CompactedJsonLdSerializer(jsonWriter, configuration);
100: }
101:
102: public static JsonLdSerializer createContextBuildingJsonLdSerializer(JsonGenerator jsonWriter) {
103: return new ContextBuildingJsonLdSerializer(jsonWriter);
104: }
105:
106: public static JsonLdSerializer createContextBuildingJsonLdSerializer(JsonGenerator jsonWriter,
107: Configuration configuration) {
108: return new ContextBuildingJsonLdSerializer(jsonWriter, configuration);
109: }
110: }