json-iterator / java

jsoniter (json-iterator) is fast and flexible JSON parser available in Java and Go
http://jsoniter.com/
MIT License
1.51k stars 519 forks source link

Using different Decoder / Encoder of same type at a same time #310

Closed ndhcoder closed 2 years ago

ndhcoder commented 2 years ago

Hello everyone,

Can I using different Decoder / Encoder of same type at a time ?

Example: I have a API for export/import a data of class X: { "name": "ndhcoder", "age": 18 }

I need optimize storage so I'm using my customize decoder / encoder for minify field name of data to { "a": "ndhcoder", "b": 18 }

But in others API, X must be in a normal format: { "name": "ndhcoder", "age": 18 }

If I register decoder / encoder type at start time, I can't do both features. If I register decoder / encoder at the start time of each API, seem very confuse on multithreading, How do I resolve this problem ? Thanks all

svobol13 commented 2 years ago

@ndhcoder Hi, can you please share what was the resolution of the issue?

ndhcoder commented 2 years ago

@ndhcoder Hi, can you please share what was the resolution of the issue?

JsonStream cache the Config object that extends EmptyExtension class (I will implement method createEncoder / Decoder for this config if I need my customize encoder/decoder) inside a ThreadLocal, So I just need create an Config and then pass it into JsonStream.seralize(config, object). By default, on each thread, JsonStream using same instance Config if not specified, so If I create one then other thread not side effect.