Stop QSettings from removing quotes in file
-
Hello,
I'm trying to use QSettings to parse a config file which looks something like this :
[foo] type = bar token = {"access_token":"bs_token", "expiry":"date"}
The problem is that QSettings remove the "s (token become
token = {access_token:bs_token, expiry:date}
)when I read it and add stuff to it, and the file become unusable from the other program that uses it, because the json part is invalid.Is there a way to prevent QSettings to do this?
-
@bvieducasse said in Stop QSettings from removing quotes in file:
token = {"access_token":"bs_token", "expiry":"date"}
I don't think this is legal syntax for the right-hand side of an entry in a
QSettings
file. How did you produce it? Not viaQSettings
saving itself, right?I think
QSettings
expects values to be something like a string, a number, .... Something with{
...}
looks like some complex "object" notation, e.g. in JSON. You can only save JSON as a string, not as an object. You might find something more like:token = "{'access_token':'bs_token', 'expiry':'date'}"
works? In any case, try creating that JSON object and saving it to
QSettings
file to see how it actually stores it, and then follow that pattern. I have a feeling you may need to go via QByteArray QJsonDocument::toJson() const to get any JSON object turned into text to save (neitherQJsonObject
noQJsonValue
offer to convert to text from what I can see). -
The file was produced by rclone. Unfortunately, rclone won't work using ' instead of ".
-
@bvieducasse
What I don't know what rclone is. I think you are showing that what it produces is not acceptable toQSettings
. Not everything which might might be in a non-Qt-generated INI file is.If you cannot alter or preprocess what rclone produces, at the moment I can only see that from the value
{access_token:bs_token, expiry:date}
that you get back you would have to figure to re-insert the necessary quote characters to make this acceptable as parsable JSON. -
rclone is a programme used to connect to various cloud service, but the revelant part is that it uses OAuth 2.0 token which are stored in the ini file.
That's why my original question was if there was a way to stop QSettings from altering the file other than the stuff I want to change. The whole point was to not have to parse the file myself, so working around the problem is not really satisfactory.
-
@bvieducasse
If there were a way to tellQSettings
to behave differently with respect to the quotes I/the documentation would have said so.Glancing through the source code at https://codebrowser.dev/qt5/qtbase/src/corelib/io/qsettings.cpp.html#799, I think that shows hard-coded handling of
"
characters in the value, to skip them from the returned value.I am not suggesting you parse the whole file yourself. I am suggesting you (do your best to) re-parse the value returned for the
token
key, i.e.{access_token:bs_token, expiry:date}
, to restore necessary quotes, if that is what you need. If this is a line which you do not need to read or change when saving back,QSettings
does not offer this, when you use it to write back with your other changes it will save entries in the way it thinks they should be, not leave an original text line in the file unaltered. -
You can create your own QSettings format. See QSettings::registerFormat.
-
@mchinand
Yes, that's what I thought! Good idea for OP's need for "raw" input line, but then a lot of code to emulate whatQSettings
is doing line-by-line for the rest of the input :( And the OP has said "The whole point was to not have to parse the file myself". -
Ok it seems this is the best I can get from QSettings. It's a bit frustrating because it was this close to be the perfect tool, but it's all right.
Thanks for your time.