I have a json object in c++. I am using json_cpp library.
I want to get the string from the Json::Value object. I am using it like below.
Json::Value obj;
....
....
....
string str = obj.toStyledString();
This returns the string in the pretty print format. But I want the string without any indentation. How can I do that as there are no other functions provided in the class?
You could use Json::FastWriter it does not have any indentation and formatting since it outputs everything on a single line. it is normally not suitable for 'human' consumption.
std::string toUnStyledString(const Json::Value& value)
{
Json::FastWriter writer;
return writer.write( value );
}
The function toStyledString also simply uses a Json::StyledWriter if you look into the definition of Json::Value::toStyledString.
Well, if this library doesn't provide appropriate methods then you could write them yourself. The JSON format is rather simple, so I don't think that it will take a lot of work.
Here you can find a good graphical representation of JSON format:
http://json.org
P.S. I've never worked with this particular library, so I propose sort of a general solution.
UPDATE: another option is to get a string returned by toStyledString() and remove indentation. But it requires string processing and will probably be resource consuming. Note that you can't just remove tabs/spaces/new line symbols, because they can be a part of JSON object.
Why do you want unindented string again?
Related
I have a Question. I want to copy something from a string. I have this:
string buffer = "
{"clientToken":"clientToken","accessToken":"abdjuhsdhjsksdnasfldafgkuadbkghubdfhlujgbdfhulgdfbhugfdbgujhlfdanhjgkhdfanhnjkgbafdhkugbadcjgfdabhgfdabgjhdfabkhgfdbghujfdabghkjfdabghujfadbgfdjhaugbafdhjujgjbfuhkgbf
dhugdbfauhgbfaluhgbdafilgbdfhgfdigujladbijfbghdufjbvgfbhgadbfgbdfjgfbgjfdbjflbgjedfbgauiadfbuigbuifgdabhf
juhgbdaihjfhgbdiuflghbdfiugbfdugbbbbb","selectedProfile":
{"name":"secret","id":"secret"},"availableProfiles":[{"name":"secret","id":"nothing"}]}";
string token;
and i want to put the accestoken (the long gibberish) into the string token(the rest is not needed only the accesstoken). Can anyone help me?
There are few ways to do it, but the most simple is to locate "accessToken" with std::string::find(), then located leading and ending " of the token value itself, and then use std::string::substr() to extract a necessary piece. But in general, you better use some json parsing library, like https://github.com/nlohmann/json
So I am just getting familiar with rapidjson.h but I can't find this one basic piece of example code of parsing the *.json file.
I found the official [turorial][1].
But here however they parse the json stored in a C string. I know how this string is supposed to look like but I'm lazy to make a custom Parser just to convert my file to this string. I mean I was kinda hoping that rapidjson is supposed to do that for me. Please correct me if I'm wrong.
The closest thing I found to what I need is here How to read json file using rapidjson and output to std::string?
Therefore I was really surprised that I can't just do something like this (with *.json file being in the same folder as my program):
rapidjson::Document d;
d.Parse("myJson.json");
My 1. question is:
Do I have to use std::ifstream and rapidjson::IStreamWrapper to get my Document like in the example above and what other as simple as possible alternatives there are?
My 2. question is: (this one would be much easier to ask if i could comment the post above)
What does the R mean in std::ifstream ifs { R"(C:\Test\Test.json)" }; and how
do I change the C:\Test\Test.json string to const char* variable?
Because this isn't working.
const char* str = "C:\Test\Test.json";
std::ifstream ifs { R"(str)" }; //error
std::ifstream ifs { R(str) }; //error
std::ifstream ifs{ (str) }; //ok but I don't like it
[1]: https://rapidjson.org/md_doc_tutorial.html
You can use FileReadStream and ParseStream instead of the IStreamWrapper. According to the documentation, FileReadStream is much faster than IStreamWrapper.
The R means that it's a raw string literal. Without it, the backslashes are interpreted as the start of escape sequences and you would have to write it like this to make it correct:
"C:\\Test\\Test.json"
Or you could use forward slashes:
"C:/Test/Test.json"
Usually I use streams for formatting stuff however in this case ?I don't know the format until runtime.
I want to be able to take something like the following format string:
Hello {0}! Your last login was on {1,date:dd/mm/yy}.
...and feed in the variables "Fire Lancer" and 1247859223, and end up with the following formatted string:
Hello Fire Lancer! Your last login was on 17/07/09.
In other languages I use there is built in support for this kind of thing, eg pythons format string method, however in c++ there doesn't seem to be any such functionality, accept the C print methods which are not very safe.
Also this is for a high performance program, so whatever solution I use needs to parse the format string once and store it (eg mayby a Parse method that returns a FormatString object with a Format(string) method), not reparse the string every time the format method is called...
Your format string looks very much like those used in ICU MessageFormat. Did you consider using it?
Boost Formatting does that for you:
http://www.boost.org/doc/libs/1_39_0/libs/format/doc/format.html
Check out this question and answer for examples of usage:
boost::format will do the positional arguments portion, but not the date formatting...
const XMLDataNode *pointsNode = node->GetChildren().at(0);
std::wistringstream pointsstrm(*pointsNode->GetInnerText());
pointsstrm >> loadedGame.points;
This is code I've written to pull an int from an XML file and pass it into loadedGame.points (an int). However, this isn't working. It compiles but doens't give the right value. Why is that? XMLDataNode is a class that manipulates xmllite.dll.
Time for some wild guesses!
I'll bet you that the text you get from *pointsNode->GetInnerText() isn't what you think it is. Have you checked that it is indeed exactly the text you want? In particular, could it contain whitespace? Parsing a nicely formatted (i.e. indented, broken into lines, etc) XML file without a schema to reference ends up meaning that all sorts text nodes involving whitespace will end up in your DOM tree.
I have binary std::string and I need insert it into the BLOB (MySQL) using simple data layer I have. So, I need to execute query: ExecuteSQL((LPTSTR)strQ).
When I am creating this query string (strQ) I cannot add anything to the string after I add this binary string - it just kind if terminated and nothing can be added. I do not want to use mysql_real_escape_string because i want to keep it not only for MySQL.
Anybody to HELP PLEASE!!!
Assuming you have code that looks something like this:
std::string s = ... // populate string somehow
ExecuteSQL( (LPCSTR) s );
Then you have several problems. Forstly, the cast won't work. In C++, whenever you use a cast you are almost certainly doing something incorrect which will break your code. You need to create a null-terminated string using the std::string member function c_str():
ExecuteSQL( s.c_str() );
However, this may not fix all your your problems because you say you hava a binary string. If that string contains the zero byte, then your SQL will terminate at that character rather than the end of string. In that case you probably need to investigate binding your values explicitly.
Edit: For details of how to bind a parameter to a MySQL statement, see http://dev.mysql.com/doc/refman/5.1/en/mysql-stmt-bind-param.html