error configuration_parser::parse_json_object(
        json_t*                         _obj,
        configuration_parser::object_t& _obj_out ) {

        const char* key = 0;
        json_t*     val = 0;
        json_object_foreach( _obj, key, val ) {
            int type = json_typeof( val );
            if ( JSON_INTEGER == type ) {
                _obj_out.set< int >( key, json_integer_value( val ) );

            }
            else if ( JSON_STRING == type ) {
                _obj_out.set< std::string >( key, json_string_value( val ) );

            }
            else if ( JSON_REAL == type ) {
                _obj_out.set< double >( key, json_real_value( val ) );

            }
            else if ( JSON_TRUE == type ||
                      JSON_FALSE == type )  {
                _obj_out.set< bool >( key, json_boolean( val ) );

            }
            else if ( JSON_NULL == type ) {
                _obj_out.set< std::string >( key, "NULL" );

            }
            else if ( JSON_ARRAY  == type ) {
                array_t arr;
                size_t  idx = 0;
                json_t* obj = 0;
                json_array_foreach( val, idx, obj ) {
                    object_t lt;
                    irods::error err = parse_json_object(
                                           obj,
                                           lt );
                    if ( err.ok() ) {
                        arr.push_back( lt );

                    }
                    else {
                        irods::log( PASS( err ) );

                    }

                } // array foreach
    error configuration_parser::load_json_object(
        const std::string&              _file ) {
        json_error_t error;

        json_t *json = json_load_file( _file.c_str(), 0, &error );
        if ( !json ) {
            std::string msg( "failed to load file [" );
            msg += _file;
            msg += "] json error [";
            msg += error.text;
            msg += "]";
            return ERROR( -1, msg );
        }

        irods::error ret = parse_json_object( json, root_ );
        json_decref( json );

        return ret;

    } // load_json_object
示例#3
0
文件: json.cpp 项目: gaoxiaojun/dync
bool JSON::parse_json_value() {
  int c;

  c = skip_to_token();
  if (c == -1) {
    return false;
  }

  // Must start with object or array
  if (level == 0) {

    switch (c) {
    case '{':
      if (parse_json_object() == false) {
        return false;
      }
      c = skip_to_token();
      if (c > 0) {
        mark_pos();
        error(SYNTAX_ERROR, "Only one top level object/array is allowed.");
        return false;
      } else if (c < 0) {
        return false;
      }
      return true;

    case '[':
      if (parse_json_array() == false) {
        return false;
      }
      c = skip_to_token();
      if (c > 0) {
        mark_pos();
        error(SYNTAX_ERROR, "Only one top level object/array is allowed.");
        return false;
      } else if (c < 0) {
        return false;
      }
      return true;

    case 0:
      error(SYNTAX_ERROR, "EOS was encountered before any json declarations");
      return false;

    default:
      error(SYNTAX_ERROR, "Json must start with an object or an array.");
      return false;
    }
  } else { // level > 0
    switch (c) {
    case '{':
      return parse_json_object();

    case '[':
      return parse_json_array();

    case '"':
      return parse_json_string();

    case '-': case '0':
    case '1': case '2': case '3':
    case '4': case '5': case '6':
    case '7': case '8': case '9':
      return parse_json_number();

    case 't':
      return parse_json_symbol("true", JSON_TRUE);

    case 'f':
      return parse_json_symbol("false", JSON_FALSE);

    case 'n':
      return parse_json_symbol("null", JSON_NULL);

    case 0:
      error(SYNTAX_ERROR, "EOS was encountered when expecting a json value.");
      return false;

    default:
      error(SYNTAX_ERROR, "Could not parse as a json value (did you forget to quote your strings?).");
      return false;
    }
  }
}