Decode CBOR to primitive data types
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?
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?
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}
Ok, understood, so all you need is a proper toString() for SimpleValue objects, right?
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.
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
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.
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!
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 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.
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.