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

CBOR serialization of a given JSON value does not serialize #641

Closed
davidalbertonogueira opened this issue Jun 28, 2017 · 3 comments
Closed
Labels
aspect: binary formats BSON, CBOR, MessagePack, UBJSON

Comments

@davidalbertonogueira
Copy link

davidalbertonogueira commented Jun 28, 2017

Hi,
I was CBOR serializing a JSON object, and noticed that, whereas when dumping a json object to a string, json string values (i.e., case value_t::string )are escaped (a call to escape_string is made), no such call is made when json values are string values in the CBOR approach.

I was reading the CBOR CRF 7049 and it seems that strings do not need to be escaped when serializing to CBOR.
From your library the behavior is consistent, you don't escape when serializing, nor except to be escaped when de-serializing.
But it appears that Newtonsoft.Json (C# library), expects that. Is it a valid expectation? Is it possible to have a serialization that behaves in this way, in case some libraries expect that behavior?

My use case involves reading the cbor byte string output in c#.
C++ side:

nlohmann::json json_doc;
json_doc["characters"] = nlohmann::json::array();
  
for (int i = 0; i < characters.size(); i++) {
  json_doc["characters"][i]["name"] = (characters[i] != nullptr) ? characters[i]->name() : "";
}

std::vector<uint8_t> cbor = nlohmann::json::to_cbor(json_doc);
output->assign((char*)&cbor[0], cbor.size());

C# side. cbor_bytes is the cbor byte string (c++ output vector)

CBORObject cbor = CBORObject.DecodeFromBytes(cbor_bytes);
output = cbor.ToString();

Such output string by then, is wrongly formed:
{"characters": [{"name": "Clara Oswald"}, {"name": "Kensi Blye"}, {"name": "Temperance "Bones" Brennan"}]}
and cannot, obviously be parsed:

JObject output_obj = JObject.Parse(output);
@nlohmann nlohmann added the aspect: binary formats BSON, CBOR, MessagePack, UBJSON label Jun 28, 2017
@nlohmann
Copy link
Owner

I'm on holiday right now and cannot have a look at this issue for the next two weeks.

@CodeMasterYi
Copy link

what about json_doc.dump()?

@davidalbertonogueira
Copy link
Author

Already fixed it. It has nothing to do with nlohmann::json.
In C#, the correct call is cbor.ToJSONString() instead of cbor.ToString(), as I was doing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
aspect: binary formats BSON, CBOR, MessagePack, UBJSON
Projects
None yet
Development

No branches or pull requests

3 participants