Skip to content

Package: MultilingualStringSerializer

MultilingualStringSerializer

nameinstructionbranchcomplexitylinemethod
MultilingualStringSerializer()
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%
addTranslationsToCollectionNode(MultilingualString, SetNode)
M: 0 C: 6
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
createNode(String, String, String)
M: 0 C: 19
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 4
100%
M: 0 C: 1
100%
lambda$addTranslationsToCollectionNode$0(SetNode, String, String)
M: 0 C: 7
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
serialize(MultilingualString, SerializationContext)
M: 0 C: 32
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 6
100%
M: 0 C: 1
100%

Coverage

1: /*
2: * JB4JSON-LD
3: * Copyright (C) 2024 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.compact;
19:
20: import cz.cvut.kbss.jopa.model.MultilingualString;
21: import cz.cvut.kbss.jsonld.JsonLd;
22: import cz.cvut.kbss.jsonld.serialization.JsonNodeFactory;
23: import cz.cvut.kbss.jsonld.serialization.model.JsonNode;
24: import cz.cvut.kbss.jsonld.serialization.model.ObjectNode;
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.util.Map;
30:
31: /**
32: * This is used to serialize {@link MultilingualString} values.
33: */
34: public class MultilingualStringSerializer implements ValueSerializer<MultilingualString> {
35:
36: @Override
37: public JsonNode serialize(MultilingualString value, SerializationContext<MultilingualString> ctx) {
38:• if (value.getValue().size() == 1) {
39: final Map.Entry<String, String> entry = value.getValue().entrySet().iterator().next();
40: return createNode(ctx.getTerm(), entry.getValue(), entry.getKey());
41: }
42: final SetNode collectionNode = JsonNodeFactory.createCollectionNodeFromArray(ctx.getTerm());
43: addTranslationsToCollectionNode(value, collectionNode);
44: return collectionNode;
45: }
46:
47: private static JsonNode createNode(String attName, String value, String language) {
48: final ObjectNode node = JsonNodeFactory.createObjectNode(attName);
49:• node.addItem(JsonNodeFactory.createLiteralNode(JsonLd.LANGUAGE, language != null ? language : JsonLd.NONE));
50: node.addItem(JsonNodeFactory.createLiteralNode(JsonLd.VALUE, value));
51: return node;
52: }
53:
54: private void addTranslationsToCollectionNode(MultilingualString str, SetNode target) {
55: str.getValue().forEach((lang, val) -> target.addItem(createNode(null, val, lang)));
56: }
57: }