msgpack-java icon indicating copy to clipboard operation
msgpack-java copied to clipboard

Convert from org.json.JSONObject.

Open emilekm2142 opened this issue 6 years ago • 3 comments

How can I convert org.json.JSONObject from android to message pack? My object is nested multiple times.

emilekm2142 avatar Feb 20 '18 18:02 emilekm2142

com.fasterxml.jackson.databind.ObjectMapper can serialize org.json.JSONObject. So you can convert org.json.JSONObject instances to MessagePack format like this

JSONObject root = new JSONObject();
    :
ObjectMapper objectMapper = new ObjectMapper(new MessagePackFactory());
byte[] bytes = objectMapper.writeValueAsBytes(root);

// ^^^ `bytes` is MessagePack serialized data.

komamitsu avatar Feb 23 '18 13:02 komamitsu

@komamitsu thank you.

kimathie avatar Apr 29 '21 15:04 kimathie

@komamitsu Hello! Thank you for your answer. It helped! I have only one problem: when try to recreate the initial JSONObject it has two extra nested JSONObjects. Could you help me with an advice?How can I avoid the creation of these objects?

Nicoleta-Nistor avatar Aug 28 '23 14:08 Nicoleta-Nistor