Skip to content

Commit 4defb13

Browse files
committed
support create encoder
1 parent 8d62f2d commit 4defb13

3 files changed

Lines changed: 24 additions & 3 deletions

File tree

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.jsoniter.JsonException;
44
import com.jsoniter.spi.Encoder;
5+
import com.jsoniter.spi.Extension;
56
import com.jsoniter.spi.JsoniterSpi;
67
import com.jsoniter.spi.TypeLiteral;
78

@@ -77,6 +78,14 @@ private static synchronized Encoder gen(String cacheKey, Type type) {
7778
if (encoder != null) {
7879
return encoder;
7980
}
81+
List<Extension> extensions = JsoniterSpi.getExtensions();
82+
for (Extension extension : extensions) {
83+
encoder = extension.createEncoder(cacheKey, type);
84+
if (encoder != null) {
85+
JsoniterSpi.addNewEncoder(cacheKey, encoder);
86+
return encoder;
87+
}
88+
}
8089
encoder = CodegenImplNative.NATIVE_ENCODERS.get(type);
8190
if (encoder != null) {
8291
JsoniterSpi.addNewEncoder(cacheKey, encoder);

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,11 @@ public Decoder createDecoder(String cacheKey, Type type) {
2424
return null;
2525
}
2626

27+
@Override
28+
public Encoder createEncoder(String cacheKey, Type type) {
29+
return null;
30+
}
31+
2732
@Override
2833
public void updateClassDescriptor(ClassDescriptor desc) {
2934
}

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

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,16 +30,23 @@ public interface Extension {
3030
/**
3131
* Customize type decoding
3232
*
33-
* @param cacheKey cacheKey parameter
33+
* @param cacheKey name of the decoder
3434
* @param type change how to decode the type
3535
* @return null, if no special customization needed
3636
*/
3737
Decoder createDecoder(String cacheKey, Type type);
3838

39-
// TODO: add createEncoder
39+
/**
40+
* Customize type encoding
41+
*
42+
* @param cacheKey name of the encoder
43+
* @param type change how to encode the type
44+
* @return null, if not special customization needed
45+
*/
46+
Encoder createEncoder(String cacheKey, Type type);
4047

4148
/**
42-
* Update binding is done for the class
49+
* Update how binding is done for the class
4350
*
4451
* @param desc binding information
4552
*/

0 commit comments

Comments
 (0)