We are doing a black box testing in our project,For that we wanted to use json and c++ and Jzon for parsing.can anybody show how to write a json file for c++ method and parse to get the node by using jzon ?
To write Json object (Jzon::Node) to file you can use Jzon::Writer class.
There are three methods writeStream,writeString and writeFile. Based on target where you want to save your object you can you appropriate method. To save into file you should use writeFile.
You code should be like this.
std::string filename="some path and file name.json";
Jzon::Node node;
//fill your object
Jzon::Writer write;
write.writeFile(node,filename);
Related
I wrote a function that takes a list of proto.Message objects. Looking at the documentation, it seems like proto.Message wraps protoreflect.ProtoMessage which contains a single function, ProtoReflect() Message. Looking at the documentation for Message, it implements a number of other functions that return types referenced by the protoreflect package.
It seems that attempting to create a mock proto.Message would be a lot more work that it's worth but I don't want to go through the whole process of creating a protobuf file, compiling it and referencing it just for unit testing.
Is there another way I can create a mock proto.Message object?
After looking into this further, I decided that the best thing to do would be to create an actual protobuf message:
syntax = "proto3";
package my.package;
option go_package = "path/to/my/package"; // golang
message TestRecord {
string key = 1;
string value = 2;
}
and compile it into a Golang file and then modify it for my own purposes:
$ protoc --go_out=./gopb/ --go_opt=paths=source_relative *.proto
Once the test file has been created, I can delete it or save it as a record.
I'm trying to implement an icon-based property in Windows File Explorer, and my understanding from this post is that it requires returning a property store binary file from the property handler. Does anyone know how to create a property store binary file? After searching, I've come across some documentation on the specification, but I don't see any examples of how to create one. Thank you very much.
You don't need any binary file, you just need an implementation of IPropertyStore. You can create one using the PSCreateMemoryPropertyStore method.
IPropertyStore *ps;
if (SUCCEEDED(PSCreateMemoryPropertyStore(IID_PPV_ARGS(&ps))))
{
// do your work
ps->Release();
}
I am trying to create a .docx file from a word template (.dotx), using OpenXML 2.5 library. More exactly, I'm trying to follow this , but I want to open the document using a MemoryStream and not the path to the file.
So in the link above, the approach is this: Copy the template file in a new file on disk. Open the new file as a WordProcessingDocument, change its type to Document and add a reference to the template (I'm not sure of why is this necessary ...)
What I need is. Get my template from the DB as byte array. Copy the array to a stream and open it as a WordProcessingDocument. Then do the same thing as above.
I don't want to use temporary files on disk and so on. What I don;t understand is why should I reference the template file in the new document (Actually, inspecting the xml files in the docx archive, it just adds a xml node containing the name of the template... why?)
Now, my problem is that if I use a MemoryStream and not a physical path to the file, the resulting docx is corrupted. I'm getting "Microsoft office cannot open this file because some parts are missing or invalid".
If I use temporary disk files, it works.
Why is this happening?
Simplified, my code looks like this:
byte[] result = null;
byte[] templateBytes = System.IO.File.ReadAllBytes("template.dotx");
using (MemoryStream templateStream = new MemoryStream())
{
templateStream.Write(templateBytes, 0, (int)templateBytes.Length);
using (WordprocessingDocument doc = WordprocessingDocument.Open(templateStream, true))
{
doc.ChangeDocumentType(WordprocessingDocumentType.Document);
var mainPart = doc.MainDocumentPart;
//Here I should create an AttachedTemplate object and assign a relationship id to it, which also implies
//setting a "reference" to the template ? I'm not using files so there's nothing to "reference" anyway
// If it's doing more than just putting a visual reference to the template file, then why limit the way of referencing the template
// only by using the Uri to the file? Please take a look at the above link to see what I'm referring to.
//Write some text in the file ....
mainPart.Document.Save();
File.WriteAllBytes("Result.docx", templateStream.ToArray());
}
}
This gets me a corrupted docx file. If I switch from MemoryStream to opening the new docx file from disk (using the other constructor of the WordProcessingDocument class), it works.
I'm not doing something wrong (like trying to write the result to the file after disposing the memory stream), so it should give me the same result.
What is wrong with my code?
Thanks
If you take the last line of your code File.WriteAllBytes out of the inner using block, it should work correctly. Just put that line right before the closing brace of the outer using block.
I am working with a library that uses XSD for creating objects from their XML-based format in C++.
Basically, the inheritance diagram looks something like this:
class BaseClass: public xsd::cxx::tree::type;
class MainXmlObject: public BaseClass;
I think I need to convert my MainXmlObject to a xerces::DOMDocument and then use DOMWriter to write the actual XML file, but I couldn't find the right routines so far.
What is the best way to do this?
It seems like adding the --add-serialization flag to the xsd code generation and then using something like:
xml_schema::namespace_infomap map;
// map[""].name = "test"; // xmlns
// map[""].schema = "http://sbgn.org/libsbgn/0.2"; // xsi:noNamespaceSchemaLocation
ofstream ofs(fname.c_str());
sbgn_(ofs, s, map); // invoking the stream here
ofs.close();
works. References: Adding serialization and details from the XSD guide.
I have a char* buffer of data that I want to parse as XML in libxml2.
How would one go about that?
Currently I am using it to open the file automatically by calling a file name, but it would be nice to have more functionality.
Currently I do it like this:
xmlDocPtr doc = xmlParseFile("data/foo.xml");
However I have a resource system that gives me access to the raw data, so my more preferred method would be:
resource_base_ptr res = load_resource("data/foo.xml");
xmlDocPtr doc /*= some_function(res->raw_data) */;
You need to use xmlReadMemory()
http://xmlsoft.org/html/libxml-parser.html#xmlReadMemory