How to convert C++ string array to json? - c++

I have started implementing Microsoft Cognitive Services using C++. I have a C++ String array(faceIds array)
string faceIds[] ={
"29e874a8-a08f-491f-84e8-eac263d51fe1",
"6f89f38a-2411-4f6c-91b5-15eb72c17c22",
"7284b730-6dd7-47a3-aed3-5dadaef75d76",
"1fc794fa-3fd4-4a78-af11-8f36c4cbf14c",
"3e57afca-bd1d-402e-9f96-2cae8dbdfbfa",
"c2a4e0f5-4277-4f5a-ae28-501085b05209",
"23b5910e-9c32-46dd-95f3-bc0434dff641"
};
Then, I try to convert string array(C++) to json string.
JSONObject jsnobject = new JSONObject(10);
JSONArray jsonArray = jsnobject.getJSONArray(faceIds);
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject explrObject = jsonArray.getJSONObject(i);
}
But, I got problem. So, My question is, How to convert C++ string array to json?
Thank in Advance.

Your question doesn't precisely identify your input and expected output. Are you parsing the C++ from a file? I can't tell.
If the first code block is an autogenerated input file and will always have that whitespace pattern and the JSON equivalent is your desired output, replace the first line with "[\n" and the last line with "]/n" and you're done.
If you can't guarantee the white space pattern of the input file, then you will need a C++ parser to generate an AST (abstract symbol tree) that you can traverse to find the faceIds array RHS (right hand side) and then do the same thing as shown below from that AST collection.
If you simply want to iterate in C++ through faceIds, then the following code should produce the desired JSON string:
#include <iostream>
#include <sstream>
std::string faceIds[] = {
"29e874a8-a08f-491f-84e8-eac263d51fe1",
"6f89f38a-2411-4f6c-91b5-15eb72c17c22",
"7284b730-6dd7-47a3-aed3-5dadaef75d76",
"1fc794fa-3fd4-4a78-af11-8f36c4cbf14c",
"3e57afca-bd1d-402e-9f96-2cae8dbdfbfa",
"c2a4e0f5-4277-4f5a-ae28-501085b05209",
"23b5910e-9c32-46dd-95f3-bc0434dff641"
};
int main() {
std::ostringstream ostr;
ostr << '[' << std::endl;
int last = std::extent<decltype(faceIds)>::value - 1;
int i = 0;
while (i < last)
ostr << " \"" << faceIds[i ++] << "\"," << std::endl;
ostr << " \"" << faceIds[i] << "\"" << std::endl;
ostr << ']' << std::endl;
std::cout << ostr.str();
return 0;
}
If you want some library's object representation, then you'll have to identify what library you are using so we can review its API. Whatever library you use, you could always just run whatever parse method it has on ostr.str() above, but we could find a more efficient method to build the equivalent JSON tree if you identified the JSON library. One can't uniquely identify the library from an object name like JSONObject, which is a class name used in dozens of libraries.

This is a robust cross platform solution to working with JSON in C++ https://github.com/nlohmann/json. I'm sure Microsoft has some library locked to their own OS too. The examples are clear.

I think the nlohmann c++ library is useful in your case.

Related

Finding keyword in strings c++

I'm still fairly new to C++ and programming so I might just be missing something big here.
I'm trying to create a chatbot for a library, deals with opening times ect and other things. I want the chatbot to be able to pickup key words in a input and then be able to call the right function that will be able to give some text back to them.
For example:
user: what time is the library open till?
//chatbot picks up the key word 'open' and returns the right function
chatbot: the libraries open between 6 and 5
It shouldn't be as hard as I am finding it to be able to get the chatbot to do this.
The function I'm having trouble with:
std::string GetKeywords(){
std::string KQuery = GetQuery();
std::vector<std::string> keywords{"open", "opening", "times", "close", "closing", "shut"};
if(std::find(keywords.begin(), keywords.end(), KQuery) != keywords.end()){
std::cout << "Library is open when I say it is" << std::endl;
}
return 0;
};
This is returning a memory error and is the only place in my code which throws an issue.
All my code:
#include <iostream>
#include <string>
#include <vector>
#include "FinalProject.hpp"
//introducing funtions
void PrintIntro();
std::string GetQuery();
std::string RunScripts();
std::string GetKeywords();;
// introducing chatbot
RunScript ChatBot;
int main(){
PrintIntro();
GetQuery();
GetKeywords();
};
void PrintIntro(){
//printing introductory text to ask the user for input
std::cout << "Hi, I'm Librarius, I'm here to help you with University library queries" << std::endl;
std::cout << "I can help you with the following: \n Spaces to study \n Opening times \n Taking out books \n Returning books\n" << std:: endl;
std::cout << "Ask away!" << std::endl;
return;
};
std::string GetQuery(){
//getting input from the user
std::string Query = "";
std::getline(std::cin, Query);
if(Query.empty()){
//checking to see if the user hasnt entered anything
std::cout << "Hey! Why didnt you enter anything?! I don't want to waste my time!" << std::endl;
};
return Query;
};
std::string GetKeywords(){
std::string KQuery = GetQuery();
std::vector<std::string> keywords{"open", "opening", "times", "close", "closing", "shut"};
if(std::find(keywords.begin(), keywords.end(), KQuery) != keywords.end()){
std::cout << "Library is open when I say it is" << std::endl;
}
return 0;
};
//using the input got from the user to decide which script to run
//TODO analyse the users keywords and decide on a script to run
//TODO return an appropriate script
Thanks for your help!
The problem with
std::find(keywords.begin(), keywords.end(), KQuery)
is it is going to see if the entire string in KQuery matches one of your keywords. Since KQuery has a sentence in it, it isn't going to find a match. What you need to do is loop through all the keywords and see if KQuery.find(keyword) returns a valid result.
You can do that using std::find_if and a lambda like
std::find_if(keywords.begin(), keywords.end(),
[&](auto const& keyword){ return KQuery.find(keyword) != std::string::npos; });
This will return an iterator to the first keyword it finds in KQuery or keywords.end() if none of the keywords are found.

C++ converting vector to 2 hex and then store it in a string

I have been using mostly C so I am pretty new into c++. I want to convert a int vector (std::vector) into hexadecimal representation and then store that into a string. I found something I would use in C in the following thread: Converting hex into a string using 'sprintf'
The code proposed by user411313 is the following:
static unsigned char digest[16];
static unsigned char hex_tmp[16];
for (i = 0; i < 16; i++) {
printf("%02x",digest[i]);
sprintf(&hex_tmp[i], "%02x", digest[i]);
}
One of my concerns is that this could go out of index at some point since sprintf may try to add a 0 after the content. Also, I was wondering if there is any way to do it with native C++, perhaps any built function could be used instead of C functions. Is this preferable in c++ over c functions? Thank you very much for your assistance!
if there is any way to do it with native C++, perhaps any built function could be used instead of C functions. Is this preferable in c++ over c functions?
Sure there is a way, and yes it's preferable:
static std::array<unsigned char,16> digest;
static std::string hex_tmp;
for (auto x : digest) {
std::ostringstream oss;
oss << std::hex << std::setw(2) << std::setfill('0') << (unsigned)x;
hex_tmp += oss.str();
}
One of my concerns is that this could go out of index at some point since sprintf may try to add a 0 after the content.
That's a valid concern. The classes used in my code snippet above will overcome all these issues, and you don't need to care about.
You can use std::stringstream
std::string hex_representation(const std::vector<int>& v) {
std::stringstream stream;
for (const auto num : v) {
stream << "0x" << std::hex << std::setw(2) << std::setfill('0') << num
<< ' ';
}
return stream.str();
}
Obviously you can remove the "0x" prefix if you don't need it
Here a live demo.

Integer to string conversion issues

I am experiencing a few problems with Crypto++'s Integer class. I am using the latest release, 5.6.2.
I'm attempting to convert Integer to string with the following code:
CryptoPP::Integer i("12345678900987654321");
std::ostrstream oss;
oss << i;
std::string s(oss.str());
LOGDEBUG(oss.str()); // Pumps log to console and log file
The output appears to have extra garbage data:
12345678900987654321.ÍÍÍÍÍÍÍÍÍÍÍýýýý««««««««îþîþ
I get the same thing when I output directly to the console:
std::cout << "Dec: " << i << std::endl; // Same result
Additionally, I cannot get precision or scientific notation working. The following will output the same results:
std::cout.precision(5); // Does nothing with CryptoPP::Integer
std::cout << "Dec: " << std::setprecision(1) << std::dec << i << std::endl;
std::cout << "Sci: " << std::setprecision(5) << std::scientific << i << std::endl;
On top of all of this, sufficiently large numbers breaks the entire thing.
CryptoPP::Integer i("12345");
// Calculate i^16
for (int x = 0; x < 16; x++)
{
i *= i;
}
std::cout << i << std::endl; // Will never finish
Ultimately I'm trying to get something where I can work with large Integer numbers, and can output a string in scientific notation. I have no problems with extracting the Integer library or modifying it as necessary, but I would prefer working with stable code.
Am I doing something wrong, or is there a way that I can get this working correctly?
I'm attempting to convert Integer to string with the following code:
CryptoPP::Integer i("12345678900987654321");
std::ostrstream oss;
oss << i;
std::string s(oss.str());
LOGDEBUG(oss.str()); // Pumps log to console and log file
The output appears to have extra garbage data:
12345678900987654321.ÍÍÍÍÍÍÍÍÍÍÍýýýý««««««««îþîþ
I can't reproduce this with Crypto++ 5.6.2 on Visual Studio 2010. The corrupted output is likely the result of some other issue, not a bug in Crypto++. If you haven't done so already, I'd suggest trying to reproduce this in a minimal program just using CryptoPP::Integer and std::cout, and none of your other application code, to eliminate all other possible problems. If it's not working in a trivial stand-alone test (which would be surprising), there could be problems with the way the library was built (e.g. maybe it was built with a different C++ runtime or compiler version from what your application is using). If your stand-alone test passes, you can add in other string operations, logging code etc. until you find the culprit.
I do notice though that you're using std::ostrstream which is deprecated. You may want to use std::ostringstream instead. This Stack Overflow answer to the question "Why was std::strstream deprecated?" may be of interest, and it may even the case that the issues mentioned in that answer are causing your problems here.
Additionally, I cannot get precision or scientific notation working.
The following will output the same results:
std::cout.precision(5); // Does nothing with CryptoPP::Integer
std::cout << "Dec: " << std::setprecision(1) << std::dec << i << std::endl;
std::cout << "Sci: " << std::setprecision(5) << std::scientific << i << std::endl;
std::setprecision and std::scientific modify floating-point input/output. So, with regular integer types in C++ like int or long long this wouldn't work either (but I can see that especially with arbitrary-length integers like CryptoPP:Integer being able to output in scientific notation with a specified precision would make sense).
Even if C++ didn't define it like this, Crypto++'s implementation would still need to heed those flags. From looking at the Crypto++ implementation of std::ostream& operator<<(std::ostream& out, const Integer &a), I can see that the only iostream flags it recognizes are std::ios::oct and std::ios::hex (for octal and hex format numbers respectively).
If you want scientific notation, you'll have to format the output yourself (or use a different library).
On top of all of this, sufficiently large numbers breaks the entire
thing.
CryptoPP::Integer i("12345");
// Calculate i^16
for (int x = 0; x < 16; x++)
{
i *= i;
}
std::cout << i << std::endl; // Will never finish
That will actually calculate i^(2^16) = i^65536, not i^16, because on each loop you're multiplying i with its new intermediate value, not with its original value. The actual result with this code would be 268,140 digits long, so I expect it's just taking Crypto++ a long time to produce that output.
Here is the code adjusted to produce the correct result:
CryptoPP::Integer i("12345");
CryptoPP::Integer i_to_16(1);
// Calculate i^16
for (int x = 0; x < 16; x++)
{
i_to_16 *= i;
}
std::cout << i_to_16 << std::endl;
LOGDEBUG(oss.str()); // Pumps log to console and log file
The output appears to have extra garbage data:
12345678900987654321.ÍÍÍÍÍÍÍÍÍÍÍýýýý««««««««îþîþ
I suspect what you presented is slighty simplified from what you are doing in real life. I believe the problem is related to LOGDEBUG and the ostringstream. And I believe you are outputting char*'s, and not string's (though we have not seen the code for your loggers).
The std::string returned from oss.str() is temporary. So this:
LOGDEBUG(oss.str());
Is slighty different than this:
string t(oss.str());
LOGDEBUG(t);
You should always make a copy of the string in an ostringstream when you intend to use it. Or ensure the use is contained in one statement.
The best way I've found is to have:
// Note: reference, and the char* is used in one statement
void LOGDEBUG(const ostringstream& oss) {
cout << oss.str().c_str() << endl;
}
Or
// Note: copy of the string below
void LOGDEBUG(string str) {
cout << str.c_str() << endl;
}
You can't even do this (this one bit me in production):
const char* msg = oss.str().c_str();
cout << msg << endl;
You can't do it because the string returned from oss.str() is temporary. So the char* is junk after the statement executes.
Here's how you fix it:
const string t(oss.str());
const char* msg = t.c_str();
cout << msg << endl;
If you run Valgrind on your program, then you will probably get what should seem to be unexplained findings related to your use of ostringstream and strings.
Here is a similar logging problem: stringstream temporary ostream return problem. Also see Turning temporary stringstream to c_str() in single statement. And here was the one I experienced: Memory Error with std:ostringstream and -std=c++11?
As Matt pointed out in the comment below, you should be using an ostringstream, and not an ostrstream. ostrstream has been deprecated since C++98, and you should have gotten a warning when using it.
So use this instead:
#include <sstream>
...
std::ostringstream oss;
...
But I believe the root of the problem is the way you are using the std::string in the LOGDEBUG function or macro.
Your other questions related to Integer were handled in Softwariness's answer and related comments. So I won't rehash them again.

PugiXML C++ getting content of an element (or a tag)

Well I'm using PugiXML in C++ using Visual Studio 2010 to get the content of an element, but the thing is that it stops to getting the value when it sees a "<" so it doesn't get the value, it just gets the content till it reaches a "<" character even if the "<" is not closing its element. I want it to get till it reaches its closing tag even if it ignores the tags, but only the text inside of the inner tags, at least.
And I also would like to know how to get the Outer XML for example if I fetch the element
pugi::xpath_node_set tools = doc.select_nodes("/mesh/bounds/b");
what do I do to get the whole content which would be " Link Till here"
this content is the same given down here:
#include "pugixml.hpp"
#include <iostream>
#include <conio.h>
#include <stdio.h>
using namespace std;
int main//21
() {
string source = "<mesh name='sphere'><bounds><b id='hey'> <a DeriveCaptionFrom='lastparam' name='testx' href='http://www.google.com'>Link Till here<b>it will stop here and ignore the rest</b> text</a></b> 0 1 1</bounds></mesh>";
int from_string;
from_string = 1;
pugi::xml_document doc;
pugi::xml_parse_result result;
string filename = "xgconsole.xml";
result = doc.load_buffer(source.c_str(), source.size());
/* result = doc.load_file(filename.c_str());
if(!result){
cout << "File " << filename.c_str() << " couldn't be found" << endl;
_getch();
return 0;
} */
pugi::xpath_node_set tools = doc.select_nodes("/mesh/bounds/b/a[#href='http://www.google.com' and #DeriveCaptionFrom='lastparam']");
for (pugi::xpath_node_set::const_iterator it = tools.begin(); it != tools.end(); ++it) {
pugi::xpath_node node = *it;
std::cout << "Attribute Href: " << node.node().attribute("href").value() << endl;
std::cout << "Value: " << node.node().child_value() << endl;
std::cout << "Name: " << node.node().name() << endl;
}
_getch();
return 0;
}
here is the output:
Attribute Href: http://www.google.com
Value: Link Till here
Name: a
I hope I was clear enough,
Thanks in advance
My psychic powers tell me you want to know how to get the concatenated text of all children of the node (aka inner text).
The easiest way to do that is to use XPath like that:
pugi::xml_node node = doc.child("mesh").child("bounds").child("b");
string text = pugi::xpath_query(".").evaluate_string();
Obviously you can write your own recursive function that concatenates the PCDATA/CDATA values from the subtree; using a built-in recursive traversing facility, such as find_node, would also work (using C++11 lambda syntax):
string text;
text.find_node([&](pugi::xml_node n) -> bool { if (n.type() == pugi::node_pcdata) result += n.value(); return false; });
Now, if you want to get the entire contents of the tag (aka outer xml), you can output a node to string stream, i.e.:
ostringstream oss;
node.print(oss);
string xml = oss.str();
Getting inner xml will require iterating through node's children and appending their outer xml to the result, i.e.
ostringstream oss;
for (pugi::xml_node_iterator it = node.begin(); it != node.end(); ++it)
it->print(oss);
string xml = oss.str();
That's how XML works. You can't embed < or > right in your values. Escape them (e.g. using HTML entities like < and >) or define a CDATA section.
I've struggled a lot with the issue of parsing subtree including all elements and sub-nodes - the easiest way is almost what shown here:
You should use this code:
ostringstream oss;
oNode.print(oss, "", format_raw);
sResponse = oss.str();
Instead of oNode use the node that you want, if needed use pugi:: before every function.

Jsoncpp problems

I am using Jsoncpp to parse json-formats for c++.
I do not understand how it works though; there is a lack of documentation and examples to get me started, and I was wondering if anyone could give me some quick pointers. The only examples I've found deals with files...
I'm using a HTTP stack to get a json-message in a buffer. For example, a buffer contains the message {"state":"Running"}. How do I use the Json::reader to parse this? Again the only example I've found deals with reading from files
How do you write values to a Json-message? For example I want to write "monkey : no" and "running : yes" to a Json-message which I can then use in my GET request.
Thanks
UPDATE:
on 1), for example, how to parse a buffer containing a json-message like this:
char* buff;
uint32_t buff_size;
Maybe this is good sample for first part of your question:
Json::Value values;
Json::Reader reader;
reader.parse(input, values);
Json::Value s = values.get("state","default value");
There is anything but lack of documentation. Yes, it's mainly reference documentation, but it's quite good and well cross-linked.
Just read the documentation
Just use this class or possibly use the other class
Sample code for your reference, below:
file.json
{
"B":"b_val2",
"A":{
"AA":"aa_val1",
"AAA" : "aaa_val2",
"AAAA" : "aaaa_val3"
},
"C":"c_val3",
"D":"d_val4"
}
jsoncpp usage scenario as below, for above sample json file.
#include <iostream>
#include "json/json.h"
#include <fstream>
using namespace std;
int main(){
Json::Value root;
Json::Reader reader;
const Json::Value defValue; //used for default reference
std::ifstream ifile("file.json");
bool isJsonOK = ( ifile != NULL && reader.parse(ifile, root) );
if(isJsonOK){
const Json::Value s = root.get("A",defValue);
if(s.isObject()){
Json::Value s2 = s.get("AAA","");
cout << "s2 : " << s2.asString() << endl;
}else{
cout << "value for key \"A\" is not object type !" << endl;
}
}
else
cout << "json not OK !!" << endl;
return 1;
}
Output::
s2 : aaa_val2
Additionally, I have used the "amalgamate.py" for generating and using the jsoncpp for the sample source above.