-
Notifications
You must be signed in to change notification settings - Fork 588
Expand file tree
/
Copy pathJacksonObjectMapper.java
More file actions
161 lines (142 loc) · 5.54 KB
/
JacksonObjectMapper.java
File metadata and controls
161 lines (142 loc) · 5.54 KB
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/**
* The MIT License
*
* Copyright for portions of unirest-java are held by Kong Inc (c) 2013.
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package BehaviorTests;
import tools.jackson.core.JacksonException;
import tools.jackson.core.JsonGenerator;
import tools.jackson.core.JsonParser;
import tools.jackson.core.StreamWriteFeature;
import tools.jackson.databind.*;
import tools.jackson.databind.json.JsonMapper;
import tools.jackson.databind.module.SimpleModule;
import tools.jackson.datatype.guava.GuavaModule;
import kong.unirest.core.*;
import kong.unirest.core.json.JSONObject;
import java.io.File;
import java.io.InputStream;
public class JacksonObjectMapper implements kong.unirest.core.ObjectMapper {
public tools.jackson.databind.ObjectMapper om;
public JacksonObjectMapper(tools.jackson.databind.ObjectMapper om){
this.om = om;
}
public JacksonObjectMapper(){
JsonMapper.Builder builder = JsonMapper.builderWithJackson2Defaults();
builder.addModule(new GuavaModule());
SimpleModule simpleModule = new SimpleModule();
simpleModule.addSerializer(JsonPatchItem.class, new PatchSerializer());
simpleModule.addSerializer(JsonPatch.class, new JsonPatchSerializer());
simpleModule.addDeserializer(JsonPatchItem.class, new PatchDeserializer());
simpleModule.addDeserializer(JsonPatch.class, new JsonPatchDeSerializer());
simpleModule.addSerializer(HttpMethod.class, new HttpMethodSerializer());
simpleModule.addDeserializer(HttpMethod.class, new HttpMethodDeSerializer());
builder.configure(StreamWriteFeature.IGNORE_UNKNOWN, true);
builder.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
builder.addModule(simpleModule);
this.om = builder.build();
}
public <T> T readValue(File f, Class<T> valueType){
try {
return om.readValue(f, valueType);
} catch (JacksonException e) {
throw new RuntimeException(e);
}
}
@Override
public <T> T readValue(String value, Class<T> valueType) {
try {
return om.readValue(value, valueType);
} catch (JacksonException e) {
throw new UnirestException(e);
}
}
@Override
public <T> T readValue(String value, GenericType<T> genericType) {
try {
return om.readValue(value, om.constructType(genericType.getType()));
} catch (JacksonException e) {
throw new UnirestException(e);
}
}
@Override
public String writeValue(Object value) {
try {
return om.writeValueAsString(value);
} catch (JacksonException e) {
throw new UnirestException(e);
}
}
public <T> T readValue(InputStream rawBody, Class<T> as) {
try {
return om.readValue(rawBody, as);
} catch (JacksonException e) {
throw new RuntimeException(e);
}
}
public <T> T readValue(byte[] content, Class<T> clazz) {
try {
return om.readValue(content, clazz);
} catch (JacksonException e) {
throw new RuntimeException(e);
}
}
public static class JsonPatchSerializer extends ValueSerializer<JsonPatch>{
@Override
public void serialize(JsonPatch patch, JsonGenerator jgen, SerializationContext ctxt) throws JacksonException {
jgen.writeRawValue(patch.toString());
}
}
public static class JsonPatchDeSerializer extends ValueDeserializer<JsonPatch>{
@Override
public JsonPatch deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) {
String s = jsonParser.readValueAsTree().toString();
return new JsonPatch(s);
}
}
public static class PatchSerializer extends ValueSerializer<JsonPatchItem> {
@Override
public void serialize(JsonPatchItem jwk, JsonGenerator jgen, SerializationContext ctxt) throws JacksonException {
jgen.writeRaw(jwk.toString());
}
}
public static class PatchDeserializer extends ValueDeserializer<JsonPatchItem> {
@Override
public JsonPatchItem deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) {
String s = jsonParser.readValueAsTree().toString();
return new JsonPatchItem(new JSONObject(s));
}
}
public static class HttpMethodSerializer extends ValueSerializer<HttpMethod> {
@Override
public void serialize(HttpMethod httpMethod, JsonGenerator jgen, SerializationContext ctxt) throws JacksonException {
jgen.writeString(httpMethod.name());
}
}
public static class HttpMethodDeSerializer extends ValueDeserializer<HttpMethod> {
@Override
public HttpMethod deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) {
String s = jsonParser.readValueAs(String.class);
return HttpMethod.valueOf(s);
}
}
}