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

Decode CBOR to primitive data types

Open felipheggaliza opened this issue 8 years ago • 10 comments

Hi,

I still didn't figure out how to decode CBOR to primitive data types. Can you give me an example?

I have been trying this:

ByteArrayInputStream bais = new ByteArrayInputStream(data);

    List<DataItem> dataItems = null;

    try {
        dataItems = new CborDecoder(bais).decode();
    } catch (CborException e) {
        e.printStackTrace();
    }

    JsonObject jsonResponseObject = null;
    for (DataItem dataItem : dataItems) {
        // process data item
        Log.d(TAG, "Decoded CBOR: " + dataItem.toString());
        Log.d(TAG, "Decoded CBOR: " + dataItem.getMajorType().toString());

    }

Output:

Decoded CBOR: SIMPLE_VALUE Decoded CBOR: MAP

I am expecting a boolean value instead of words: SIMPLE_VALUE or MAP. How can I fix it?

felipheggaliza avatar May 16 '17 19:05 felipheggaliza

I have added a test case that shows how a SimpleValue can be decoded here: https://github.com/c-rack/cbor-java/blob/master/src/test/java/co/nstant/in/cbor/decoder/SimpleValueDecoderTest.java

Does this help?

c-rack avatar May 16 '17 19:05 c-rack

I read this test case, but I really didn't understand how to decode at least the most common data types such as boolean, integer, Strings, etc..

I am having a problem, when I am exchanging messages with a server that is serializing JSON Objects and then when I decode those messages I get something like this:

Problem: { status : device not found } {sucess: SIMPLE_VALUE}

What I was expecting: { status : "device not found" } {sucess: false}

felipheggaliza avatar May 24 '17 16:05 felipheggaliza

Ok, understood, so all you need is a proper toString() for SimpleValue objects, right?

c-rack avatar May 24 '17 17:05 c-rack

Actually I am using toString() on a dataItem. Look:

for (DataItem dataItem : dataItems) { // process data item Log.d(TAG, "Decoded CBOR: " + dataItem.toString()); }

Maybe a proper toString() can be the solution for this issue.

In the project I am working now, I an encoding and decoding JSON messages. Therefore sometimes I am getting errors like for example:

Suppose this JSON message below was encoded using CBOR on the server side:

Message I expected: {error_cause: "device not found", success: true }

Message I am reading after decoding: {error_cause: device not found , success: SIMPLE_VALUE }

Problems: error_cause does not have quotation marks, and success did not arrive with a boolean value.

felipheggaliza avatar May 24 '17 17:05 felipheggaliza

Ok, but then you don't need a toString() (although it would be an enhancement to have a proper one), but a toJsonElement() (if you would use GSON, for example), which then gives you the proper quotation marks in output.

Maybe someone wants to write a cbor-gson (or whatever you use) converter, but that would be out of scope of the pure cbor-java project. In the meantime, maybe the Jackson CBOR converter might be what you are looking for: https://github.com/FasterXML/jackson-dataformats-binary/tree/master/cbor

c-rack avatar May 24 '17 17:05 c-rack

Actually the method dataItem.getMajorType() is returning MAP, how can I decode a MAP type?

It seems I have a MAP with a SIMPLE_VALUE inside.

felipheggaliza avatar Jun 02 '17 17:06 felipheggaliza

Could you please post the CBOR-encoded byte array for your example? Maybe we can add a unit test that helps to understand the issue. Thanks!

c-rack avatar Jun 03 '17 20:06 c-rack

hello, I get the question, if I use decode method and get a result dataItem with type Array , now I can only get the MajorType is ARRAY and Value=4 from the result dataItem, but how can I get the real value in the array since the DataItem is not serializable and no other method provide? FYI: if I use dataItem .toString() only get this: [co.nstant.in.cbor.model.ByteString@5b35308e, 3455111133]

flurryCat avatar Apr 10 '18 03:04 flurryCat

@flurryCat if you get a DataItem of type Array, simply cast that object to Array in order to access the array elements:

Array array = (Array) yourDataItem;
List<DataItem> elements = array.getDataItems();

https://github.com/c-rack/cbor-java/blob/master/src/main/java/co/nstant/in/cbor/model/Array.java#L21 Same holds for other types.

c-rack avatar Apr 11 '18 03:04 c-rack

This seems to have turned into a question about a use case that is explicitly not on topic (conversion to JSON), it might be prudent to close the issue otherwise it will linger forever.

owlstead avatar Jan 11 '24 12:01 owlstead