forked from json-iterator/java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReflectionEnumDecoder.java
More file actions
33 lines (28 loc) · 912 Bytes
/
ReflectionEnumDecoder.java
File metadata and controls
33 lines (28 loc) · 912 Bytes
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
package com.jsoniter;
import com.jsoniter.spi.Decoder;
import com.jsoniter.spi.Slice;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
class ReflectionEnumDecoder implements Decoder{
private final Map<Slice, Object> enumMap = new HashMap<Slice, Object>();
private Class clazz;
public ReflectionEnumDecoder(Class clazz) {
this.clazz = clazz;
for (Object e : clazz.getEnumConstants()) {
enumMap.put(Slice.make(e.toString()), e);
}
}
@Override
public Object decode(JsonIterator iter) throws IOException {
if (iter.readNull()) {
return null;
}
Slice slice = IterImpl.readSlice(iter);
Object e = enumMap.get(slice);
if (e == null) {
throw iter.reportError("ReflectionEnumDecoder", slice + " is not valid enum for " + clazz);
}
return e;
}
}