-
-
Notifications
You must be signed in to change notification settings - Fork 6.8k
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
Overriding default values? #1083
Comments
There is the |
Hi nlohmann, thanks for the help. When I call update, I get this error: My default object looks like:
It seems it may be complaining about the hierarchy? |
It says that you can only call |
Here's the whole code: #include "ParseInput.h"
#include "nlohmann/json.hpp"
#include <fstream>
#include <iostream>
json getDefaultConfig(void){
json jsn =
{
"geometry" ,
{
"endianness" , "little"
},
"nsSolver" ,
{
"tol" , 1.0e-8,
"CFL0" , 1.0,
"CFL1" , 200.0,
"CFLsteps" , 100,
"JacobiSweeps" , 30
},
"ransSolver" ,
{
"tol" , 1.0e-6,
"JacobiSweeps" , 30
},
"freestream" ,
{
"Re" , 1000.0,
"M" , 0.5,
"Tref" , 300,
"alpha" , 0.0,
"beta" , 0.0
},
{
"boundaries",
{
}
}
};
return jsn;
}
json readConfig(std::string file){
std::ifstream i(file);
json j;
i >> j;
i.close();
json dflt = getDefaultConfig();
//dflt.update(j.begin(), j.end());
for(auto it= j.begin(); it != j.end(); ++it){
dflt[it.key()].update(it.value());
}
return dflt;
} |
What would be the content of an input file? |
The example content of an input file is: {
"geometry" :
{
"meshName" : "wing.ugrid",
"endianness" : "little"
},
"nsSolver" :
{
"tol" : 1.0e-8,
"CFL0" : 0.1,
"CFL1" : 500.0,
"CFLsteps" : 100,
"JacobiSweeps" : 50
},
"ransSolver" :
{
"tol" : 1.0e-6,
"CFL0" : 0.1,
"CFL1" : 5.0,
"CFLsteps" : 100,
"JacobiSweeps" : 50
},
"freestream" :
{
"Re" : 10000.0,
"M" : 0.25,
"Tref" : 300,
"alpha" : 0.0,
"beta" : 0.0
},
"boundaries" : {
"1" : {
"type" : "NoSlipWall"
},
"2" : {
"type" : "SlipWall"
},
"3" : {
"type" : "Freestream"
}
}
} Thanks for the help! |
I think the issue is the JSON value returned by [
"geometry",
[
"endianness",
"little"
],
"nsSolver",
[
"tol",
"1e-08",
"CFL0",
1.0,
"CFL1",
200.0,
"CFLsteps",
100,
"JacobiSweeps",
30
],
"ransSolver",
[
"tol",
"1e-06",
"JacobiSweeps",
30
],
"freestream",
[
"Re",
1000.0,
"M",
0.5,
"Tref",
300,
"alpha",
0.0,
"beta",
0.0
],
[
"boundaries",
{}
]
] You need to take care about the braces required to create an object: json j = { {"key", "value"} }; That is, you need to have an extra pair of braces around each key/value pair. |
I assumed how your JSON value should look like. #include <iostream>
#include <fstream>
#include "json.hpp"
using json = nlohmann::json;
json getDefaultConfig(void){
json jsn = R"(
{
"geometry": {
"endianness": "little"
},
"nsSolver": {
"tol": 1e-08,
"CFL0": 1.0,
"CFL1": 200.0,
"CFLsteps": 100,
"JacobiSweeps": 30
},
"ransSolver": {
"tol": 1e-06,
"JacobiSweeps": 30
},
"freestream": {
"Re": 1000.0,
"M": 0.5,
"Tref": 300,
"alpha": 0.0,
"beta": 0.0
},
"boundaries": {}
})"_json;
return jsn;
}
json readConfig(std::string file){
std::ifstream i(file);
json j;
i >> j;
i.close();
json dflt = getDefaultConfig();
//dflt.update(j.begin(), j.end());
for(auto it= j.begin(); it != j.end(); ++it) {
dflt[it.key()].update(it.value());
}
return dflt;
}
int main() {
auto j = readConfig("file.json");
std::cout << std::setw(2) << j << std::endl;
} Output: {
"boundaries": {
"1": {
"type": "NoSlipWall"
},
"2": {
"type": "SlipWall"
},
"3": {
"type": "Freestream"
}
},
"freestream": {
"M": 0.25,
"Re": 10000.0,
"Tref": 300,
"alpha": 0.0,
"beta": 0.0
},
"geometry": {
"endianness": "little",
"meshName": "wing.ugrid"
},
"nsSolver": {
"CFL0": 0.1,
"CFL1": 500.0,
"CFLsteps": 100,
"JacobiSweeps": 50,
"tol": 1e-08
},
"ransSolver": {
"CFL0": 0.1,
"CFL1": 5.0,
"CFLsteps": 100,
"JacobiSweeps": 50,
"tol": 1e-06
}
} |
I have a json file that I read in using nlohmann:json. It contains nested dicts/lists that are configurations specified by users.
Only when a user specifies a value in this file (read in to a json object) should it override the default value, which is specified by another json object.
My question is: what's the easiest way to replace all the items in one json object with that of another json object, if that item exists in the other json object?
The text was updated successfully, but these errors were encountered: