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