Skip to content

Small improvements to persistence-API doc#18761

Closed
fegorsch wants to merge 1 commit intoopencv:masterfrom
fegorsch:improve-persistence-doc
Closed

Small improvements to persistence-API doc#18761
fegorsch wants to merge 1 commit intoopencv:masterfrom
fegorsch:improve-persistence-doc

Conversation

@fegorsch
Copy link
Copy Markdown

@fegorsch fegorsch commented Nov 9, 2020

  • Add instructions on how to write unnamed objects with Python-API for writing to sequences.
  • Add more information on the typeName-parameter.

@alalek
Copy link
Copy Markdown
Member

alalek commented Nov 11, 2020

Does it work with all supported formats?
Could you please add simple test for that in this file?

@asmorkalov
Copy link
Copy Markdown
Contributor

It looks like empty key is not possible at least for YAML (syntax issue) and XML (empty tag). I propose to add explicit check to code to not allow empty keys. @alalek @vpisarev what do you think?

@asmorkalov
Copy link
Copy Markdown
Contributor

I tried FileStorage with empty key string and it throws assertion as expected. So empty key name is not allowed.

C++ code:

    std::string fname = std::string("test.json");
    FileStorage fs(tempfile(fname.c_str()), cv::FileStorage::WRITE);
    fs.write("", 10);
    fs.release();

Ouput:

unknown file: Failure
C++ exception with description "OpenCV(4.5.0-dev) /home/alexander/Projects/OpenCV/opencv-master2/modules/core/src/persistence_json.cpp:161: error: (-5:Bad argument) An attempt to add element without a key to a map, or add element with key to sequence in function 'writeScalar'

@asmorkalov
Copy link
Copy Markdown
Contributor

The same behavior for writeStruct call:

    std::string fname = std::string("test.json");
    FileStorage fs(tempfile(fname.c_str()), cv::FileStorage::WRITE);
    fs.startWriteStruct("", cv::FileNode::MAP);
    fs.endWriteStruct();
    fs.release();

Output:

unknown file: Failure
C++ exception with description "OpenCV(4.5.0-dev) /home/alexander/Projects/OpenCV/opencv-master2/modules/core/src/persistence_json.cpp:161: error: (-5:Bad argument) An attempt to add element without a key to a map, or add element with key to sequence in function 'writeScalar'

@asmorkalov
Copy link
Copy Markdown
Contributor

The PR is closed as proposed documentation changes do not correspond to real OpenCV behavior.

@asmorkalov asmorkalov closed this Nov 17, 2020
@fegorsch
Copy link
Copy Markdown
Author

fegorsch commented Nov 17, 2020

It looks like empty key is not possible at least for YAML (syntax issue) and XML (empty tag). I propose to add explicit check to code to not allow empty keys. @alalek @vpisarev what do you think?

Passing an empty key is only possible, if you write to a sequence (i.e. called fs.startWriteStruct("sequence", FileNode::SEQ) before). I rephrased the comment to make this clearer. I also provided a test.

The changes are not reflected in the closed PR, they can be viewed here. Can the PR be reopend, should I open a new one, or is still something amiss?

@asmorkalov
Copy link
Copy Markdown
Contributor

Please create new PR. Github does not allow to re-open this one as force push is done.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

category: documentation Documentation fix or update

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants