You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I have default app settngs in JSON object DefaultSettings and I'm parsing current app settings from file Config.json (ifstream). How to compare indexes from Settings with indexes in DefaultSettings? Does library have functions for this? I tried to compare with iterator_wrapper but he's deprecated.
For example, i have this JSON string and parsing it. Let this JSON will be a Settings. Settings string I'm getting from file. { "Value1": 4, "Value3": "Hello" }
And this string will be DefaultSettings. { "Value1": 4, "Value2": true, "Value3": "Hello" }
This string I'm parsing into objects. In DefaultSettings i have three values, but in Settings only two. And when I will use Settings["Value2"] it will generate an exception. For fix it, I want to compare indexes and their values after parsing. Now I'll try to use items().
// start with a copy of the default settings
json s = DefaultSettings;
// overwrite all default settings with those given in Settings
s.update(Settings);
Assume Settings is {"Value1": 100} and DefaultSettings like above. Then the result would be s = {"Value1": 100, "Value2": true, "Value3": "Hello" }.
Activity
nlohmann commentedon Mar 11, 2018
The
items()function replaces the deprecatediterator_wrapper.Can you provide an example so I can understand what you mean with "compare indexes"?
zkelo commentedon Mar 11, 2018
For example, i have this JSON string and parsing it. Let this JSON will be a
Settings.Settingsstring I'm getting from file.{ "Value1": 4, "Value3": "Hello" }And this string will be
DefaultSettings.{ "Value1": 4, "Value2": true, "Value3": "Hello" }This string I'm parsing into objects. In
DefaultSettingsi have three values, but inSettingsonly two. And when I will use Settings["Value2"] it will generate an exception. For fix it, I want to compare indexes and their values after parsing. Now I'll try to useitems().nlohmann commentedon Mar 11, 2018
The
updatefunction could be handy for you:Assume
Settingsis{"Value1": 100}andDefaultSettingslike above. Then the result would bes = {"Value1": 100, "Value2": true, "Value3": "Hello" }.zkelo commentedon Mar 11, 2018
Big thanks.
pboettch commentedon Mar 11, 2018
@zkelo FWIW: for a larger use cases you might consider using JSON-schemas for validating JSON objects.