forked from json-iterator/java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDefaultMapKeyDecoder.java
More file actions
39 lines (32 loc) · 1.2 KB
/
DefaultMapKeyDecoder.java
File metadata and controls
39 lines (32 loc) · 1.2 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
package com.jsoniter;
import com.jsoniter.spi.*;
import java.io.IOException;
import java.lang.reflect.Type;
class DefaultMapKeyDecoder implements MapKeyDecoder {
public static MapKeyDecoder registerOrGetExisting(Type mapKeyType) {
String cacheKey = JsoniterSpi.getMapKeyDecoderCacheKey(mapKeyType);
MapKeyDecoder mapKeyDecoder = JsoniterSpi.getMapKeyDecoder(cacheKey);
if (null != mapKeyDecoder) {
return mapKeyDecoder;
}
mapKeyDecoder = new DefaultMapKeyDecoder(TypeLiteral.create(mapKeyType));
JsoniterSpi.addNewMapDecoder(cacheKey, mapKeyDecoder);
return mapKeyDecoder;
}
private final TypeLiteral mapKeyTypeLiteral;
private DefaultMapKeyDecoder(TypeLiteral mapKeyTypeLiteral) {
this.mapKeyTypeLiteral = mapKeyTypeLiteral;
}
@Override
public Object decode(Slice encodedMapKey) {
JsonIterator iter = JsonIteratorPool.borrowJsonIterator();
iter.reset(encodedMapKey);
try {
return iter.read(mapKeyTypeLiteral);
} catch (IOException e) {
throw new JsonException(e);
} finally {
JsonIteratorPool.returnJsonIterator(iter);
}
}
}