Skip to content

Commit

Permalink
Change trait_group::load_trait_group to accept a JsonValue
Browse files Browse the repository at this point in the history
Which can be used just like the stream.

Also added a `JsonObject::get_member` function to get a specific member as `JsonValue`.
  • Loading branch information
BevapDin committed Dec 18, 2019
1 parent 5e2088f commit d088831
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 20 deletions.
9 changes: 9 additions & 0 deletions src/json.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1885,3 +1885,12 @@ JsonIn &JsonValue::seek() const
jsin_.seek( pos_ );
return jsin_;
}

JsonValue JsonObject::get_member( const std::string &name ) const {
const auto iter = positions.find(name);
if(!jsin || iter == positions.end()) {
throw_error("requested non-existing member \"" + name + "\"");
}
visited_members.insert( name );
return JsonValue( *jsin, iter->second );
}
4 changes: 2 additions & 2 deletions src/json.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ class JsonObject;
class JsonArray;
class JsonSerializer;
class JsonDeserializer;
class JsonValue;

template<typename T>
class string_id;
Expand Down Expand Up @@ -836,6 +837,7 @@ class JsonObject
[[noreturn]] void throw_error( std::string err, const std::string &name ) const;
// seek to a value and return a pointer to the JsonIn (member must exist)
JsonIn *get_raw( const std::string &name ) const;
JsonValue get_member( const std::string &name ) const;

// values by name
// variants with no fallback throw an error if the name is not found.
Expand Down Expand Up @@ -910,8 +912,6 @@ class JsonObject
std::string line_number() const; // for occasional use only
};

class JsonValue;

/* JsonArray
* =========
*
Expand Down
3 changes: 1 addition & 2 deletions src/mutation.h
Original file line number Diff line number Diff line change
Expand Up @@ -355,8 +355,7 @@ struct mutation_branch {
* Note that each entry in the array has to be a JSON object. The other function above
* can also load data from arrays of strings, where the strings are item or group ids.
*/
static void load_trait_group( JsonArray &entries, const trait_group::Trait_group_tag &gid,
bool is_collection );
static void load_trait_group( const JsonArray &entries, const trait_group::Trait_group_tag &gid, bool is_collection );

/**
* Create a new trait group as specified by the given JSON object and register
Expand Down
4 changes: 2 additions & 2 deletions src/npc_class.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -247,8 +247,8 @@ void npc_class::load( const JsonObject &jo, const std::string & )
optional( jo, was_loaded, "carry_override", carry_override );
optional( jo, was_loaded, "weapon_override", weapon_override );

if( jo.has_array( "traits" ) ) {
traits = trait_group::load_trait_group( *jo.get_raw( "traits" ), "collection" );
if( jo.has_member( "traits" ) ) {
traits = trait_group::load_trait_group( jo.get_member( "traits" ), "collection" );
}

if( jo.has_array( "spells" ) ) {
Expand Down
18 changes: 9 additions & 9 deletions src/trait_group.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,30 +50,30 @@ static Trait_group_tag get_unique_trait_group_id()
}
}

Trait_group_tag trait_group::load_trait_group( JsonIn &stream, const std::string &default_subtype )
Trait_group_tag trait_group::load_trait_group( const JsonValue &value, const std::string &default_subtype )
{
if( stream.test_string() ) {
return Trait_group_tag( stream.get_string() );
} else if( stream.test_object() ) {
if( value.test_string() ) {
return Trait_group_tag( value.get_string() );
} else if( value.test_object() ) {
const Trait_group_tag group = get_unique_trait_group_id();

JsonObject jo = stream.get_object();
JsonObject jo = value.get_object();
const std::string subtype = jo.get_string( "subtype", default_subtype );

mutation_branch::load_trait_group( jo, group, subtype );

return group;
} else if( stream.test_array() ) {
} else if( value.test_array() ) {
const Trait_group_tag group = get_unique_trait_group_id();

if( default_subtype != "collection" && default_subtype != "distribution" ) {
stream.throw_error( "invalid subtype for trait group" );
value.throw_error( "invalid subtype for trait group" );
}

mutation_branch::load_trait_group( stream.get_array(), group, default_subtype == "collection" );
mutation_branch::load_trait_group( value.get_array(), group, default_subtype == "collection" );
return group;
} else {
stream.error( "invalid trait group, must be string (group id) or object/array (the group data)" );
value.throw_error( "invalid trait group, must be string (group id) or object/array (the group data)" );
return Trait_group_tag{};
}
}
Expand Down
10 changes: 5 additions & 5 deletions src/trait_group.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
class JsonObject;
class JsonIn;
class Trait_group;
class JsonValue;

namespace trait_group
{
Expand All @@ -38,23 +39,22 @@ void load_trait_group( const JsonObject &jsobj, const Trait_group_tag &gid,
/**
* Get a trait group ID and optionally load an inlined trait group.
*
* If the next value in the JSON stream is string, it's assumed to be a trait group id and it's
* If the value is string, it's assumed to be a trait group id and it's
* returned directly.
*
* If the next value is a JSON object, it is loaded as a trait group. The group will be given a
* If the value is a JSON object, it is loaded as a trait group. The group will be given a
* unique id (if the JSON object contains an id, it is ignored) and that id will be returned.
* If the JSON object does not contain a subtype, the given default is used.
*
* If the next value is a JSON array, it is loaded as a trait group: the default_subtype will be
* If the value is a JSON array, it is loaded as a trait group: the default_subtype will be
* used as subtype of the new trait group and the array is loaded like the "entries" array of
* a trait group definition (see format of trait groups).
*
* @param stream Stream to load from
* @param default_subtype If an inlined trait group is loaded this is used as the default
* subtype. It must be either "distribution" or "collection". See @ref Trait_group.
* @throw JsonError as usual for JSON errors, including invalid input values.
*/
Trait_group_tag load_trait_group( JsonIn &stream, const std::string &default_subtype );
Trait_group_tag load_trait_group( const JsonValue &value, const std::string &default_subtype );

/**
* Show a debug menu for testing trait groups.
Expand Down

0 comments on commit d088831

Please sign in to comment.