Skip to content

Commit

Permalink
Merge pull request opencv#18858 from fegorsch:improve-persistence-doc
Browse files Browse the repository at this point in the history
  • Loading branch information
alalek committed Nov 20, 2020
2 parents 12fd382 + c996fd1 commit 049b50d
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 4 deletions.
9 changes: 5 additions & 4 deletions modules/core/include/opencv2/core/persistence.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -403,8 +403,8 @@ class CV_EXPORTS_W FileStorage

/**
* @brief Simplified writing API to use with bindings.
* @param name Name of the written object
* @param val Value of the written object
* @param name Name of the written object. When writing to sequences (a.k.a. "arrays"), pass an empty string.
* @param val Value of the written object.
*/
CV_WRAP void write(const String& name, int val);
/// @overload
Expand Down Expand Up @@ -437,9 +437,10 @@ class CV_EXPORTS_W FileStorage
CV_WRAP void writeComment(const String& comment, bool append = false);

/** @brief Starts to write a nested structure (sequence or a mapping).
@param name name of the structure (if it's a member of parent mapping, otherwise it should be empty
@param name name of the structure. When writing to sequences (a.k.a. "arrays"), pass an empty string.
@param flags type of the structure (FileNode::MAP or FileNode::SEQ (both with optional FileNode::FLOW)).
@param typeName usually an empty string
@param typeName optional name of the type you store. The effect of setting this depends on the storage format.
I.e. if the format has a specification for storing type information, this parameter is used.
*/
CV_WRAP void startWriteStruct(const String& name, int flags, const String& typeName=String());

Expand Down
26 changes: 26 additions & 0 deletions modules/core/test/test_io.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1640,6 +1640,32 @@ TEST(Core_InputOutput, FileStorage_free_file_after_exception)
ASSERT_EQ(0, std::remove(fileName.c_str()));
}

TEST(Core_InputOutput, FileStorage_write_to_sequence)
{
const std::vector<std::string> formatExts = { ".yml", ".json", ".xml" };
const std::string fileName = "FileStorage_write_to_sequence";

for (const auto& ext : formatExts)
{
FileStorage fs(fileName + ext, FileStorage::WRITE);
std::vector<int> in = { 23, 42 };
fs.startWriteStruct("some_sequence", cv::FileNode::SEQ);
for (int i : in)
fs.write("", i);
fs.endWriteStruct();
fs.release();

FileStorage fsIn(fileName + ext, FileStorage::READ);
FileNode seq = fsIn["some_sequence"];
FileNodeIterator it = seq.begin(), it_end = seq.end();
std::vector<int> out;
for (; it != it_end; ++it)
out.push_back((int)*it);

EXPECT_EQ(in, out);
}
}

TEST(Core_InputOutput, FileStorage_YAML_parse_multiple_documents)
{
const std::string filename = "FileStorage_YAML_parse_multiple_documents.yml";
Expand Down

0 comments on commit 049b50d

Please sign in to comment.