コード例 #1
0
ファイル: jsonc_parser.cpp プロジェクト: Debug-Orz/hhvm
bool JSON_parser(Variant &return_value, const char *data, int data_len,
                 bool assoc, int depth, int64_t options) {
    json_tokener *tok;
    json_object *new_obj;
    bool retval = false;

#if JSON_C_MINOR_VERSION >= 11
    tok = json_tokener_new_ex(depth);
#else
    tok = json_tokener_new();
#endif
    if (!tok) {
        return retval;
    }

    //if (!(options & k_JSON_FB_LOOSE)) {
    //    json_tokener_set_flags(tok, JSON_TOKENER_STRICT);
    //}

    bool const stable_maps = options & k_JSON_FB_STABLE_MAPS;
    bool const collections = stable_maps || options & k_JSON_FB_COLLECTIONS;

    new_obj = json_tokener_parse_ex(tok, data, data_len);
    if (json_tokener_get_error(tok)==json_tokener_continue) {
        new_obj = json_tokener_parse_ex(tok, "", -1);
    }

    if (new_obj) {
        return_value = json_object_to_variant(new_obj, assoc, stable_maps,
                                              collections);
        json_object_put(new_obj);
        retval = true;
    } else {
        switch (json_tokener_get_error(tok)) {
        case json_tokener_success:
            retval = true;
            break;

        case json_tokener_error_depth:
            json_set_last_error_code(json_error_codes::JSON_ERROR_DEPTH);
            break;

        default:
            json_set_last_error_code(json_error_codes::JSON_ERROR_SYNTAX,
                                     json_tokener_get_error(tok));
        }
    }

    json_tokener_free(tok);
    return retval;
}
コード例 #2
0
ファイル: JSON.cpp プロジェクト: code-iai/semrec
  void JSON::parse(std::string strJSON, std::string strMimeType) {
    if(strMimeType == "") {
      strMimeType = "application/json";
    }
    
    if(!m_prRootProperty) {
      delete m_prRootProperty;
    }
    
    m_prRootProperty = new Property("root", Property::Object);
    
    if(strMimeType == "application/json") {
      struct json_tokener* tok;
      struct json_object* jobj;
      enum json_tokener_error jteError;
      
      tok = json_tokener_new_ex(1000);
      
      if(!tok) {
	std::cerr << "Couldn't initialize json_tokener." << std::endl;
      } else {
	jobj = json_tokener_parse_ex(tok, strJSON.c_str(), strJSON.length());
	jteError = tok->err;
	
	if(jteError == json_tokener_success) {
	  if(jobj != NULL) {
	    this->parse(jobj, m_prRootProperty);
	  } else {
	    std::cerr << "Failed to parse JSON: " << json_tokener_error_desc(jteError) << std::endl;
	  }
	  
	  jobj = NULL;
	} else {
	  std::cerr << "Failed to parse JSON: " << json_tokener_error_desc(jteError) << std::endl;
	}
	
	json_tokener_free(tok);
      }
    } else {
      this->parseXML(strJSON);
    }
  }
コード例 #3
0
ファイル: cpl_json.cpp プロジェクト: ksshannon/gdal
bool CPLJSONDocument::LoadUrl(const std::string & /*osUrl*/, char ** /*papszOptions*/,
                              GDALProgressFunc /*pfnProgress*/,
                              void * /*pProgressArg*/)
#endif // HAVE_CURL
{
#ifdef HAVE_CURL
    int nDepth = atoi( CSLFetchNameValueDef( papszOptions, "JSON_DEPTH", "10") );
    JsonContext ctx = { nullptr, json_tokener_new_ex(nDepth), 0 };

    CPLHTTPFetchWriteFunc pWriteFunc = CPLJSONWriteFunction;
    CPLHTTPResult *psResult = CPLHTTPFetchEx( osUrl.c_str(), papszOptions,
                                              pfnProgress, pProgressArg,
                                              pWriteFunc, &ctx );

    bool bResult = true;
    if( psResult->nStatus != 0 /*CURLE_OK*/ )
    {
        bResult = false;
    }

    CPLHTTPDestroyResult( psResult );

    enum json_tokener_error jerr;
    if ((jerr = json_tokener_get_error(ctx.pTokener)) != json_tokener_success) {
        CPLError(CE_Failure, CPLE_AppDefined, "JSON error: %s\n",
               json_tokener_error_desc(jerr));
        bResult = false;
    }
    else {
        if( m_poRootJsonObject )
            json_object_put( TO_JSONOBJ(m_poRootJsonObject) );

        m_poRootJsonObject = ctx.pObject;
    }
    json_tokener_free(ctx.pTokener);

    return bResult;
#else
    return false;
#endif
}