Skip to content

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

Closed
@davidalbertonogueira

Description

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);

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions