Skip to content

Commit

Permalink
Convert a bunch of trivial deserialize functions off JsonIn
Browse files Browse the repository at this point in the history
Most instances of deserialize instantly convert from JsonIn to the
object oriented interface, usually JsonObject. Using the improved
interface on JsonIn we can change the signatures on these to more
accurately reflect what they're doing. Also help get rid of JsonIn
which is not supported in the binary json refactor this unblocks.
  • Loading branch information
akrieger committed Sep 7, 2021
1 parent 668f5d2 commit 6e14a2b
Show file tree
Hide file tree
Showing 109 changed files with 364 additions and 477 deletions.
12 changes: 4 additions & 8 deletions src/achievement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -164,8 +164,7 @@ struct achievement_requirement {

bool becomes_false = false; // NOLINT(cata-serialize)

void deserialize( JsonIn &jin ) {
const JsonObject &jo = jin.get_object();
void deserialize( const JsonObject &jo ) {
if( !( jo.read( "event_statistic", statistic ) &&
jo.read( "is", comparison ) &&
( comparison == achievement_comparison::anything ||
Expand Down Expand Up @@ -267,9 +266,8 @@ static time_point epoch_to_time_point( achievement::time_bound::epoch e )
abort();
}

void achievement::time_bound::deserialize( JsonIn &jin )
void achievement::time_bound::deserialize( const JsonObject &jo )
{
const JsonObject &jo = jin.get_object();
if( !( jo.read( "since", epoch_ ) &&
jo.read( "is", comparison_ ) &&
jo.read( "target", period_ ) ) ) {
Expand Down Expand Up @@ -637,9 +635,8 @@ void achievement_state::serialize( JsonOut &jsout ) const
jsout.end_object();
}

void achievement_state::deserialize( JsonIn &jsin )
void achievement_state::deserialize( const JsonObject &jo )
{
JsonObject jo = jsin.get_object();
jo.read( "completion", completion );
jo.read( "last_state_change", last_state_change );
jo.read( "final_values", final_values );
Expand Down Expand Up @@ -879,9 +876,8 @@ void achievements_tracker::serialize( JsonOut &jsout ) const
jsout.end_object();
}

void achievements_tracker::deserialize( JsonIn &jsin )
void achievements_tracker::deserialize( const JsonObject &jo )
{
JsonObject jo = jsin.get_object();
if( !jo.read( "enabled", enabled_ ) ) {
enabled_ = true;
}
Expand Down
6 changes: 3 additions & 3 deletions src/achievement.h
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ class achievement
last
};

void deserialize( JsonIn & );
void deserialize( const JsonObject &jo );
void check( const achievement_id & ) const;

time_point target() const;
Expand Down Expand Up @@ -146,7 +146,7 @@ struct achievement_state {
std::string ui_text( const achievement * ) const;

void serialize( JsonOut & ) const;
void deserialize( JsonIn & );
void deserialize( const JsonObject &jo );
};

class achievement_tracker
Expand Down Expand Up @@ -215,7 +215,7 @@ class achievements_tracker : public event_subscriber
void notify( const cata::event & ) override;

void serialize( JsonOut & ) const;
void deserialize( JsonIn & );
void deserialize( const JsonObject &jo );
private:
void init_watchers();

Expand Down
3 changes: 1 addition & 2 deletions src/activity_actor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3282,9 +3282,8 @@ void drop_or_stash_item_info::serialize( JsonOut &jsout ) const
jsout.end_object();
}

void drop_or_stash_item_info::deserialize( JsonIn &jsin )
void drop_or_stash_item_info::deserialize( const JsonObject &jsobj )
{
JsonObject jsobj = jsin.get_object();
jsobj.read( "loc", _loc );
jsobj.read( "count", _count );
}
Expand Down
2 changes: 1 addition & 1 deletion src/activity_actor_definitions.h
Original file line number Diff line number Diff line change
Expand Up @@ -991,7 +991,7 @@ class drop_or_stash_item_info
: _loc( _loc ), _count( _count ) {}

void serialize( JsonOut &jsout ) const;
void deserialize( JsonIn &jsin );
void deserialize( const JsonObject &jsobj );

const item_location &loc() const {
return _loc;
Expand Down
3 changes: 2 additions & 1 deletion src/activity_tracker.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "calendar.h"

class JsonIn;
class JsonObject;
class JsonOut;

class activity_tracker
Expand Down Expand Up @@ -48,7 +49,7 @@ class activity_tracker
std::string debug_weary_info() const;

void serialize( JsonOut &json ) const;
void deserialize( JsonIn &jsin );
void deserialize( const JsonObject &jo );
};

#endif // CATA_SRC_ACTIVITY_TRACKER_H
5 changes: 2 additions & 3 deletions src/auto_pickup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -817,9 +817,8 @@ void rule_list::serialize( JsonOut &jsout ) const
jsout.end_array();
}

void rule::deserialize( JsonIn &jsin )
void rule::deserialize( const JsonObject &jo )
{
JsonObject jo = jsin.get_object();
sRule = jo.get_string( "rule" );
bActive = jo.get_bool( "active" );
bExclude = jo.get_bool( "exclude" );
Expand All @@ -832,7 +831,7 @@ void rule_list::deserialize( JsonIn &jsin )
jsin.start_array();
while( !jsin.end_array() ) {
rule tmp;
tmp.deserialize( jsin );
tmp.deserialize( jsin.get_object() );
push_back( tmp );
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/auto_pickup.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include "enums.h"

class JsonIn;
class JsonObject;
class JsonOut;
class item;
struct itype;
Expand Down Expand Up @@ -51,7 +52,7 @@ class rule
}

void serialize( JsonOut &jsout ) const;
void deserialize( JsonIn &jsin );
void deserialize( const JsonObject &jo );

void test_pattern() const;
};
Expand Down
2 changes: 1 addition & 1 deletion src/avatar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1449,7 +1449,7 @@ void avatar::daily_calories::save_activity( JsonOut &json ) const
json.end_array();
}

void avatar::daily_calories::read_activity( JsonObject &data )
void avatar::daily_calories::read_activity( const JsonObject &data )
{
if( data.has_array( "activity" ) ) {
double act_level;
Expand Down
8 changes: 3 additions & 5 deletions src/avatar.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ class avatar : public Character
void store( JsonOut &json ) const;
void load( const JsonObject &data );
void serialize( JsonOut &json ) const override;
void deserialize( JsonIn &jsin ) override;
void deserialize( const JsonObject &data ) override;
bool save_map_memory();
void load_map_memory();

Expand Down Expand Up @@ -280,9 +280,7 @@ class avatar : public Character

json.end_object();
}
void deserialize( JsonIn &jsin ) {
JsonObject data = jsin.get_object();

void deserialize( const JsonObject &data ) {
data.read( "spent", spent );
data.read( "gained", gained );
if( data.has_member( "activity" ) ) {
Expand All @@ -300,7 +298,7 @@ class avatar : public Character
}

void save_activity( JsonOut &json ) const;
void read_activity( JsonObject &data );
void read_activity( const JsonObject &data );

};
// called once a day; adds a new daily_calories to the
Expand Down
4 changes: 2 additions & 2 deletions src/basecamp.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@
#include "translations.h"
#include "type_id.h"

class JsonIn;
class JsonOut;
class JsonObject;
class character_id;
class npc;
class time_duration;
Expand Down Expand Up @@ -337,7 +337,7 @@ class basecamp
std::vector<npc_ptr> get_npcs_assigned();
// Save/load
void serialize( JsonOut &json ) const;
void deserialize( JsonIn &jsin );
void deserialize( const JsonObject &data );
void load_data( const std::string &data );

static constexpr int inv_range = 20;
Expand Down
3 changes: 1 addition & 2 deletions src/bionics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3142,9 +3142,8 @@ void bionic::serialize( JsonOut &json ) const
json.end_object();
}

void bionic::deserialize( JsonIn &jsin )
void bionic::deserialize( const JsonObject &jo )
{
JsonObject jo = jsin.get_object();
id = bionic_id( jo.get_string( "id" ) );
invlet = jo.get_int( "invlet" );
powered = jo.get_bool( "powered" );
Expand Down
3 changes: 1 addition & 2 deletions src/bionics.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
#include "value_ptr.h"

class Character;
class JsonIn;
class JsonObject;
class JsonOut;

Expand Down Expand Up @@ -221,7 +220,7 @@ struct bionic {
bool activate_spell( Character &caster );

void serialize( JsonOut &json ) const;
void deserialize( JsonIn &jsin );
void deserialize( const JsonObject &jo );
private:
// generic bionic specific flags
cata::flat_set<std::string> bionic_tags;
Expand Down
6 changes: 2 additions & 4 deletions src/bodypart.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -711,9 +711,8 @@ void bodypart::serialize( JsonOut &json ) const
json.end_object();
}

void bodypart::deserialize( JsonIn &jsin )
void bodypart::deserialize( const JsonObject &jo )
{
JsonObject jo = jsin.get_object();
jo.read( "id", id, true );
jo.read( "hp_cur", hp_cur, true );
jo.read( "hp_max", hp_max, true );
Expand All @@ -738,8 +737,7 @@ void stat_hp_mods::load( const JsonObject &jsobj )
optional( jsobj, was_loaded, "health_mod", health_mod, 0.0f );
}

void stat_hp_mods::deserialize( JsonIn &jsin )
void stat_hp_mods::deserialize( const JsonObject &jo )
{
const JsonObject &jo = jsin.get_object();
load( jo );
}
10 changes: 5 additions & 5 deletions src/bodypart.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@
#include "string_id.h"
#include "translations.h"

class JsonIn;
class JsonObject;
class JsonOut;
class JsonValue;
struct body_part_type;
template <typename E> struct enum_traits;

Expand Down Expand Up @@ -102,7 +102,7 @@ struct stat_hp_mods {

bool was_loaded = false;
void load( const JsonObject &jsobj );
void deserialize( JsonIn &jsin );
void deserialize( const JsonObject &jo );
};

struct body_part_type {
Expand Down Expand Up @@ -372,7 +372,7 @@ class bodypart
void mod_frostbite_timer( int mod );

void serialize( JsonOut &json ) const;
void deserialize( JsonIn &jsin );
void deserialize( const JsonObject &jo );
};

class body_part_set
Expand Down Expand Up @@ -433,8 +433,8 @@ class body_part_set
void serialize( Stream &s ) const {
s.write( parts );
}
template<typename Stream = JsonIn, std::enable_if_t<std::is_same<std::decay_t<Stream>, JsonIn>::value>* = nullptr>
void deserialize( Stream &s ) {
template<typename Value = JsonValue, std::enable_if_t<std::is_same<std::decay_t<Value>, JsonValue>::value>* = nullptr>
void deserialize( const Value &s ) {
s.read( parts );
}
};
Expand Down
5 changes: 3 additions & 2 deletions src/calendar.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

class JsonIn;
class JsonOut;
class JsonValue;
struct lat_long;
struct rl_vec2d;
class time_duration;
Expand Down Expand Up @@ -185,7 +186,7 @@ class time_duration
time_duration() : turns_( 0 ) {}

void serialize( JsonOut &jsout ) const;
void deserialize( JsonIn &jsin );
void deserialize( const JsonValue &jv );

/**
* Named constructors to get a duration representing a multiple of the named time
Expand Down Expand Up @@ -454,7 +455,7 @@ class time_point
}

void serialize( JsonOut &jsout ) const;
void deserialize( JsonIn &jsin );
void deserialize( int );

// TODO: try to get rid of this
template<typename T>
Expand Down
14 changes: 13 additions & 1 deletion src/cata_utility.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
#include <utility>
#include <vector>

#include "json.h"

class JsonIn;
class JsonOut;
class translation;
Expand Down Expand Up @@ -390,13 +392,23 @@ inline std::string serialize( const T &obj )
} );
}

template<typename T>
template < typename T, std::enable_if_t < detail::IsJsonInDeserializable<T>::value &&
!detail::IsJsonValueDeserializable<T>::value > * = nullptr >
inline void deserialize_from_string( T &obj, const std::string &data )
{
deserialize_wrapper( [&obj]( JsonIn & jsin ) {
obj.deserialize( jsin );
}, data );
}

template < typename T, std::enable_if_t < !detail::IsJsonInDeserializable<T>::value &&
detail::IsJsonValueDeserializable<T>::value > * = nullptr >
inline void deserialize_from_string( T &obj, const std::string &data )
{
deserialize_wrapper( [&obj]( JsonIn & jsin ) {
obj.deserialize( jsin.get_value() );
}, data );
}
/**@}*/

/**
Expand Down
6 changes: 3 additions & 3 deletions src/character.h
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ struct consumption_event {
component_hash = food.make_component_hash();
}
void serialize( JsonOut &json ) const;
void deserialize( JsonIn &jsin );
void deserialize( const JsonObject &jo );
};

struct stat_mod {
Expand Down Expand Up @@ -369,7 +369,7 @@ class Character : public Creature, public visitable
return false; // Overloaded for NPCs in npc.h
}
// populate variables, inventory items, and misc from json object
virtual void deserialize( JsonIn &jsin ) = 0;
virtual void deserialize( const JsonObject &jsobj ) = 0;

// by default save all contained info
virtual void serialize( JsonOut &jsout ) const = 0;
Expand Down Expand Up @@ -3089,7 +3089,7 @@ class Character : public Creature, public visitable
*/
int charge = 0;
void serialize( JsonOut &json ) const;
void deserialize( JsonIn &jsin );
void deserialize( const JsonObject &data );
};

/** Bonuses to stats, calculated each turn */
Expand Down
3 changes: 1 addition & 2 deletions src/character_id.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

#include <iosfwd>

class JsonIn;
class JsonOut;

class character_id
Expand All @@ -29,7 +28,7 @@ class character_id
}

void serialize( JsonOut & ) const;
void deserialize( JsonIn & );
void deserialize( int );

private:
int value;
Expand Down
Loading

0 comments on commit 6e14a2b

Please sign in to comment.