diff --git a/modules/core/include/opencv2/core/persistence.hpp b/modules/core/include/opencv2/core/persistence.hpp index 9e3f8f6914..276f640323 100644 --- a/modules/core/include/opencv2/core/persistence.hpp +++ b/modules/core/include/opencv2/core/persistence.hpp @@ -403,8 +403,8 @@ public: /** * @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 @@ -437,9 +437,10 @@ public: 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()); diff --git a/modules/core/test/test_io.cpp b/modules/core/test/test_io.cpp index e695300d4b..d30c485368 100644 --- a/modules/core/test/test_io.cpp +++ b/modules/core/test/test_io.cpp @@ -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 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 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 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";