-
-
Notifications
You must be signed in to change notification settings - Fork 56.2k
core: support parsing back slash \
in parseKey
in FileStorage (JSON)
#27587
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
core: support parsing back slash \
in parseKey
in FileStorage (JSON)
#27587
Conversation
This comment was marked as outdated.
This comment was marked as outdated.
@fengyuentau What about raw strings in python with triple quotes? |
@asmorkalov Thank you for suggestion! Now it works as expected with the # a.json: {"\"":1,"\\":59,"Ġ\"":366,"\\\\":6852}
import cv2 as cv
import json
# a.json
a = cv.FileStorage("a.json", cv.FileStorage_FORMAT_JSON)
print(type(a), a.getNode("\"").real(), a.getNode(r"\\").real())
# <class 'cv2.FileStorage'> 1.0 59.0
with open("a.json", "r") as f:
a = json.load(f)
print(type(a), a["\""], a["\\"])
# <class 'dict'> 1 59 |
@dkurt @asmorkalov I found multiple sources saying that backslash should be escaped as well. So python's json printing a key of double backslash should be the reason of human readability; that's to say in memory it is still parsed as one single backslash # a.json: {"\"":1,"\\":59,"Ġ\"":366,"\\\\":6852}
import json
with open("a.json", "r") as f:
a = json.load(f)
print(a.keys())
# dict_keys(['"', '\\', 'Ġ"', '\\\\'])
print(type(a), a["\""], a["\\"], a[r"\\"])
# <class 'dict'> 1 59 6852 What do you think? |
With the latest commit, we can do the following things correctly: import cv2 as cv
import json
# a.json
a = cv.FileStorage("a.json", cv.FileStorage_FORMAT_JSON)
print(a.getNode("\"").name(), a.getNode("\\").name(), a.getNode(r"\\").name())
# " \ \\
print(type(a), a.getNode("\"").real(), a.getNode("\\").real(), a.getNode(r"\\").real())
# <class 'cv2.FileStorage'> 1.0 59.0 6852.0
with open("a.json", "r") as f:
a = json.load(f)
print(a.keys())
# dict_keys(['"', '\\', 'Ġ"', '\\\\'])
print(type(a), a["\""], a["\\"], a[r"\\"])
# <class 'dict'> 1 59 6852
# tokenizer.json
a = cv.FileStorage("tokenizer.json", cv.FileStorage_FORMAT_JSON)
b = a.getNode("model")
c = b.getNode("vocab")
print(type(a), c.getNode("\"").real(), c.getNode("\\").real())
# <class 'cv2.FileStorage'> 1.0 59.0
with open("tokenizer.json", "r") as f:
a = json.load(f)
b = a["model"]
c = b["vocab"]
print(type(a), c["\""], c["\\"])
# <class 'dict'> 1 59 The only problem now is our key does not show as python's json's. I guess it is fine since it is different way to show information. |
I would say, that it's the case, when real file on opencv_extra is better than hard-coded solution. It's error prone and with file with do not need to re-check escape sequences and other syntax sugar in particular programming language. |
The current test uses raw string; that should be clear enough for readability and maintainability. |
Fixes #27585
Pull Request Readiness Checklist
See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request
Patch to opencv_extra has the same branch name.