Skip to content

Package: JsonLdSerializer

JsonLdSerializer

nameinstructionbranchcomplexitylinemethod
JsonLdSerializer(JsonGenerator)
M: 0 C: 17
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 5
100%
M: 0 C: 1
100%
JsonLdSerializer(JsonGenerator, Configuration)
M: 17 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 5 C: 0
0%
M: 1 C: 0
0%
configuration()
M: 0 C: 3
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
createCompactedJsonLdSerializer(JsonGenerator)
M: 0 C: 5
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
createCompactedJsonLdSerializer(JsonGenerator, Configuration)
M: 6 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
createContextBuildingJsonLdSerializer(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%
createContextBuildingJsonLdSerializer(JsonGenerator, Configuration)
M: 6 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
registerSerializer(Class, ValueSerializer)
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%
serialize(Object)
M: 0 C: 17
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 5
100%
M: 0 C: 1
100%

Coverage

1: /*
2: * JB4JSON-LD
3: * Copyright (C) 2023 Czech Technical University in Prague
4: *
5: * This library is free software; you can redistribute it and/or
6: * modify it under the terms of the GNU Lesser General Public
7: * License as published by the Free Software Foundation; either
8: * version 3.0 of the License, or (at your option) any later version.
9: *
10: * This library is distributed in the hope that it will be useful,
11: * but WITHOUT ANY WARRANTY; without even the implied warranty of
12: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13: * Lesser General Public License for more details.
14: *
15: * You should have received a copy of the GNU Lesser General Public
16: * License along with this library.
17: */
18: package cz.cvut.kbss.jsonld.serialization;
19:
20: import cz.cvut.kbss.jsonld.Configuration;
21: import cz.cvut.kbss.jsonld.common.Configured;
22: import cz.cvut.kbss.jsonld.serialization.model.JsonNode;
23: import cz.cvut.kbss.jsonld.serialization.serializer.ValueSerializer;
24: import cz.cvut.kbss.jsonld.serialization.serializer.ValueSerializers;
25:
26: import java.util.Objects;
27:
28: /**
29: * Base class for all JSON-LD serializers.
30: * <p>
31: * The serializers will mostly differ in the form of the generated JSON. E.g. the output can be expanded, using contexts etc.
32: */
33: public abstract class JsonLdSerializer implements Configured {
34:
35: private final Configuration configuration;
36:
37: protected final JsonGenerator jsonGenerator;
38:
39: protected final ValueSerializers serializers;
40:
41: protected JsonLdSerializer(JsonGenerator jsonGenerator) {
42: this.jsonGenerator = Objects.requireNonNull(jsonGenerator);
43: this.configuration = new Configuration();
44: this.serializers = initSerializers();
45: }
46:
47: public JsonLdSerializer(JsonGenerator jsonGenerator, Configuration configuration) {
48: this.jsonGenerator = Objects.requireNonNull(jsonGenerator);
49: this.configuration = Objects.requireNonNull(configuration);
50: this.serializers = initSerializers();
51: }
52:
53: @Override
54: public Configuration configuration() {
55: return configuration;
56: }
57:
58: /**
59: * Registers a custom serializer for the specified type.
60: * <p>
61: * If a serializer already existed for the type, it is replaced by the new one.
62: *
63: * @param type Type to register the serializer for
64: * @param serializer Serializer to register
65: * @param <T> Serialized type
66: */
67: public <T> void registerSerializer(Class<T> type, ValueSerializer<T> serializer) {
68: Objects.requireNonNull(type);
69: Objects.requireNonNull(serializer);
70: serializers.registerSerializer(type, serializer);
71: }
72:
73: /**
74: * Serializes object graph with the specified root.
75: * <p>
76: * The serialization builds a JSON-LD tree model and then writes it using a {@link JsonGenerator}, which was passed to this instance in
77: * constructor.
78: *
79: * @param root Object graph root
80: */
81: public void serialize(Object root) {
82: Objects.requireNonNull(root);
83: serializers.configure(configuration);
84: final JsonNode jsonRoot = buildJsonTree(root);
85: jsonRoot.write(jsonGenerator);
86: }
87:
88: protected abstract ValueSerializers initSerializers();
89:
90: /**
91: * Builds the JSON-LD tree model.
92: *
93: * @param root Object graph root
94: * @return {@link JsonNode} corresponding to the JSON-LD's tree root
95: */
96: protected abstract JsonNode buildJsonTree(Object root);
97:
98: public static JsonLdSerializer createCompactedJsonLdSerializer(JsonGenerator jsonWriter) {
99: return new CompactedJsonLdSerializer(jsonWriter);
100: }
101:
102: public static JsonLdSerializer createCompactedJsonLdSerializer(JsonGenerator jsonWriter,
103: Configuration configuration) {
104: return new CompactedJsonLdSerializer(jsonWriter, configuration);
105: }
106:
107: public static JsonLdSerializer createContextBuildingJsonLdSerializer(JsonGenerator jsonWriter) {
108: return new ContextBuildingJsonLdSerializer(jsonWriter);
109: }
110:
111: public static JsonLdSerializer createContextBuildingJsonLdSerializer(JsonGenerator jsonWriter,
112: Configuration configuration) {
113: return new ContextBuildingJsonLdSerializer(jsonWriter, configuration);
114: }
115: }