Skip to content

Commit 049b50d

Browse files
committed
Merge pull request opencv#18858 from fegorsch:improve-persistence-doc
2 parents 12fd382 + c996fd1 commit 049b50d

File tree

2 files changed

+31
-4
lines changed

2 files changed

+31
-4
lines changed

modules/core/include/opencv2/core/persistence.hpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -403,8 +403,8 @@ class CV_EXPORTS_W FileStorage
403403

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

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

modules/core/test/test_io.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1640,6 +1640,32 @@ TEST(Core_InputOutput, FileStorage_free_file_after_exception)
16401640
ASSERT_EQ(0, std::remove(fileName.c_str()));
16411641
}
16421642

1643+
TEST(Core_InputOutput, FileStorage_write_to_sequence)
1644+
{
1645+
const std::vector<std::string> formatExts = { ".yml", ".json", ".xml" };
1646+
const std::string fileName = "FileStorage_write_to_sequence";
1647+
1648+
for (const auto& ext : formatExts)
1649+
{
1650+
FileStorage fs(fileName + ext, FileStorage::WRITE);
1651+
std::vector<int> in = { 23, 42 };
1652+
fs.startWriteStruct("some_sequence", cv::FileNode::SEQ);
1653+
for (int i : in)
1654+
fs.write("", i);
1655+
fs.endWriteStruct();
1656+
fs.release();
1657+
1658+
FileStorage fsIn(fileName + ext, FileStorage::READ);
1659+
FileNode seq = fsIn["some_sequence"];
1660+
FileNodeIterator it = seq.begin(), it_end = seq.end();
1661+
std::vector<int> out;
1662+
for (; it != it_end; ++it)
1663+
out.push_back((int)*it);
1664+
1665+
EXPECT_EQ(in, out);
1666+
}
1667+
}
1668+
16431669
TEST(Core_InputOutput, FileStorage_YAML_parse_multiple_documents)
16441670
{
16451671
const std::string filename = "FileStorage_YAML_parse_multiple_documents.yml";

0 commit comments

Comments
 (0)