I want to replace a parameter in my JSON file with a string. My script scrubs a json file for all 'dialogue' strings, exports them to a txt file where they can be edited and than reimports them back into the json file. The problem I am getting is that, while adding the string back into the json, additional characters are added.
Say I wanted to replace a dialogue from "Hello" To Hello there".
With the example code the original json... "\{Hello " when exported to a txt file becomes "\{Hello there" but, when reimported back into the json becomes ""\\{Hello there"" when the expected result should be "\{Hello there"
I also tried to put asString() for the root line but that didn't work either as i've found on other forums.
Edit: Test code to use. Create a file called "Map130.json"
{
"parameters": "\"Hello, this is a test line.. "
}
select option 1. Edit the generated text file if needed. Then run the program again and select option 2.
// jsonExtractor.cpp : This file contains the 'main' function. Program execution begins and ends there.
//
#include <iostream>
#include <json/value.h>
#include <json/json.h>
#include <fstream>
#include <string>
#include <locale> // std::wstring_convert
#include <codecvt> // std::codecvt_utf8
#include <cstdint> // std::uint_least32_t
using namespace std;
int main()
{
bool doDialogue;
bool writeToTxt = true; //write dialouge (and page/ scene num) to a text file.
//compare the json dialouge (and page/scene num) to txt file.
bool compareLines = false; //If the line counts match, continue. If not stop. seems like it might clear the txt file if this fails...
bool newJson = false; //compares json to text and makes a new json
int jsonLine = 0;
int cReadWrite;
int currentMapNumInt;
string currentMapNumStr;
std::cout << "1 for write to txt file from json. 2 for txt to overwrite json .";
cin >> cReadWrite;
if (cReadWrite == 1) {
writeToTxt = true;
compareLines = false;
newJson = false;
}
else if (cReadWrite == 2) {
writeToTxt = false;
compareLines = true;
newJson = true;
}
else {std::cerr << "WARNING!!! the 1 or 2 thingy... you didnt enter it.\n"; return EXIT_FAILURE;}
cout << currentMapNumStr;
Json::Value root;
Json::Reader reader;
Json::StreamWriterBuilder builder;
builder["emitUTF8"] = true;
std::unique_ptr<Json::StreamWriter> writer(builder.newStreamWriter());
ifstream file("...Map130.json"); //pg Jsonfile
reader.parse(file, root);
cout << root << endl;
Json::Value events = root["parameters"];
cout << events << endl;
string dialogue;
Json::Value tempDialogue;
if (newJson && writeToTxt) {
std::cerr << "WARNING!!! newJson and writeToTxt cannot both be true at the same time.\n";
return EXIT_FAILURE;
}
if (compareLines && writeToTxt) {
std::cerr << "WARNING!!! compareLines and writeToTxt cannot both be true at the same time.\n";
return EXIT_FAILURE;
}
std::fstream txtFileIn;
if (newJson) txtFileIn.open("...Map130.txt"); //new txt file
std::ofstream txtFileOut;
if (writeToTxt) txtFileOut.open("...Map130.txt");//same as above
int numOfLines = std::count(std::istreambuf_iterator<char>(txtFileIn), std::istreambuf_iterator<char>(), '\n');
string txtLine;
wstring convertDialogue;
if (newJson) txtFileIn.seekg(0);
std::ofstream convertedFile;
if (newJson) convertedFile.open("...Map130.json");//new json
tempDialogue = events;//.asString().substr(1, tempDialogue.size() - 1);
cout << "Dialouge" << tempDialogue << endl;
if (writeToTxt) {
writer->write(tempDialogue, &txtFileOut);
txtFileOut << endl;
}
if (newJson) {
getline(txtFileIn, txtLine);
root["parameters"] = txtLine;
}
if (newJson) writer->write(root, &convertedFile);
file.close();
txtFileIn.close();
convertedFile.close();
cout << endl << "done";
}