コード例 #1
0
ファイル: init.cpp プロジェクト: 2birdie/Cataclysm-DDA
void DynamicDataLoader::load_all_from_json(JsonIn &jsin)
{
    char ch;
    jsin.eat_whitespace();
    // examine first non-whitespace char
    ch = jsin.peek();
    if (ch == '{') {
        // find type and dispatch single object
        JsonObject jo = jsin.get_object();
        load_object(jo);
        jo.finish();
        // if there's anything else in the file, it's an error.
        jsin.eat_whitespace();
        if (jsin.good()) {
            jsin.error( string_format( "expected single-object file but found '%c'", jsin.peek() ) );
        }
    } else if (ch == '[') {
        jsin.start_array();
        // find type and dispatch each object until array close
        while (!jsin.end_array()) {
            jsin.eat_whitespace();
            ch = jsin.peek();
            if (ch != '{') {
                jsin.error( string_format( "expected array of objects but found '%c', not '{'", ch ) );
            }
            JsonObject jo = jsin.get_object();
            load_object(jo);
            jo.finish();
        }
    } else {
        // not an object or an array?
        jsin.error( string_format( "expected object or array, but found '%c'", ch ) );
    }
}
コード例 #2
0
ファイル: init.cpp プロジェクト: Barhandar/Cataclysm-DDA
void DynamicDataLoader::load_all_from_json( JsonIn &jsin, const std::string &src, loading_ui &,
        const std::string &base_path, const std::string &full_path )
{
    if( jsin.test_object() ) {
        // find type and dispatch single object
        JsonObject jo = jsin.get_object();
        load_object( jo, src, base_path, full_path );
        jo.finish();
        // if there's anything else in the file, it's an error.
        jsin.eat_whitespace();
        if( jsin.good() ) {
            jsin.error( string_format( "expected single-object file but found '%c'", jsin.peek() ) );
        }
    } else if( jsin.test_array() ) {
        jsin.start_array();
        // find type and dispatch each object until array close
        while( !jsin.end_array() ) {
            JsonObject jo = jsin.get_object();
            load_object( jo, src, base_path, full_path );
            jo.finish();
        }
    } else {
        // not an object or an array?
        jsin.error( "expected object or array" );
    }
}
コード例 #3
0
ファイル: item_group.cpp プロジェクト: Bubbadoo/Cataclysm-DDA
Group_tag item_group::load_item_group( JsonIn& stream, const std::string& default_subtype )
{
    if( stream.test_string() ) {
        return stream.get_string();
    } else if( stream.test_object() ) {
        const Group_tag group = get_unique_group_id();

        JsonObject jo = stream.get_object();
        const std::string subtype = jo.get_string( "subtype", default_subtype );
        item_controller->load_item_group( jo, group, subtype );

        return group;
    } else if( stream.test_array() ) {
        const Group_tag group = get_unique_group_id();

        JsonArray jarr = stream.get_array();
        // load_item_group needs a bool, invalid subtypes are unexpected and most likely errors
        // from the caller of this function.
        if( default_subtype != "collection" && default_subtype != "distribution" ) {
            debugmsg( "invalid subtype for item group: %s", default_subtype.c_str() );
        }
        item_controller->load_item_group( jarr, group, default_subtype == "collection" );

        return group;
    } else {
        stream.error( "invalid item group, must be string (group id) or object/array (the group data)" );
        // stream.error always throws, this is here to prevent a warning
        return Group_tag{};
    }
}
コード例 #4
0
Trait_group_tag trait_group::load_trait_group( JsonIn &stream, const std::string &default_subtype )
{
    if( stream.test_string() ) {
        return Trait_group_tag( stream.get_string() );
    } else if( stream.test_object() ) {
        const Trait_group_tag group = get_unique_trait_group_id();

        JsonObject jo = stream.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() ) {
        const Trait_group_tag group = get_unique_trait_group_id();

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

        mutation_branch::load_trait_group( jarr, group, default_subtype == "collection" );
        return group;
    } else {
        stream.error( "invalid trait group, must be string (group id) or object/array (the group data)" );
        return Trait_group_tag{};
    }
}
コード例 #5
0
ファイル: chkjson.cpp プロジェクト: 1942rob/Cataclysm-DDA
void load_all_from_json(JsonIn &jsin)
{
    char ch;
    std::string type = "";
    jsin.eat_whitespace();
    // examine first non-whitespace char
    ch = jsin.peek();
    if (ch == '{') {
        // find type and dispatch single object
        JsonObject jo = jsin.get_object();
        load_object(jo);
        jo.finish();
        // if there's anything else in the file, it's an error.
        jsin.eat_whitespace();
        if (jsin.good()) {
            std::stringstream err;
            err << "expected single-object file but found '";
            err << jsin.peek() << "'";
            jsin.error( err.str() );
        }
    } else if (ch == '[') {
        jsin.start_array();
        // find type and dispatch each object until array close
        while (!jsin.end_array()) {
            jsin.eat_whitespace();
            ch = jsin.peek();
            if (ch != '{') {
                std::stringstream err;
                err << "expected array of objects but found '";
                err << ch << "', not '{'";
                jsin.error( err.str() );
            }
            JsonObject jo = jsin.get_object();
            load_object(jo);
            jo.finish();
        }
    } else {
        // not an object or an array?
        std::stringstream err;
        err << "expected object or array, but found '" << ch << "'";
        jsin.error( err.str() );
    }
}
コード例 #6
0
ファイル: mapdata.cpp プロジェクト: 1942rob/Cataclysm-DDA
long string_to_symbol( JsonIn &js )
{
    const std::string s = js.get_string();
    if( s == "LINE_XOXO" ) {
        return LINE_XOXO;
    } else if( s == "LINE_OXOX" ) {
        return LINE_OXOX;
    } else if( s.length() != 1 ) {
        js.error( "Symbol string must be exactly 1 character long." );
    }
    return s[0];
}
コード例 #7
0
ファイル: json.cpp プロジェクト: Daidalon/Cataclysm-DDA
/* class JsonObject
 * represents a JSON object,
 * providing access to the underlying data.
 */
JsonObject::JsonObject(JsonIn &j) : positions()
{
    jsin = &j;
    start = jsin->tell();
    // cache the position of the value for each member
    jsin->start_object();
    while (!jsin->end_object()) {
        std::string n = jsin->get_member_name();
        int p = jsin->tell();
        if (n != "//" && n != "comment" && positions.count(n) > 0) {
            // members with name "//" or "comment" are used for comments and
            // should be ignored anyway.
            j.error("duplicate entry in json object");
        }
        positions[n] = p;
        jsin->skip_value();
    }
    end = jsin->tell();
    final_separator = jsin->get_ate_separator();
}