Skip to contentMethod: ContextBuildingTypesSerializer.ContextBasedTypesNode(SerializationContext)
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.serializer.context;
19:
20: import cz.cvut.kbss.jsonld.JsonLd;
21: import cz.cvut.kbss.jsonld.serialization.JsonGenerator;
22: import cz.cvut.kbss.jsonld.serialization.JsonNodeFactory;
23: import cz.cvut.kbss.jsonld.serialization.model.CollectionNode;
24: import cz.cvut.kbss.jsonld.serialization.model.JsonNode;
25: import cz.cvut.kbss.jsonld.serialization.model.SetNode;
26: import cz.cvut.kbss.jsonld.serialization.serializer.ValueSerializer;
27: import cz.cvut.kbss.jsonld.serialization.traversal.SerializationContext;
28:
29: import java.io.IOException;
30: import java.util.Optional;
31: import java.util.Set;
32:
33: public class ContextBuildingTypesSerializer implements ValueSerializer<Set<String>> {
34:
35: @Override
36: public JsonNode serialize(Set<String> value, SerializationContext<Set<String>> ctx) {
37: final CollectionNode<?> typesNode;
38: if (ctx.getField() != null) {
39: ctx.registerTermMapping(ctx.getFieldName(), JsonLd.TYPE);
40: typesNode = JsonNodeFactory.createSetNode(ctx.getTerm());
41: } else {
42: typesNode = new ContextBasedTypesNode(ctx);
43: }
44: value.forEach(type -> typesNode.addItem(JsonNodeFactory.createLiteralNode(type)));
45: return typesNode;
46: }
47:
48: /**
49: * JSON node representing types ({@link JsonLd#TYPE}).
50: * <p>
51: * Allows overriding the default JSON attribute when a term mapping for types is provided deeper in the object
52: * graph. For example, when an instance without a {@link cz.cvut.kbss.jopa.model.annotations.Types} is serialized,
53: * its type would be serialized in the {@link JsonLd#TYPE} attribute. But if it also references an object with a
54: * {@link cz.cvut.kbss.jopa.model.annotations.Types} field, the field name will be used for term mapping in the
55: * context. This new name is applied to the types attribute representing the parent's type.
56: */
57: private static class ContextBasedTypesNode extends SetNode {
58:
59: private final SerializationContext<Set<String>> ctx;
60:
61: private ContextBasedTypesNode(SerializationContext<Set<String>> ctx) {
62: super(JsonLd.TYPE);
63: this.ctx = ctx;
64: }
65:
66: @Override
67: protected void writeKey(JsonGenerator writer) throws IOException {
68: final Optional<String> typesTerm = ctx.getMappedTerm(JsonLd.TYPE);
69: if (typesTerm.isPresent()) {
70: writer.writeFieldName(typesTerm.get());
71: } else {
72: super.writeKey(writer);
73: }
74: }
75: }
76: }