Skip to content

Method: serialize(MultilingualString)

1: package cz.cvut.kbss.jsonld.serialization;
2:
3: import cz.cvut.kbss.jopa.model.MultilingualString;
4: import cz.cvut.kbss.jsonld.serialization.model.CollectionNode;
5: import cz.cvut.kbss.jsonld.serialization.model.JsonNode;
6:
7: import java.util.Map;
8:
9: /**
10: * This use used to serialize {@link MultilingualString} values.
11: */
12: class MultilingualStringSerializer {
13:
14: JsonNode serialize(String attName, MultilingualString value) {
15: assert value != null;
16: if (value.getValue().size() == 1) {
17: final Map.Entry<String, String> entry = value.getValue().entrySet().iterator().next();
18: return JsonNodeFactory.createLangStringNode(attName, entry.getValue(), entry.getKey());
19: }
20: final CollectionNode collectionNode = JsonNodeFactory.createCollectionNodeFromArray(attName);
21: addTranslationsToCollectionNode(value, collectionNode);
22: return collectionNode;
23: }
24:
25: private void addTranslationsToCollectionNode(MultilingualString str, CollectionNode target) {
26: str.getValue()
27: .forEach((lang, val) -> target.addItem(JsonNodeFactory.createLangStringNode(val, lang)));
28: }
29:
30: JsonNode serialize(MultilingualString value) {
31:• assert value != null;
32:• if (value.getValue().size() == 1) {
33: final Map.Entry<String, String> entry = value.getValue().entrySet().iterator().next();
34: return JsonNodeFactory.createLangStringNode(entry.getValue(), entry.getKey());
35: }
36: final CollectionNode collectionNode = JsonNodeFactory.createCollectionNodeFromArray();
37: addTranslationsToCollectionNode(value, collectionNode);
38: return collectionNode;
39: }
40: }