Skip to content

Android: How to convert message pack binary data into readable JSON string? #434

Closed

Description

@iraj-wisilica

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.

Message Pack response from PHP back end.

response

The unpacker produces the following output.

Unpack: -17
Unpack: ��Response��Status��a
Unpack: 112
Unpack: 105
Unpack: 73
Unpack: 100
Unpack: 16
Unpack: -17
Unpack: �statusCode�N!�statusMessag
Unpack: 101
Unpack: -17
Unpack: �device get api is success�Da
Unpack: 116
Unpack: 97
Unpack: -17
Unpack: ��deviceCount�0�deviceDet
Unpack: 97
Unpack: 105
Unpack: 108
Unpack: 115
Unpack: -17
Unpack: ���message�No data found�

Activity

  1. 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
  2. iraj-wisilica commented on Oct 16, 2017

    @iraj-wisilica
    Author

    Gentle reminder.

  3. komamitsu commented on Oct 16, 2017

    @komamitsu
    Member

    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?

  4. iraj-wisilica commented on Oct 17, 2017

    @iraj-wisilica
    Author

    Thanks for the reply. I am struggling to convert MessagePack byte array into JSON.

  5. komamitsu commented on Oct 17, 2017

    @komamitsu
    Member

    You can't convert MessagePack byte array into JSON directly. The following steps are needed.

    1. Deserialize (Unpack) MessagePack byte array to a Java object
    2. Serialize (Pack) the Java object to JSON format string

    So I was interested in which step you're facing now.

  6. iraj-wisilica commented on Oct 17, 2017

    @iraj-wisilica
    Author

    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.

  7. komamitsu commented on Oct 17, 2017

    @komamitsu
    Member

    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

    irb(main):006:0> {name:'Foo Bar', age: 99, options: {aaa: 'AAAA', bbb: 'BBBB'}}.to_msgpack.unpack('C*')
    => [131, 164, 110, 97, 109, 101, 167, 70, 111, 111, 32, 66, 97, 114, 163, 97, 103, 101, 99, 167, 111, 112, 116, 105, 111, 110, 115, 130, 163, 97, 97, 97, 164, 65, 65, 65, 65, 163, 98, 98, 98, 164, 66, 66, 66, 66]
    

    Java

            byte[] bytes = new byte[] {
                    (byte)131, (byte)164, (byte)110, (byte)97, (byte)109, (byte)101, (byte)167, (byte)70,
                    (byte)111, (byte)111, (byte)32, (byte)66, (byte)97, (byte)114, (byte)163, (byte)97,
                    (byte)103, (byte)101, (byte)99, (byte)167, (byte)111, (byte)112, (byte)116, (byte)105,
                    (byte)111, (byte)110, (byte)115, (byte)130, (byte)163, (byte)97, (byte)97, (byte)97,
                    (byte)164, (byte)65, (byte)65, (byte)65, (byte)65, (byte)163, (byte)98, (byte)98,
                    (byte)98, (byte)164, (byte)66, (byte)66, (byte)66, (byte)66};
    
            {
                ObjectMapper objectMapper = new ObjectMapper(new MessagePackFactory());
                Map<String, Object> map = objectMapper.readValue(bytes, new TypeReference<Map<String, Object>>() {});
                System.out.println(map.get("name"));
                System.out.println(map.get("age"));
                System.out.println(map.get("options"));
            }
    
            {
                Map<Value, Value> map = MessagePack.newDefaultUnpacker(bytes).unpackValue().asMapValue().map();
                System.out.println(map.get(ValueFactory.newString("name")));
                System.out.println(map.get(ValueFactory.newString("age")));
                System.out.println(map.get(ValueFactory.newString("options")));
            }
    
    Foo Bar
    99
    {aaa=AAAA, bbb=BBBB}
    Foo Bar
    99
    {"aaa":"AAAA","bbb":"BBBB"}
    
  8. iraj-wisilica commented on Oct 17, 2017

    @iraj-wisilica
    Author

    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();
        }
    
  9. komamitsu commented on Oct 17, 2017

    @komamitsu
    Member

    🎉
    Yeah, using JsonNode would be smarter.

  10. iraj-wisilica commented on Oct 17, 2017

    @iraj-wisilica
    Author

    Thank You.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions