Is it possible to parse YAML formatted strings with yaml-cpp?
There isn't a YAML::Parser::Parser(std::string&) constructor. (I'm getting a YAML string via libcurl from a http-server.)
Try using a stringstream:
std::string s = "name: YAML from libcurl";
std::stringstream ss(s);
YAML::Parser parser(ss);
In the new version, you can parse a string directly (see here):
YAML::Node node = YAML::Load("[1, 2, 3]");
Related
I have a text file of format given below (this is just sample file, but actually file is big one with key, values). Some of the values for the key will have format in file (look for value of Port key in below file). This is input file for me.
I am converting this file into Json using CJson Library in C++. Since value corresponding to Port key is not a valid string or value, CJson gives error message saying "Insert missing quotes around strings". So I tried to convert Port value to string using below code. But I am unable to insert missing quotes around. Could you please help me how to "Insert missing quotes around strings".
Code I tried:
ifstream infile { "conf_file.conf" };
string file_contents{ istreambuf_iterator<char>(infile), istreambuf_iterator<char>() };
pParentJson = cJSON_Parse( (char*)file_contents.c_str() );
cJSON* pJsonObj = cJSON_GetObjectItem(pParentJson,"port");
char* pDataBuffer = cJSON_Print(pJsonObj);
cout<<pDataBuffer<<endl;
Text File
{
"port": <CbTcpPortVariable>
"IP" : "127.0.0.1"
"Message": "Hello"
}
I have a text file (ansi encoding) that contains an arabic contents and I have read it using c++ as:
ifstream ifs(file.GetFileName());
std::string content((std::istreambuf_iterator<char>(ifs)), std::istreambuf_iterator<char>());
unfortunately the content variable holds encrypted strings (which must be in arabic lang) i.e:
121101 ÇáÒÈæä ßãÇá
121102 ÇáÒÈæä ÓÚíÏ
121103 ÇáÒÈæä ÚãÇÑ
any solution???
Thanks :)
I'm trying to encode a file to Base64 in Jmeter to test a web service using the following script:
String file = FileUtils.readFileToString(new File("${filepath}"), "UTF-8");
vars.put("file", new String(Base64.encodeBase64(file.getBytes("UTF-8"))));
This works fine for plain/text files and does not work for other file types correctly.
How could I make it work for other file types?
Jmeter has many potions to convert a variable to "Base64", below are a few options
Bean shell pre processor
BeanShell PostProcessor
BeanShell Sampler.
Below is the "bean shell" code, which used in "Bean shell pre processor" to convert variable to Base64
import org.apache.commons.codec.binary.Base64;
String emailIs= vars.get("session");
byte[] encryptedUid = Base64.encodeBase64(emailIs.getBytes());
vars.put("genStringValue",new String(encryptedUid));
Example :
Before Base64 :jdwqoeendwqqm12sdw
After Base64 :amR3cW9lZW5kd3FxbTEyc2R3
Converted using Jmeter :
Converted Using base64 site:
As Groovy is now the preferred JSR223 script language in each JMeter Sampler, Pre- & PostProcessor, Listener, Assertion, etc. this task is pretty easy.
def fileAsBase64 = new File("${filepath}").bytes.encodeBase64().toString()
vars.put("file", fileAsBase64)
Or as one liner:
vars.put("file", new File("${filepath}").bytes.encodeBase64().toString())
That's it.
Use the function ${__base64Encode(nameofthestring)} to encode the string value and ${__base64Decode(nameofthestring)} to decode the string value.
Your issue comes from the fact that you're considering binary files as text when reading them which is wrong.
Use this for reading the file:
https://commons.apache.org/proper/commons-io/apidocs/org/apache/commons/io/FileUtils.html#readFileToByteArray(java.io.File)
Then encode the byte array in Base64
Try this
import org.apache.commons.codec.binary.Base64;
String forEncoding = vars.get("userName") + ":" + vars.get("passWord");
byte[] token = Base64.encodeBase64(forEncoding.getBytes());
vars.put("token", new String(token));
Alternative for this is to directly create a groovy function in User Defined Variable as
${__groovy(new File(vars.get("filepath")).bytes.encodeBase64().toString(),)}
I have string with xml content, writing it into a file and reading it back with xmlParseFile affects performance, if there is a way to parse the string directly, can you please show it with an example?
Consider xmlParseMemory instead.
I have string with xml content
So you should be able to do it like so:
const std::string xmlContent = "<something> </something>";
xmlDocPtr doc = xmlParseMemory(xmlContent.c_str(), xmlContent.length());
I'm using jsoncpp and I'm having a problem with how the json messages are formatted when they are written using one of the Writers.
For example:
root["name"] = "monkey";
std::cout << writer.write(root) << "\n";
Gives me something formatted like this
{
"name" : "monkey"
}
While I actually want:
{"name":"monkey"}
I've looked at the documentation and there are mentions of setIndentLength() but they don't appear in the source files, so maybe they are deprecated or something.
Anyway anyone knows how to do this?
As an extension of cdunn2001's answer, there is no need to re-write default settings (.settings_). You can just override 'indentation' value of StreamWriterBuilder builder:
Json::Value json = ...
Json::StreamWriterBuilder builder;
builder["commentStyle"] = "None";
builder["indentation"] = ""; //The JSON document is written in a single line
std::unique_ptr<Json::StreamWriter> writer(builder.newStreamWriter());
writer->write(json, &std::cout);
If you use Jsoncpp version 1.1, you can use Json::FastWriter instead of Json::StyledWriter or Json::Writer :
The JSON document is written in a single line. It is not intended for
'human' consumption, but may be usefull to support feature such as RPC
where bandwith is limited.
FastWriter, StyledWriter, StyledStreamWriter, and Writer are deprecated. Use StreamWriterBuilder, which creates a StreamWriter with a slightly different API. Use it this way:
Json::StreamWriterBuilder builder;
builder.settings_["indentation"] = "";
std::unique_ptr<Json::StreamWriter> writer(builder.newStreamWriter());
writer->write(root, &std::cout);