-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJacksonEntryCustomizer.java
72 lines (61 loc) · 2.48 KB
/
JacksonEntryCustomizer.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
package com.cakoose.json_tuple_databinding_examples;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.JsonToken;
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.exc.InvalidFormatException;
import com.fasterxml.jackson.databind.module.SimpleModule;
import java.io.IOException;
/**
* Jackson serialization and deserialization code to represent {@link Entry} using
* a JSON array instead of a JSON object.
*
* See {@link #configure} to see how to make Jackson use our custom serializer and
* deserializer.
*/
public class JacksonEntryCustomizer
{
/**
* Configures the given {@code }ObjectMapper} to use our custom serializer and
* deserializer. Alternatively, you can use the Jackson {@code JsonSerialize}
* and {@code JsonDeserialize} annotations on the class (see: {@code Entry}).
*/
public static void configure(ObjectMapper mapper)
{
SimpleModule module = new SimpleModule();
module.addSerializer(Entry.class, new Serializer());
module.addDeserializer(Entry.class, new Deserializer());
mapper.registerModule(module);
}
public static class Serializer extends JsonSerializer<Entry>
{
@Override
public void serialize(Entry value, JsonGenerator jgen, SerializerProvider provider) throws IOException, JsonProcessingException
{
jgen.writeStartArray();
jgen.writeString(value.name);
jgen.writeObject(value.info);
jgen.writeEndArray();
}
}
public static class Deserializer extends JsonDeserializer<Entry>
{
public Entry deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException
{
JsonToken t;
t = jp.getCurrentToken();
if (t != JsonToken.START_ARRAY) {
throw new InvalidFormatException("expecting JSON array, got " + t, jp.getCurrentLocation(), t, Entry.class);
}
jp.nextToken();
String path = jp.readValueAs(String.class);
Metadata info = jp.readValueAs(Metadata.class);
t = jp.nextToken();
if (t != JsonToken.END_ARRAY) {
throw new InvalidFormatException("expecting only two elements in array", jp.getCurrentLocation(), t, Entry.class);
}
return new Entry(path, info);
}
}
}