Skip to contentMethod: lambda$serializeField$0(CollectionNode, Object)
1: /**
2: * Copyright (C) 2017 Czech Technical University in Prague
3: *
4: * This program is free software: you can redistribute it and/or modify it under
5: * the terms of the GNU General Public License as published by the Free Software
6: * Foundation, either version 3 of the License, or (at your option) any
7: * later version.
8: *
9: * This program is distributed in the hope that it will be useful, but WITHOUT
10: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11: * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
12: * details. You should have received a copy of the GNU General Public License
13: * along with this program. If not, see <http://www.gnu.org/licenses/>.
14: */
15: package cz.cvut.kbss.jsonld.serialization;
16:
17: import cz.cvut.kbss.jsonld.common.BeanAnnotationProcessor;
18: import cz.cvut.kbss.jsonld.serialization.model.CollectionNode;
19: import cz.cvut.kbss.jsonld.serialization.model.JsonNode;
20:
21: import java.lang.reflect.Field;
22: import java.util.Collection;
23: import java.util.Collections;
24: import java.util.List;
25:
26: class LiteralFieldSerializer implements FieldSerializer {
27:
28: @Override
29: public List<JsonNode> serializeField(Field field, Object value) {
30: final String attName = BeanAnnotationProcessor.getAttributeIdentifier(field);
31: final JsonNode result;
32: if (value instanceof Collection) {
33: final Collection<?> col = (Collection<?>) value;
34: final CollectionNode node = JsonNodeFactory.createCollectionNode(attName, col);
35: col.forEach(obj -> node.addItem(JsonNodeFactory.createLiteralNode(obj)));
36: result = node;
37: } else {
38: result = JsonNodeFactory.createLiteralNode(attName, value);
39: }
40: return Collections.singletonList(result);
41: }
42: }