Skip to content

Commit

Permalink
Change checks for JSON member existence to ignore the type.
Browse files Browse the repository at this point in the history
Now it only checks whether a member of the given name exists. The type is ignored hereby. The type is checked later, when the value of the member is read. If the type is not what we expect, an error will be generated.
  • Loading branch information
BevapDin committed Dec 25, 2019
1 parent f4ac936 commit 283bd1b
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 27 deletions.
8 changes: 4 additions & 4 deletions src/item_factory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1713,19 +1713,19 @@ void Item_factory::load( islot_comestible &slot, const JsonObject &jo, const std

bool got_calories = false;

if( jo.has_int( "calories" ) ) {
if( jo.has_member( "calories" ) ) {
slot.default_nutrition.kcal = jo.get_int( "calories" );
got_calories = true;

} else if( relative.has_int( "calories" ) ) {
} else if( relative.has_member( "calories" ) ) {
slot.default_nutrition.kcal += relative.get_int( "calories" );
got_calories = true;

} else if( proportional.has_float( "calories" ) ) {
} else if( proportional.has_member( "calories" ) ) {
slot.default_nutrition.kcal *= proportional.get_float( "calories" );
got_calories = true;

} else if( jo.has_int( "nutrition" ) ) {
} else if( jo.has_member( "nutrition" ) ) {
slot.default_nutrition.kcal = jo.get_int( "nutrition" ) * islot_comestible::kcal_per_nutr;
}

Expand Down
2 changes: 1 addition & 1 deletion src/mapgen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2085,7 +2085,7 @@ mapgen_palette mapgen_palette::load_internal( const JsonObject &jo, const std::s

// mandatory: every character in rows must have matching entry, unless fill_ter is set
// "terrain": { "a": "t_grass", "b": "t_lava" }
if( jo.has_object( "terrain" ) ) {
if( jo.has_member( "terrain" ) ) {
JsonObject pjo = jo.get_object( "terrain" );
for( const auto &key : pjo.get_member_names() ) {
if( key.size() != 1 ) {
Expand Down
42 changes: 21 additions & 21 deletions src/mission_util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -359,56 +359,56 @@ tripoint mission_util::target_om_ter_random( const std::string &omter, int revea
mission_target_params mission_util::parse_mission_om_target( const JsonObject &jo )
{
mission_target_params p;
if( jo.has_string( "om_terrain" ) ) {
if( jo.has_member( "om_terrain" ) ) {
p.overmap_terrain = jo.get_string( "om_terrain" );
}
if( jo.has_string( "om_terrain_match_type" ) ) {
if( jo.has_member( "om_terrain_match_type" ) ) {
p.overmap_terrain_match_type = jo.get_enum_value<ot_match_type>( "om_terrain_match_type" );
}
if( jo.get_bool( "origin_npc", false ) ) {
p.origin_u = false;
}
if( jo.has_string( "om_terrain_replace" ) ) {
if( jo.has_member( "om_terrain_replace" ) ) {
p.replaceable_overmap_terrain = jo.get_string( "om_terrain_replace" );
}
if( jo.has_string( "om_special" ) ) {
if( jo.has_member( "om_special" ) ) {
p.overmap_special = overmap_special_id( jo.get_string( "om_special" ) );
}
if( jo.has_int( "reveal_radius" ) ) {
if( jo.has_member( "reveal_radius" ) ) {
p.reveal_radius = std::max( 1, jo.get_int( "reveal_radius" ) );
}
if( jo.has_bool( "must_see" ) ) {
if( jo.has_member( "must_see" ) ) {
p.must_see = jo.get_bool( "must_see" );
}
if( jo.has_bool( "cant_see" ) ) {
if( jo.has_member( "cant_see" ) ) {
p.cant_see = jo.get_bool( "cant_see" );
}
if( jo.has_bool( "exclude_seen" ) ) {
if( jo.has_member( "exclude_seen" ) ) {
p.random = jo.get_bool( "exclude" );
}
if( jo.has_bool( "random" ) ) {
if( jo.has_member( "random" ) ) {
p.random = jo.get_bool( "random" );
}
if( jo.has_int( "search_range" ) ) {
if( jo.has_member( "search_range" ) ) {
p.search_range = std::max( 1, jo.get_int( "search_range" ) );
}
if( jo.has_int( "min_distance" ) ) {
if( jo.has_member( "min_distance" ) ) {
p.min_distance = std::max( 1, jo.get_int( "min_distance" ) );
}
if( jo.has_int( "offset_x" ) || jo.has_int( "offset_y" ) || jo.has_int( "offset_z" ) ) {
if( jo.has_member( "offset_x" ) || jo.has_member( "offset_y" ) || jo.has_member( "offset_z" ) ) {
tripoint offset;
if( jo.has_int( "offset_x" ) ) {
if( jo.has_member( "offset_x" ) ) {
offset.x = jo.get_int( "offset_x" );
}
if( jo.has_int( "offset_y" ) ) {
if( jo.has_member( "offset_y" ) ) {
offset.y = jo.get_int( "offset_y" );
}
if( jo.has_int( "offset_z" ) ) {
if( jo.has_member( "offset_z" ) ) {
offset.z = jo.get_int( "offset_z" );
}
p.offset = offset;
}
if( jo.has_int( "z" ) ) {
if( jo.has_member( "z" ) ) {
p.z = jo.get_int( "z" );
}
return p;
Expand Down Expand Up @@ -439,7 +439,7 @@ void mission_util::set_reveal_any( const JsonArray &ja,
void mission_util::set_assign_om_target( const JsonObject &jo,
std::vector<std::function<void( mission *miss )>> &funcs )
{
if( !jo.has_string( "om_terrain" ) ) {
if( !jo.has_member( "om_terrain" ) ) {
jo.throw_error( "'om_terrain' is required for assign_mission_target" );
}
mission_target_params p = parse_mission_om_target( jo );
Expand All @@ -461,7 +461,7 @@ bool mission_util::set_update_mapgen( const JsonObject &jo,
return false;
}

if( jo.has_string( "om_special" ) && jo.has_string( "om_terrain" ) ) {
if( jo.has_member( "om_special" ) && jo.has_member( "om_terrain" ) ) {
const std::string om_terrain = jo.get_string( "om_terrain" );
const auto mission_func = [update_map, om_terrain]( mission * miss ) {
tripoint update_pos3 = mission_util::reveal_om_ter( om_terrain, 1, false );
Expand All @@ -484,9 +484,9 @@ bool mission_util::load_funcs( const JsonObject &jo,
if( jo.has_string( "reveal_om_ter" ) ) {
const std::string target_terrain = jo.get_string( "reveal_om_ter" );
set_reveal( target_terrain, funcs );
} else if( jo.has_array( "reveal_om_ter" ) ) {
} else if( jo.has_member( "reveal_om_ter" ) ) {
set_reveal_any( jo.get_array( "reveal_om_ter" ), funcs );
} else if( jo.has_object( "assign_mission_target" ) ) {
} else if( jo.has_member( "assign_mission_target" ) ) {
JsonObject mission_target = jo.get_object( "assign_mission_target" );
set_assign_om_target( mission_target, funcs );
}
Expand All @@ -496,7 +496,7 @@ bool mission_util::load_funcs( const JsonObject &jo,
if( !set_update_mapgen( update_mapgen, funcs ) ) {
return false;
}
} else if( jo.has_array( "update_mapgen" ) ) {
} else {
for( JsonObject update_mapgen : jo.get_array( "update_mapgen" ) ) {
if( !set_update_mapgen( update_mapgen, funcs ) ) {
return false;
Expand Down
2 changes: 1 addition & 1 deletion src/npctalk.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1778,7 +1778,7 @@ talk_trial::talk_trial( const JsonObject &jo )

read_condition<dialogue>( jo, "condition", condition, false );

if( jo.has_array( "mod" ) ) {
if( jo.has_member( "mod" ) ) {
for( JsonArray jmod : jo.get_array( "mod" ) ) {
trial_mod this_modifier;
this_modifier.first = jmod.next_string();
Expand Down

0 comments on commit 283bd1b

Please sign in to comment.