You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Android: How to convert message pack binary data into readable JSON string? #434
I already have JSON parsing module and works fine. Recently my back end team added support for message pack. So I am planning to convert the message pack data into JSON string so that I can keep the existing parsing logic.
changed the title [-]How to convert message back bytes into JSON string?[/-][+]Android: How to convert message pack binary data into readable JSON string?[/+]on Oct 11, 2017
I have no idea about what problem you're facing now.
You've already unpacked the MessagePack format byte array in Java that was packed by PHP and you're struggling to convert the Java object to JSON?
I am struck in #1. The POJO java object is dynamic so I can't map it to a static POJO class. I just want a JSON string from MessagePack byte array. Please provide me some code snippet.
If the schema of the object is dynamic, you'd better handle it as java.util.Map or org.msgpack.value.MapValue.
This is an example snippet although you may get the way from the usage I mentioned above
Finally I fixed it using the below snippet. Thanks #komamitsu for your time and let me try your code snippet as well.
MessageUnpacker unpacker = null;
try {
byte[] formattedBytes = Arrays.copyOfRange(responseBytes, 4, responseBytes.length); /* This line saved my life. I had to remove new line characters because my backend was always appending 4 junk new line bytes with the MessagePack byte array. */
unpacker = new MessagePack.UnpackerConfig()
.withStringDecoderBufferSize(16 * 1024) // If your data contains many large strings (the default is 8k)
.newUnpacker(formattedBytes);
ObjectMapper mapper = new ObjectMapper(new MessagePackFactory());
try {
JsonNode jsonNode = mapper.readTree(formattedBytes);
String responseJson = jsonNode.toString();
Log.i(TAG, "Response Json" + responseJson);
} catch (IOException e) {
Log.i(TAG, "Exception occurred" + e.getStackTrace());
}
} catch (Exception e) {
e.printStackTrace();
}
Activity
[-]How to convert message back bytes into JSON string?[/-][+]Android: How to convert message pack binary data into readable JSON string?[/+]iraj-wisilica commentedon Oct 16, 2017
Gentle reminder.
komamitsu commentedon Oct 16, 2017
These examples might help https://github.com/msgpack/msgpack-java/tree/develop/msgpack-jackson#usage
I have no idea about what problem you're facing now.
You've already unpacked the MessagePack format byte array in Java that was packed by PHP and you're struggling to convert the Java object to JSON?
iraj-wisilica commentedon Oct 17, 2017
Thanks for the reply. I am struggling to convert MessagePack byte array into JSON.
komamitsu commentedon Oct 17, 2017
You can't convert MessagePack byte array into JSON directly. The following steps are needed.
So I was interested in which step you're facing now.
iraj-wisilica commentedon Oct 17, 2017
I am struck in #1. The POJO java object is dynamic so I can't map it to a static POJO class. I just want a JSON string from MessagePack byte array. Please provide me some code snippet.
komamitsu commentedon Oct 17, 2017
If the schema of the object is dynamic, you'd better handle it as java.util.Map or org.msgpack.value.MapValue.
This is an example snippet although you may get the way from the usage I mentioned above
Ruby
Java
iraj-wisilica commentedon Oct 17, 2017
Finally I fixed it using the below snippet. Thanks #komamitsu for your time and let me try your code snippet as well.
komamitsu commentedon Oct 17, 2017
🎉
Yeah, using JsonNode would be smarter.
iraj-wisilica commentedon Oct 17, 2017
Thank You.