0

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!”."

hoboayoyo
  • 3
  • 3

1 Answers1

0

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);
Botje
  • 26,269
  • 3
  • 31
  • 41
  • so with your changes, the last line writer->write(tempDialogue, txtFile); gives me a conversion argument error that states " no suitable conversion function from "std::fstream" to "json::OStream *" exists. – hoboayoyo Jun 29 '23 at 15:28
  • Take the address of `txtFile` instead, like I did in my edit. – Botje Jun 29 '23 at 19:06
  • That did the trick. Thanks. Can you explain why passing it by reference worked instead of passing it by value? – hoboayoyo Jun 30 '23 at 12:12
  • The `&` operator takes the **address**, which results in a `fstream*`, and `json::OStream` is a typedef for `std::ostream`. – Botje Jun 30 '23 at 12:29