-
Notifications
You must be signed in to change notification settings - Fork 25
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
Comments
I have added a test case that shows how a SimpleValue can be decoded here: 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: What I was expecting: |
Ok, understood, so all you need is a proper |
Actually I am using toString() on a dataItem. Look: for (DataItem dataItem : dataItems) { 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: Message I am reading after decoding: 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 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: |
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? |
@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 |
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. |
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:
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?
The text was updated successfully, but these errors were encountered: