Skip to content

Commit 7a09d0c

Browse files
committed
cache config on JsonStream
1 parent 0b4b908 commit 7a09d0c

7 files changed

Lines changed: 64 additions & 19 deletions

File tree

src/main/java/com/jsoniter/annotation/JsoniterConfig.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ public class JsoniterConfig extends EmptyExtension implements Config {
1212
private final String configName;
1313
private final Builder builder;
1414
private volatile Map<Type, String> decoderCacheKeys = new HashMap<Type, String>();
15+
private volatile Map<Type, String> encoderCacheKeys = new HashMap<Type, String>();
1516

1617
public JsoniterConfig(Builder builder) {
1718
this.configName = JsoniterSpi.assignConfigName(builder);
@@ -37,6 +38,26 @@ public String getDecoderCacheKey(Type type) {
3738
cacheKey = TypeLiteral.create(type).getDecoderCacheKey(configName);
3839
HashMap<Type, String> newCache = new HashMap<Type, String>(decoderCacheKeys);
3940
newCache.put(type, cacheKey);
41+
decoderCacheKeys = newCache;
42+
return cacheKey;
43+
}
44+
}
45+
46+
@Override
47+
public String getEncoderCacheKey(Type type) {
48+
String cacheKey = encoderCacheKeys.get(type);
49+
if (cacheKey != null) {
50+
return cacheKey;
51+
}
52+
synchronized(this) {
53+
cacheKey = encoderCacheKeys.get(type);
54+
if (cacheKey != null) {
55+
return cacheKey;
56+
}
57+
cacheKey = TypeLiteral.create(type).getEncoderCacheKey(configName);
58+
HashMap<Type, String> newCache = new HashMap<Type, String>(encoderCacheKeys);
59+
newCache.put(type, cacheKey);
60+
encoderCacheKeys = newCache;
4061
return cacheKey;
4162
}
4263
}

src/main/java/com/jsoniter/output/JsonStream.java

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
public class JsonStream extends OutputStream {
1010

11+
public Config configCache;
1112
public static int defaultIndentionStep = 0;
1213
public int indentionStep = defaultIndentionStep;
1314
private int indention = 0;
@@ -322,18 +323,27 @@ public final void writeVal(Object obj) throws IOException {
322323
return;
323324
}
324325
Class<?> clazz = obj.getClass();
325-
String cacheKey = TypeLiteral.create(clazz).getEncoderCacheKey();
326+
String cacheKey = currentConfig().getEncoderCacheKey(clazz);
326327
Codegen.getEncoder(cacheKey, clazz).encode(obj, this);
327328
}
328329

329330
public final <T> void writeVal(TypeLiteral<T> typeLiteral, T obj) throws IOException {
330331
if (null == obj) {
331332
writeNull();
332333
} else {
333-
Codegen.getEncoder(typeLiteral.getEncoderCacheKey(), typeLiteral.getType()).encode(obj, this);
334+
String cacheKey = currentConfig().getEncoderCacheKey(typeLiteral.getType());
335+
Codegen.getEncoder(cacheKey, typeLiteral.getType()).encode(obj, this);
334336
}
335337
}
336338

339+
public Config currentConfig() {
340+
if (configCache != null) {
341+
return configCache;
342+
}
343+
configCache = JsoniterSpi.getCurrentConfig();
344+
return configCache;
345+
}
346+
337347
public static void serialize(Config config, Object obj, OutputStream out) {
338348
JsoniterSpi.setCurrentConfig(config);
339349
try {

src/main/java/com/jsoniter/output/JsonStreamPool.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,32 @@
11
package com.jsoniter.output;
22

3+
import com.jsoniter.spi.Config;
4+
import com.jsoniter.spi.ConfigListener;
5+
import com.jsoniter.spi.JsoniterSpi;
6+
37
public class JsonStreamPool {
48

59
private final static ThreadLocal<JsonStream> slot1 = new ThreadLocal<JsonStream>();
610
private final static ThreadLocal<JsonStream> slot2 = new ThreadLocal<JsonStream>();
711
private final static ThreadLocal<AsciiOutputStream> osSlot1 = new ThreadLocal<AsciiOutputStream>();
812
private final static ThreadLocal<AsciiOutputStream> osSlot2 = new ThreadLocal<AsciiOutputStream>();
913

14+
static {
15+
JsoniterSpi.registerConfigListener(new ConfigListener() {
16+
@Override
17+
public void onCurrentConfigChanged(Config newConfig) {
18+
JsonStream stream = slot1.get();
19+
if (stream != null) {
20+
stream.configCache = newConfig;
21+
}
22+
stream = slot2.get();
23+
if (stream != null) {
24+
stream.configCache = newConfig;
25+
}
26+
}
27+
});
28+
}
29+
1030
public static JsonStream borrowJsonStream() {
1131
JsonStream stream = slot1.get();
1232
if (stream != null) {

src/main/java/com/jsoniter/spi/Config.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@
55
public interface Config extends Extension {
66
String configName();
77
String getDecoderCacheKey(Type type);
8+
String getEncoderCacheKey(Type type);
89
}

src/test/java/com/jsoniter/TestSpiTypeDecoder.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,12 @@
1212

1313
public class TestSpiTypeDecoder extends TestCase {
1414

15+
static {
16+
// JsonIterator.setMode(DecodingMode.DYNAMIC_MODE_AND_MATCH_FIELD_WITH_HASH);
17+
}
18+
1519
public static class TestObject1 {
1620
public int field1;
17-
18-
private TestObject1() {
19-
}
2021
}
2122

2223
public void test_TypeDecoder() throws IOException {

src/test/java/com/jsoniter/output/TestAnnotationJsonProperty.java

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,12 @@
44
import com.jsoniter.spi.Encoder;
55
import junit.framework.TestCase;
66

7-
import java.io.ByteArrayOutputStream;
87
import java.io.IOException;
98

109
public class TestAnnotationJsonProperty extends TestCase {
1110

12-
private ByteArrayOutputStream baos;
13-
private JsonStream stream;
14-
15-
public void setUp() {
16-
baos = new ByteArrayOutputStream();
17-
stream = new JsonStream(baos, 4096);
11+
static {
12+
// JsonStream.setMode(EncodingMode.DYNAMIC_MODE);
1813
}
1914

2015
public static class TestObject1 {
@@ -25,9 +20,8 @@ public static class TestObject1 {
2520
public void test_property() throws IOException {
2621
TestObject1 obj = new TestObject1();
2722
obj.field1 = "hello";
28-
stream.writeVal(obj);
29-
stream.close();
30-
assertEquals("{\"field-1\":\"hello\"}", baos.toString());
23+
String output = JsonStream.serialize(obj);
24+
assertEquals("{\"field-1\":\"hello\"}", output);
3125
}
3226

3327

@@ -39,8 +33,7 @@ public static class TestObject2 {
3933
public void test_encoder() throws IOException {
4034
TestObject2 obj = new TestObject2();
4135
obj.field1 = 100;
42-
stream.writeVal(obj);
43-
stream.close();
44-
assertEquals("{\"field1\":\"100\"}", baos.toString());
36+
String output = JsonStream.serialize(obj);
37+
assertEquals("{\"field1\":\"100\"}", output);
4538
}
4639
}

src/test/java/com/jsoniter/output/TestGson.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import com.google.gson.annotations.Expose;
66
import com.google.gson.annotations.SerializedName;
77
import com.jsoniter.extra.GsonCompatibilityMode;
8-
import com.jsoniter.spi.JsoniterSpi;
98
import junit.framework.TestCase;
109

1110
public class TestGson extends TestCase {

0 commit comments

Comments
 (0)