Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Decode CBOR to primitive data types #39

Open
felipheggaliza opened this issue May 16, 2017 · 10 comments
Open

Decode CBOR to primitive data types #39

felipheggaliza opened this issue May 16, 2017 · 10 comments
Labels

Comments

@felipheggaliza
Copy link

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?

@c-rack
Copy link
Owner

c-rack commented May 16, 2017

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?

@felipheggaliza
Copy link
Author

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}

@c-rack
Copy link
Owner

c-rack commented May 24, 2017

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

@felipheggaliza
Copy link
Author

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.

@c-rack
Copy link
Owner

c-rack commented May 24, 2017

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

@felipheggaliza
Copy link
Author

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.

@c-rack
Copy link
Owner

c-rack commented Jun 3, 2017

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!

@flurryCat
Copy link

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]

@c-rack
Copy link
Owner

c-rack commented Apr 11, 2018

@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.

@owlstead
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants