forked from FraunhoferIOSB/FROST-Server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
JsonWriter.java
109 lines (94 loc) · 4.33 KB
/
JsonWriter.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/*
* Copyright (C) 2021 Fraunhofer Institut IOSB, Fraunhoferstr. 1, D 76131
* Karlsruhe, Germany.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package de.fraunhofer.iosb.ilt.frostserver.json.serialize;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.module.SimpleModule;
import de.fraunhofer.iosb.ilt.frostserver.json.mixin.MixinUtils;
import de.fraunhofer.iosb.ilt.frostserver.model.EntityChangedMessage;
import de.fraunhofer.iosb.ilt.frostserver.model.EntityType;
import de.fraunhofer.iosb.ilt.frostserver.model.core.Entity;
import de.fraunhofer.iosb.ilt.frostserver.model.core.EntitySet;
import de.fraunhofer.iosb.ilt.frostserver.model.ext.EntitySetResult;
import de.fraunhofer.iosb.ilt.frostserver.model.ext.TimeObject;
import de.fraunhofer.iosb.ilt.frostserver.property.Property;
import java.io.IOException;
import java.io.Writer;
import java.time.OffsetDateTime;
import java.util.Date;
import net.time4j.Moment;
/**
* Enables serialization of entities as JSON.
*
* @author jab
* @author scf
*/
public class JsonWriter {
private static ObjectMapper objectMapperInstance;
public static ObjectMapper getObjectMapper() {
if (objectMapperInstance == null) {
initObjectMapper();
}
return objectMapperInstance;
}
private static synchronized void initObjectMapper() {
if (objectMapperInstance == null) {
objectMapperInstance = createObjectMapper();
}
}
private static ObjectMapper createObjectMapper() {
ObjectMapper mapper = new ObjectMapper();
mapper.setSerializationInclusion(JsonInclude.Include.NON_EMPTY);
mapper.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);
mapper.disable(SerializationFeature.FLUSH_AFTER_WRITE_VALUE);
MixinUtils.addMixins(mapper);
SimpleModule module = new SimpleModule();
module.addSerializer(Entity.class, new EntitySerializer());
module.addSerializer(EntityChangedMessage.class, new EntityChangedMessageSerializer());
module.addSerializer(EntitySetResult.class, new EntitySetResultSerializer());
module.addSerializer(TimeObject.class, new TimeObjectSerializer());
module.addSerializer(OffsetDateTime.class, new OffsetDateTimeSerializer());
module.addSerializer(Moment.class, new MomentSerializer());
module.addSerializer(EntityType.class, new EntityTypeSerialiser());
module.addSerializer(Property.class, new EntityPropertySerialiser());
module.addSerializer(Date.class, new DateSerialiser());
mapper.registerModule(module);
return mapper;
}
private JsonWriter() {
}
public static <T extends Entity> void writeEntity(Writer writer, T entity) throws IOException {
getObjectMapper().writeValue(writer, entity);
}
public static <T extends Entity> String writeEntity(T entity) throws IOException {
return getObjectMapper().writeValueAsString(entity);
}
public static void writeEntityCollection(Writer writer, EntitySet entityCollection) throws IOException {
getObjectMapper().writeValue(writer, new EntitySetResult(entityCollection));
}
public static String writeEntityCollection(EntitySet entityCollection) throws IOException {
return getObjectMapper().writeValueAsString(new EntitySetResult(entityCollection));
}
public static void writeObject(Writer writer, Object object) throws IOException {
getObjectMapper().writeValue(writer, object);
}
public static String writeObject(Object object) throws IOException {
return getObjectMapper().writeValueAsString(object);
}
}