Search code examples
c++jsoncpp

Jsoncpp reading some characters as escaped unicode


Problem: I'm trying to extract a line from a json file and put it in a txt file. The issue is that certain characters are represented as escaped unicode instead of the standard unicode representation.

Attempts to fix this issue: I'm not exactly sure what is causing this issue. My guess is that it has something to do with JSONcpp being unicode 8 instead of unicode 16. I looked up on ways to convert a unicode 8 string to 16 by looking up https://stackoverflow.com/questions/4804298/how-to-convert-wstring-into-string. But that didn't work either. I also looked up if it was something with the file stream system https://en.cppreference.com/w/cpp/io/basic_fstream and that did not get me my desired results either. ](https://stackoverflow.com)
example code...

    Json::Value root;
    Json::Reader reader; 
    Json::StyledStreamWriter writer;
    ifstream  file("fileName");
    //wifstream  file(L"fileName");
    //ifstream<wchar_t> file("FileName");
    std::fstream txtFile("txtFileName");
    reader.parse(file, root);
    Json::Value events = root["events"];

//contains "He said “stop!”."
tempDialogue = events[eventIndex]["pages"][pageIndex]["list"][listIndex]["parameters"][0];
writer.write(txtFile, tempDialogue);
file.close();
txtFile.close();

text File than contains
"He said \u201cHey stop \u201d."
expected result
"He said “stop!”."


Solution

  • The JSON specification allows non-ascii characters to be represented in the native UTF-8 encoding or as a \uXXXX escape. JSONCPP converts everything into the native UTF-8 encoding internally, so what you are seeing is result of the default configuration of the StreamWriter.

    You can construct a StreamWriter yourself that emits only the UTF-8 form using a StreamWriterBuilder:

    StreamWriterBuilder builder;
    builder["emitUTF8"] = true;
    std::unique_ptr<Json::StreamWriter> writer(builder.newStreamWriter())
    writer->write(tempDialogue, &txtFile);