I have the following .json (saved as a file named: settings.json):
[{"compact": false, "noPadding": true}]
On click, I want to change the boolean for "compact" to its opposite state. This is my function to do it (theres a little bit of react in there, but i think it shouldnt matter for my question):
onClick={editJson(this, "settings", "compact", this.context.settings[0]["compact"], !this.context.settings[0]["compact"])}
Which performs the following function:
function editJson(component, filename, field, oldvalue, newvalue) {
var json = component.state[filename] || [];
var i;
for (i = 0; i < json.length; i++) {
if (json[i][field] === oldvalue) {
json[i][field] = newvalue
}
}
$.post('json/write.php', {filename: filename + '.json', data: json}).then(function (data) {
queryJson(component, filename);
});
}
Which then writes back to my .json by performing write.php:
<?php
file_put_contents($_POST['filename'], json_encode($_POST['data']));
return ($_POST['data']);
But now my booleans have become strings (which I dont want):
[{"compact": "true","noPadding": "true"}]
I assume the problem is either with javascripts weak typing or my php, but i cant figure out the solution.
How can I keep my booleans as booleans in this scenario?