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"
}
Related
I'd like to test if it's possible assigning a list of strings from a txt file to a terraform variable.
I have a file named ip_adresses.txt with something like this:
["1.1.1.1", "8.8.8.8", ..., "n"]
This txt is constantly changing with different IP addresses, I'd like to have this values into a terraform resource of WAF IP_Set, which it requires an argument of addresses as an array of strings.
I tried setting up my variable like this, giving the value of the variable the path of my file, which both are at the same directory level:
variable "addresses_ipv4" {
default = "ip_adresses.txt"
}
and in my resource:
addresses = file(var.addresses_ipv4)
But I got this Error Message:
Inappropriate value for attribute "addresses": set of string required.
How can I tell terraform that I want to use the content in my file, which is an array of strings, as the variable for that needed list?
Your actual code return "[\"1.1.1.1\", \"8.8.8.8\"]", you have to use jsondecode like this:
addresses = jsondecode(file(var.addresses_ipv4))
This will parse your JSON in your file and return an array of strings.
Output example
[
"1.1.1.1",
"8.8.8.8",
]
Beside the format of the IPs you have in your txt are not correct, aws_wafv2_ip_set waiting a format like this: "1.1.1.1/32" not just "1.1.1.1"
I have a JSON input that I would like to transform to another JSON output. I defined my list of input JSONPaths, and trying to create a simple JSON output in the template like so:
{
"fax": \"<userFax>\"
}
This was one of the formats given in the example from AWS themselves:
{
"instance": \"<instance>\",
"state": [9, \"<state>\", true],
"Transformed": "Yes"
}
However, when I try to update the changes, I get the following error:
Invalid InputTemplate for target ... : [Source: (String)"{ "fax": \"null\" }"; line: 2, column: 13].
Basically, I'd like all incoming values in the input to be converted to strings as an output via the template. This is to prevent values like zip codes from being converted into an integer and having it's leading zero stripped away. But it's confusing that even following the simple example from AWS is failing.
I am trying to write a file (json/yaml/xml, the format does not matter) that stores a filepath with a specific cv::Mat. As I ended up realizing here, the key combination does not allow for a path (/home/user, for example).
void
persist_histograms(const QString& filepath, const QHash<QString, Mat>& histograms)
{
cv::FileStorage storage(filepath.toStdString(), cv::FileStorage::WRITE);
QHashIterator<QString, Mat> it(histograms);
while (it.hasNext()) {
it.next();
storage << it.key().toStdString() << it.value();
}
}
Is this a real limitation of the cv::FileStorageclass? Or is there a way of going around it?
The end result should be something like:
{
"/my/path/to/file.png": "<my cv::Mat serialization here>",
"/my/path/to/another/file.png": "<my cv::Mat serialization here>",
...
}
Observation
The error occurs independently of the format. If I pass a filepath that
ends with either yaml/json/xml the errors are the same:
This is the error if a try adding a letter at the beginning of the key:
This is the error I get when trying the code above:
OpenCV Error: Unspecified error (Incorrect element name /home/user/dtd/images/freckled/freckled_0055.jpg) in operator<<, file /build/opencv/src/opencv-3.2.0/modules/core/src/persistence.cpp, line 6877
terminate called after throwing an instance of 'cv::Exception'
what(): /build/opencv/src/opencv 3.2.0/modules/core/src/persistence.cpp:6877: error: (-2) Incorrect element name /home/user/dtd/images/freckled/freckled_0055.jpg in function operator<<
This is the error I get if I try to add a letter to the beginning of the key.
OpenCV Error: Bad argument (Key names may only contain alphanumeric characters [a-zA-Z0-9], '-', '_' and ' ') in icvJSONWrite, file /build/opencv/src/opencv-3.2.0/modules/core/src/persistence.cpp, line 3896
It seems that I have encountered a similar error. If I try to store a matrix in YAML format like this
std::string fName("/path/to/cameraParams.yml");
cv::FileStorage fs(fName, cv::FileStorage::WRITE);
fs << "camera matrix" << cameraMatrix;
everything works fine. But as soon as I change the file format from YAML to XML
std::string fName("/path/to/cameraParams.xml");
I get an error
terminate called after throwing an instance of 'cv::Exception'
what(): OpenCV(4.0.0-pre) /home/shura/software.downloads/libraries/opencv/opencv/modules/core/src/persistence_xml.cpp:83: error: (-5:Bad argument) Key name may only contain alphanumeric characters [a-zA-Z0-9], '-' and '_' in function 'writeTag'
As I have realized, the reason is that XML format does not allow spaces in a tag, while YAML does. So for YAML the file below is completely valid
%YAML:1.0
---
camera matrix: !!opencv-matrix
while for XML something like this is not allowed
<?xml version="1.0"?>
<opencv_storage>
<camera matrix type_id="opencv-matrix">
As soon as I replace the space in "camera matrix" by underscore, everything works fine.
fs << "camera_matrix" << cameraMatrix;
As I have found out here XML format has some restrictions on symbols you can have in a tag name. In particular, slashes, which you have in names, are not allowed. Dots seem to be allowed in tags, but for some reason OpenCV refuses to work with tags, which contain dots.
I tried using ptree to fetch value of key in c++ for key name having multiple . in keys.
so I have json,
"product": {
"product.description.text": "Some text here"
}
I tried calling
std::string product = pt.get_value("product.product.description.text");
but can't get value.
please help me
According to http://www.boost.org/doc/libs/1_43_0/doc/html/boost_propertytree/accessing.html
You may use something like the following:
pt.get<std::string>('/', "product/product.description.text");
The code I'm using to write a string to a file is as follows:
void addToWhitelist(System::String ^emailAddress)
{
StreamWriter ^pwriter = gcnew StreamWriter("whitelist.txt");
pwriter->WriteLine(emailAddress);
pwriter->Close();
}
This works well, but as soon as I run the function again, the string that was written to the text file is overwritten with the new value. How would I go about appending the string to the file on a new line?
Try adding "true" as second parameter to StreamWriter constructor.
(Boolean value says whether to append)