Ejemplo n.º 1
0
/* 判断janson的类型 */
int my_json_type(json_t *value)
{
    if(json_is_object(value))
	{
        printf("json_is_object\n");
		return JSON_OBJECT;
	}

    if(json_is_array(value))
	{
        printf("json_is_array\n");
		return JSON_ARRAY;
	}

    if(json_is_string(value))
	{
        printf("json_is_string\n");
		return JSON_STRING;
	}

    if(json_is_integer(value))
	{
        printf("json_is_integer\n");
		return JSON_INTEGER;
	}

    if(json_is_real(value))
	{
        printf("json_is_real\n");
		return JSON_REAL;
	}

    if(json_is_number(value))
	{
        printf("json_is_number\n");
	}

    if(json_is_boolean(value))
	{
        printf("json_is_boolean\n");
	}

    if(json_is_null(value))
	{
        printf("json_is_null\n");
		return JSON_NULL;
	}

	if(json_is_true(value))
	{
        printf("json_boolean(1)\n");
		return JSON_TRUE;
	}

    if(json_is_false(value))
	{
        printf("json_boolean(0)\n");
		return JSON_FALSE;
	}
}
Ejemplo n.º 2
0
static size_t S_write_f( void *ptr, size_t size, size_t nmemb, void *userdata){
  json_t *root;
  json_error_t error;
  size_t len = nmemb*size;

  S_buf = (char *)realloc( S_buf, len + 1 );
  memcpy( (void *)S_buf, ptr, len );
  S_buf[len] = '\0';
  //printf( "Received: %s\n", S_buf );

  root = json_loads(S_buf, &error);
  if(!root){
    fprintf(stderr, "error: on line %d: %s\n", error.line, error.text);
  }
  else{
    CouchServerWorkingMemory::S_Root = root;
    json_t *version;
    version = json_object_get( root, "wm" );
    if( json_is_null( version ) ){
      fprintf( stderr, "Working Memory is NULL!\n" );
    }
    else if( json_is_object( version ) ){
      /*
      char *s = json_dumps( version, 0 );
      printf( "Working Memory:\n%s\n", s );
      free( s );
      */
    }
  }

  free( S_buf );
  S_buf = NULL;

  return len;
}
Ejemplo n.º 3
0
json_t *json_deep_copy(json_t *json)
{
    if(!json)
        return NULL;

    if(json_is_object(json))
        return json_object_deep_copy(json);

    if(json_is_array(json))
        return json_array_deep_copy(json);

    /* for the rest of the types, deep copying doesn't differ from
       shallow copying */

    if(json_is_string(json))
        return json_string_copy(json);

    if(json_is_integer(json))
        return json_integer_copy(json);

    if(json_is_real(json))
        return json_real_copy(json);

    if(json_is_true(json) || json_is_false(json) || json_is_null(json))
        return json;

    return NULL;
}
Ejemplo n.º 4
0
json_t *json_copy(json_t *json)
{
    if(!json)
        return NULL;

    if(json_is_object(json))
        return json_object_copy(json);

    if(json_is_array(json))
        return json_array_copy(json);

    if(json_is_string(json))
        return json_string_copy(json);

    if(json_is_integer(json))
        return json_integer_copy(json);

    if(json_is_real(json))
        return json_real_copy(json);

    if(json_is_true(json) || json_is_false(json) || json_is_null(json))
        return json;

    return NULL;
}
Ejemplo n.º 5
0
static void decode_any()
{
    json_t *json;
    json_error_t error;

    json = json_loads("\"foo\"", JSON_DECODE_ANY, &error);
    if (!json || !json_is_string(json))
        fail("json_load decoded any failed - string");
    json_decref(json);

    json = json_loads("42", JSON_DECODE_ANY, &error);
    if (!json || !json_is_integer(json))
        fail("json_load decoded any failed - integer");
    json_decref(json);

    json = json_loads("true", JSON_DECODE_ANY, &error);
    if (!json || !json_is_true(json))
        fail("json_load decoded any failed - boolean");
    json_decref(json);

    json = json_loads("null", JSON_DECODE_ANY, &error);
    if (!json || !json_is_null(json))
        fail("json_load decoded any failed - null");
    json_decref(json);
}
Ejemplo n.º 6
0
int
jsonUnpackOoiRespStr( json_t *responseObj, char **outStr ) {
    const char *responseStr;
    int status;

    if ( !json_is_string( responseObj ) ) {
        if ( json_is_null( responseObj ) ) {
            responseStr = "";
        }
        else {
            rodsLog( LOG_ERROR,
                     "jsonUnpackOoiRespStr: responseObj type %d is not JSON_STRING.",
                     json_typeof( responseObj ) );
            return OOI_JSON_TYPE_ERR;
        }
    }
    else {
        responseStr = json_string_value( responseObj );
    }

    if ( responseStr != NULL ) {
        *outStr = strdup( responseStr );
        status = 0;
    }
    else {
        status = OOI_JSON_NO_ANSWER_ERR;
    }
    return status;
}
Ejemplo n.º 7
0
static bool get_bool_orelse(json_t* json, const char* key, bool def) {
    json_t* v = json_object_get(json, key);
    if (v == NULL)
	return def;
    if (json_is_null(v))
	return def;
    return json_is_true(v) ? true : false;
}
Ejemplo n.º 8
0
string SpaceMessage::toString() {

	string reply = "";
	if (!json_is_null(root)) {
		char* buf = json_dumps(root, 0);
		reply.append(buf);
		free(buf);
	}
	return reply;
}
static char *ngx_http_acme_json_request(ngx_conf_t *cf, void *conf, char *url, ngx_http_acme_http_method_t http_method,
        json_t *request_json, json_t **response_json, ngx_http_acme_slist_t **response_headers)
{
    ngx_str_t response_data;
    ngx_str_t request_data;
    char *tmp;

    json_error_t error;

    /* Convert request_json to string to provide it to the following method */
    request_data = (ngx_str_t)ngx_null_string;
    if(!json_is_null(request_json)) {
        tmp = json_dumps(request_json, 0);
        if(tmp == NULL) {
            ngx_log_error(NGX_LOG_ERR, cf->log, 0, "Error while creating request string from JSON\n");
            return NGX_CONF_ERROR;
        } else {
            request_data.data = (u_char *)tmp;
            request_data.len = ngx_strlen(tmp);
        }
    }

    /* Make request */
    if(ngx_http_acme_plain_request(cf, conf, url, http_method, request_data, &response_data, response_headers) != NGX_CONF_OK) {
        ngx_log_error(NGX_LOG_ERR, cf->log, 0, "Error while making request\n");
        return NGX_CONF_ERROR;
    }

    /* Now all the returned JSON is in the data variable */

    /*
     * Parsing returned JSON
     */

    /* Begin Jansson part */

    if(response_data.len <= 0) {
        *response_json = json_null();
    } else {
        *response_json = json_loadb((char *) response_data.data, response_data.len, 0, &error);
    }
    free(response_data.data);
    ngx_str_null(&response_data);

    if(*response_json == NULL)
    {
        ngx_log_error(NGX_LOG_ERR, cf->log, 0,
                "Error parsing JSON: on line %d: %s\n", error.line, error.text);
        return NGX_CONF_ERROR;
    }

    /* End Jansson part */

    return NGX_CONF_OK;
} /* ngx_http_acme_json_request */
Ejemplo n.º 10
0
int jansson_to_val(pv_value_t* val, char** freeme, json_t* v) {

	val->flags = 0;

	if(json_is_object(v) || json_is_array(v)) {
		const char* value = json_dumps(v, JSON_COMPACT|JSON_PRESERVE_ORDER);
		*freeme = (char*)value;
		val->rs.s = (char*)value;
		val->rs.len = strlen(value);
		val->flags = PV_VAL_STR;
	}else if(json_is_string(v)) {
		const char* value = json_string_value(v);
		val->rs.s = (char*)value;
		val->rs.len = strlen(value);
		val->flags = PV_VAL_STR;
	}else if(json_is_boolean(v)) {
		val->ri = json_is_true(v) ? 0 : 1;
		val->flags = PV_TYPE_INT|PV_VAL_INT;
	}else if(json_is_real(v)) {
		char* value = NULL;
		if(asprintf(&value, "%.15g", json_real_value(v))<0) {
			ERR("asprintf failed\n");
			return -1;
		}
		*freeme = value;
		val->rs.s = value;
		val->rs.len = strlen(value);
		val->flags = PV_VAL_STR;
	}else if(json_is_integer(v)) {
		long long value = json_integer_value(v);
		if ((value > INT_MAX) || (value < INT_MIN))  {
			char* svalue = NULL;
			if (asprintf(&svalue, "%"JSON_INTEGER_FORMAT, value) < 0) {
				ERR("asprintf failed\n");
				return -1;
			}
			*freeme = svalue;
			val->rs.s = svalue;
			val->rs.len = strlen(svalue);
			val->flags = PV_VAL_STR;
		} else {
			val->ri = (int)value;
			val->flags = PV_TYPE_INT|PV_VAL_INT;
		}
	}else if(json_is_null(v)) {
		val->flags = PV_VAL_NULL;
	}else {
		ERR("unrecognized json type: %d\n", json_typeof(v));
		return -1;
	}
	return 0;
}
Ejemplo n.º 11
0
// -1 Failed to parse
// -2 Buffer too small
static int
pack_shadow_struct(json_t *root, struct spwd *result, char *buffer, size_t buflen)
{

    char *next_buf = buffer;
    size_t bufleft = buflen;

    if (!json_is_object(root)) return -1;

    json_t *j_sp_namp = json_object_get(root, "sp_namp");
    json_t *j_sp_pwdp = json_object_get(root, "sp_pwdp");
    json_t *j_sp_lstchg = json_object_get(root, "sp_lstchg");
    json_t *j_sp_min = json_object_get(root, "sp_min");
    json_t *j_sp_max = json_object_get(root, "sp_max");
    json_t *j_sp_warn = json_object_get(root, "sp_warn");
    json_t *j_sp_inact = json_object_get(root, "sp_inact");
    json_t *j_sp_expire = json_object_get(root, "sp_expire");
    json_t *j_sp_flag = json_object_get(root, "sp_flag");

    if (!json_is_string(j_sp_namp)) return -1;
    if (!json_is_string(j_sp_pwdp)) return -1;
    if (!json_is_integer(j_sp_lstchg)) return -1;
    if (!json_is_integer(j_sp_min)) return -1;
    if (!json_is_integer(j_sp_max)) return -1;
    if (!json_is_integer(j_sp_warn)) return -1;
    if (!json_is_integer(j_sp_inact) && !json_is_null(j_sp_inact)) return -1;
    if (!json_is_integer(j_sp_expire) && !json_is_null(j_sp_expire)) return -1;
    if (!json_is_integer(j_sp_flag) && !json_is_null(j_sp_flag)) return -1;

    memset(buffer, '\0', buflen);

    if (bufleft <= j_strlen(j_sp_namp)) return -2;
    result->sp_namp = strncpy(next_buf, json_string_value(j_sp_namp), bufleft);
    next_buf += strlen(result->sp_namp) + 1;
    bufleft  -= strlen(result->sp_namp) + 1;

    if (bufleft <= j_strlen(j_sp_pwdp)) return -2;
    result->sp_pwdp = strncpy(next_buf, json_string_value(j_sp_pwdp), bufleft);
    next_buf += strlen(result->sp_pwdp) + 1;
    bufleft  -= strlen(result->sp_pwdp) + 1;

    // Yay, ints are so easy!
    result->sp_lstchg = json_integer_value(j_sp_lstchg);
    result->sp_min = json_integer_value(j_sp_min);
    result->sp_max = json_integer_value(j_sp_max);
    result->sp_warn = json_integer_value(j_sp_warn);

    if (!json_is_null(j_sp_inact)) result->sp_inact = json_integer_value(j_sp_inact);
    else result->sp_inact = -1;

    if (!json_is_null(j_sp_expire)) result->sp_expire = json_integer_value(j_sp_expire);
    else result->sp_expire = -1;

    if (!json_is_null(j_sp_flag)) result->sp_flag = json_integer_value(j_sp_flag);
    else result->sp_flag = ~0ul;

    return 0;
}
Ejemplo n.º 12
0
const json_t* JsonReader::getCurrentObject() const {
    const json_t* current = _root.get();
    if (json_is_null(current))
        throw std::logic_error("JsonReader: Reading before parsed");

    vector<string> path = split(getObjPrefix(), getPrefixSeparator());
    for (const string& name : path) {
        //printf("Reading object %s\n", name.c_str());
        current = json_object_get(current, name.c_str());
        if (!current)
            throw std::logic_error("JsonWriter: json_object_get failed");
    }
    return current;
}
static char *ngx_http_acme_request(ngx_conf_t *cf, void *conf, char *url, ngx_http_acme_http_method_t http_method,
        json_t *request_json, RSA *key, ngx_str_t *replay_nonce, json_t **response_json,
        ngx_http_acme_slist_t **response_headers)
{
    json_t *signed_request_json;
    ngx_http_acme_slist_t *header = NULL;

    /* Sign JSON and create JWS from the request JSON data */
    if(json_is_null(request_json)) {
        signed_request_json = json_null();
    } else {
        if(ngx_http_acme_sign_json(cf, conf, request_json, key, *replay_nonce, &signed_request_json) != NGX_CONF_OK) {
            ngx_log_error(NGX_LOG_ERR, cf->log, 0, "Creating JWS failed");
            return NGX_CONF_ERROR;
        }
    }

    /* Make JSON request */
    if(ngx_http_acme_json_request(cf, conf, url, http_method, signed_request_json, response_json, response_headers) != NGX_CONF_OK) {
        ngx_log_error(NGX_LOG_ERR, cf->log, 0, "Error while making JSON request");
        return NGX_CONF_ERROR;
    }

    if(replay_nonce->data != NULL)
        ngx_free(replay_nonce->data);
    ngx_str_null(replay_nonce);
    json_decref(signed_request_json);

    /* Search for and extract replay nonce from response headers */
    for(header = *response_headers; header != NULL; header = header->next) {
        if(header->value_len < strlen(ACME_REPLAY_NONCE_PREFIX_STRING))
            continue;

        if(ngx_strncmp(header->value, ACME_REPLAY_NONCE_PREFIX_STRING, strlen(ACME_REPLAY_NONCE_PREFIX_STRING)) == 0) {
            /* Replay nonce found, extract it */
            replay_nonce->len = header->value_len - strlen(ACME_REPLAY_NONCE_PREFIX_STRING);
            replay_nonce->data = ngx_alloc(replay_nonce->len, cf->log);
            ngx_memcpy(replay_nonce->data, header->value + strlen(ACME_REPLAY_NONCE_PREFIX_STRING), replay_nonce->len);
            break;
        }
    }

    if(header == NULL) {
        ngx_log_error(NGX_LOG_ERR, cf->log, 0, "No Replay-Nonce found in HTTP response headers");
        return NGX_CONF_ERROR;
    }

    return NGX_CONF_OK;
} /* ngx_http_acme_create_jwk */
Ejemplo n.º 14
0
char* json_get_string_key(const json_t* obj, const char* key) {
    json_t* value = json_object_get(obj, key);
    if (value == NULL) {
        fprintf(stderr, "error: missing required \"%s\" key\n", key);
        exit(1);
    }
    if (json_is_null(value)) {
        return NULL;
    }
    if (!json_is_string(value)) {
        fprintf(stderr, "error: \"%s\" key must be a string, got %s\n", key, json_strof(value));
        exit(1);
    }
    return strdup(json_string_value(value));
}
Ejemplo n.º 15
0
static kbool_t IsJsonType(struct JsonBuf *jsonbuf, KJSONTYPE type)
{
	switch(type) {
		case KJSON_OBJECT:   return json_is_object(jsonbuf->jsonobj);
		case KJSON_ARRAY:    return json_is_array(jsonbuf->jsonobj);
		case KJSON_STRING:   return json_is_string(jsonbuf->jsonobj);
		case KJSON_INT:      return json_is_integer(jsonbuf->jsonobj);
		case KJSON_DOUBLE:   return json_is_real(jsonbuf->jsonobj);
		case KJSON_BOOLEAN:  return json_is_boolean(jsonbuf->jsonobj);
		case KJSON_NULL:     return json_is_null(jsonbuf->jsonobj);
		case KJSON_INT64:    return json_is_integer(jsonbuf->jsonobj);
		case KJSON_LONG:     return json_is_integer(jsonbuf->jsonobj);
	}
	return false;
}
Ejemplo n.º 16
0
bool stratum_authorize(struct stratum_ctx *sctx, const char *user, const char *pass)
{
    json_t *val = NULL, *res_val, *err_val;
    char *s, *sret;
    json_error_t err;
    bool ret = false;

    s = (char*)malloc(80 + strlen(user) + strlen(pass));
    sprintf(s, "{\"id\": 2, \"method\": \"mining.authorize\", \"params\": [\"%s\", \"%s\"]}",
            user, pass);

    if (!stratum_send_line(sctx, s))
        goto out;

    while (1) {
        sret = stratum_recv_line(sctx);
        if (!sret)
            goto out;
        if (!stratum_handle_method(sctx, sret))
            break;
        free(sret);
    }

    val = JSON_LOADS(sret, &err);
    free(sret);
    if (!val) {
        applog(LOG_ERR, "JSON decode failed(%d): %s", err.line, err.text);
        goto out;
    }

    res_val = json_object_get(val, "result");
    err_val = json_object_get(val, "error");

    if (!res_val || json_is_false(res_val) ||
            (err_val && !json_is_null(err_val)))  {
        applog(LOG_ERR, "Stratum authentication failed");
        goto out;
    }

    ret = true;

out:
    free(s);
    if (val)
        json_decref(val);

    return ret;
}
Ejemplo n.º 17
0
/* Extracts a string value from a json array with error checking. To be used
 * when the value of the string returned is only examined and not to be stored.
 * See json_array_string below */
const char *__json_array_string(json_t *val, unsigned int entry)
{
	json_t *arr_entry;

	if (json_is_null(val))
		return NULL;
	if (!json_is_array(val))
		return NULL;
	if (entry > json_array_size(val))
		return NULL;
	arr_entry = json_array_get(val, entry);
	if (!json_is_string(arr_entry))
		return NULL;

	return json_string_value(arr_entry);
}
Ejemplo n.º 18
0
//===----------------------------------------------------------------------===//
//
// Set commands
//
//===----------------------------------------------------------------------===//
void CouchWorkingMemory::set_json_t( const char *data, json_t *val ){
  CouchWorkingMemory::S_Root = NULL;
  init();
  if( CouchWorkingMemory::S_Root ){
    json_t *wm = json_object_get( CouchWorkingMemory::S_Root, "wm" );
    if( json_is_null( wm ) ){
      fprintf( stderr, "Eror: Working Memory is NULL!\n" );
    }
    else if( json_is_object( wm ) ){
      json_object_set_new( wm, data, val );
      update();
      json_decref( CouchWorkingMemory::S_Root );
      return;
    }   
  }
  json_decref( CouchWorkingMemory::S_Root );
}
Ejemplo n.º 19
0
static int json_t_to_avro_value_helper(
		const avro_schema_t schema, const json_t *json, avro_datum_t datum)
{
	switch (schema->type) {
		case AVRO_BOOLEAN:
			check_param(EINVAL, json_is_boolean(json), "JSON boolean");
			if (avro_boolean_set(datum, json_is_true(json))) return EINVAL;
			return 0;
		case AVRO_DOUBLE:
			check_param(EINVAL, json_is_real(json), "JSON float");
			if (avro_double_set(datum, json_real_value(json))) return EINVAL;
			return 0;
		case AVRO_FLOAT:
			check_param(EINVAL, json_is_real(json), "JSON float");
			if (avro_float_set(datum, json_real_value(json))) return EINVAL;
			return 0;
		case AVRO_INT32:
			check_param(EINVAL, json_is_integer(json), "JSON integer");
			if (avro_int32_set(datum, json_integer_value(json))) return EINVAL;
			return 0;
		case AVRO_INT64:
			check_param(EINVAL, json_is_integer(json), "JSON integer");
			if (avro_int64_set(datum, json_integer_value(json))) return EINVAL;
			return 0;
		case AVRO_NULL:
			check_param(EINVAL, json_is_null(json), "JSON null");
			return 0;
		case AVRO_STRING: {
			check_param(EINVAL, json_is_string(json), "JSON string");
			if (avro_string_set(datum, json_string_value(json))) return EINVAL;
			return 0;
		}
		case AVRO_BYTES:
		case AVRO_ARRAY:
		case AVRO_ENUM:
		case AVRO_FIXED:
		case AVRO_MAP:
		case AVRO_RECORD:
		case AVRO_UNION:
		default:
			// TODO
			avro_set_error("json_t_to_avro_value: unimplemented type");
			return EINVAL;
	}
}
Ejemplo n.º 20
0
static bool stratum_get_version(struct stratum_ctx *sctx, json_t *id)
{
	char *s;
	json_t *val;
	bool ret;
	
	if (!id || json_is_null(id))
		return false;

	val = json_object();
	json_object_set(val, "id", id);
	json_object_set_new(val, "error", json_null());
	json_object_set_new(val, "result", json_string(USER_AGENT));
	s = json_dumps(val, 0);
	ret = stratum_send_line(sctx, s);
	json_decref(val);
	free(s);

	return ret;
}
Ejemplo n.º 21
0
json_t *CouchWorkingMemory::get_json_t( char *data ){
  CouchWorkingMemory::S_Root = NULL;
  init();
  if( CouchWorkingMemory::S_Root ){
    json_t *wm = json_object_get( CouchWorkingMemory::S_Root, "wm" );
    json_t *sign;
    if( json_is_null( wm ) ){
      fprintf( stderr, "Eror: Working Memory is NULL!\n" );
    }
    else if( json_is_object( wm ) ){
      sign = json_object_get( wm, data );
      if( sign ){
	//	json_decref( CouchWorkingMemory::S_Root );
	return sign;
      }
    }   
  }
  //json_decref( CouchWorkingMemory::S_Root );
  return NULL;
}
Ejemplo n.º 22
0
//===----------------------------------------------------------------------===//
//
// Presence/absence in working memory
//
//===----------------------------------------------------------------------===//
const int CouchWorkingMemory::knownp( const char *data ){
  // printf( "Known variable %s?\n", data );
  CouchWorkingMemory::S_Root = NULL;
  init();
  if( CouchWorkingMemory::S_Root ){
    json_t *wm = json_object_get( CouchWorkingMemory::S_Root, "wm" );
    json_t *sign;
    if( json_is_null( wm ) ){
      fprintf( stderr, "Eror: Working Memory is NULL!\n" );
    }
    else if( json_is_object( wm ) ){
      sign = json_object_get( wm, data );
      if( sign ){
	json_decref( CouchWorkingMemory::S_Root );
	return WM_KNOWN;
      }
    }   
  }
  json_decref( CouchWorkingMemory::S_Root );
  return WM_UNKNOWN;
}
Ejemplo n.º 23
0
GObject*
searpc_client_fret__object (GType gtype, char *data, size_t len, GError **error)
{
    json_t  *object = NULL;
    GObject *ret = NULL;
    json_t  *member;

    if (handle_ret_common(data, len, &object, error) == 0) {
        member = json_object_get (object, "ret");
        if (json_is_null(member)) {
            json_decref(object);
            return NULL;
        }
        
        ret = json_gobject_deserialize(gtype, member);
        json_decref(object);
        return ret;
    }

    return NULL;
}
Ejemplo n.º 24
0
static char *
parse_json_get_object_string (const char *json, const char *key, int flags,
                              const char *func, const char *cmd)
{
  const char *str;
  char *ret;
  json_t *tree = NULL, *node;

  tree = parse_json (json, func);
  if (tree == NULL)
    return NULL;

  if (!json_is_object (tree))
    goto bad_type;

  node = json_object_get (tree, key);
  if (node == NULL)
    goto bad_type;

  if ((flags & GET_STRING_NULL_TO_EMPTY) && json_is_null (node))
    ret = strdup ("");
  else {
    str = json_string_value (node);
    if (str == NULL)
      goto bad_type;
    ret = strndup (str, json_string_length (node));
  }
  if (ret == NULL)
    reply_with_perror ("strdup");

  json_decref (tree);

  return ret;

 bad_type:
  reply_with_error ("output of '%s' was not a JSON object "
                    "containing a key '%s' of type string", cmd, key);
  json_decref (tree);
  return NULL;
}
Ejemplo n.º 25
0
static bool stratum_show_message(struct stratum_ctx *sctx, json_t *id, json_t *params)
{
	char *s;
	json_t *val;
	bool ret;

	val = json_array_get(params, 0);
	if (val)
		applog(LOG_NOTICE, "MESSAGE FROM SERVER: %s", json_string_value(val));
	
	if (!id || json_is_null(id))
		return true;

	val = json_object();
	json_object_set(val, "id", id);
	json_object_set_new(val, "error", json_null());
	json_object_set_new(val, "result", json_true());
	s = json_dumps(val, 0);
	ret = stratum_send_line(sctx, s);
	json_decref(val);
	free(s);

	return ret;
}
Ejemplo n.º 26
0
void CouchWorkingMemory::clear(){
  // Get the latest revision of the document
  CouchWorkingMemory::S_Root = NULL;
  init();
  // Update it with an empty WM
  if( CouchWorkingMemory::S_Root ){  
    json_object_set_new( CouchWorkingMemory::S_Root, "agenda", json_array() );

    json_t *wm = json_object_get( CouchWorkingMemory::S_Root, "wm" );
    if( json_is_null( wm ) ){
      fprintf( stderr, "Eror: Working Memory is NULL!\n" );
    }
    else if( json_is_object( wm ) ){
      // Print current state
      char *s = json_dumps( wm, 0 );
      // printf( "Working Memory:\n%s\n", s );
      free( s );
      // Clear
      json_object_set_new( CouchWorkingMemory::S_Root, "wm", json_object() );
      update();
    }
  }
  json_decref( CouchWorkingMemory::S_Root );
}
Ejemplo n.º 27
0
bool stratum_authorize(struct stratum_ctx *sctx, const char *user, const char *pass)
{
	json_t *val = NULL, *res_val, *err_val;
	char *s, *sret;
	json_error_t err;
	bool ret = false;

	if(jsonrpc_2) {
        s = (char*)malloc(300 + strlen(user) + strlen(pass));
        sprintf(s, "{\"method\": \"login\", \"params\": {\"login\": \"%s\", \"pass\": \"%s\", \"agent\": \"cpuminer-multi/0.1\"}, \"id\": 1}",
                user, pass);
	} else {
        s = (char*)malloc(80 + strlen(user) + strlen(pass));
        sprintf(s, "{\"id\": 2, \"method\": \"mining.authorize\", \"params\": [\"%s\", \"%s\"]}",
                user, pass);
	}

	if (!stratum_send_line(sctx, s))
		goto out;

	while (1) {
		sret = stratum_recv_line(sctx);
		if (!sret)
			goto out;
		if (!stratum_handle_method(sctx, sret))
			break;
		free(sret);
	}

	val = JSON_LOADS(sret, &err);
	free(sret);
	if (!val) {
		applog(LOG_ERR, "JSON decode failed(%d): %s", err.line, err.text);
		goto out;
	}

	res_val = json_object_get(val, "result");
	err_val = json_object_get(val, "error");

	if (!res_val || json_is_false(res_val) ||
	    (err_val && !json_is_null(err_val)))  {
		applog(LOG_ERR, "Stratum authentication failed");
		goto out;
	}

    if(jsonrpc_2) {
        rpc2_login_decode(val);
        json_t *job_val = json_object_get(res_val, "job");
        pthread_mutex_lock(&sctx->work_lock);
        if(job_val) rpc2_job_decode(job_val, &sctx->work);
        pthread_mutex_unlock(&sctx->work_lock);
    }

	ret = true;

out:
	free(s);
	if (val)
		json_decref(val);

	return ret;
}
Ejemplo n.º 28
0
int schema_traverse(const avro_schema_t schema, json_t *json, json_t *dft,
                    avro_value_t *current_val, int quiet, int strjson, size_t max_str_sz) {

    json = json ? json : dft;
    if (!json) {
        fprintf(stderr, "ERROR: Avro schema does not match JSON\n");
        return 1;
    }

    switch (schema->type) {
    case AVRO_RECORD:
    {
        if (!json_is_object(json)) {
            if (!quiet)
                fprintf(stderr, "ERROR: Expecting JSON object for Avro record, got something else\n");
            return 1;
        }

        int len = avro_schema_record_size(schema), i;
        for (i=0; i<len; i++) {

            const char *name = avro_schema_record_field_name(schema, i);
            avro_schema_t field_schema = avro_schema_record_field_get_by_index(schema, i);

            json_t *json_val = json_object_get(json, name);
            json_t *dft = avro_schema_record_field_default_get_by_index(schema, i);

            avro_value_t field;
            avro_value_get_by_index(current_val, i, &field, NULL);

            if (schema_traverse(field_schema, json_val, dft, &field, quiet, strjson, max_str_sz))
                return 1;
        }
    } break;

    case AVRO_LINK:
        /* TODO */
        fprintf(stderr, "ERROR: AVRO_LINK is not implemented\n");
        return 1;
        break;

    case AVRO_STRING:
        if (!json_is_string(json)) {
            if (json && strjson) {
                /* -j specified, just dump the remaining json as string */
                char * js = json_dumps(json, JSON_COMPACT|JSON_SORT_KEYS|JSON_ENCODE_ANY);
                if (max_str_sz && (strlen(js) > max_str_sz))
                    js[max_str_sz] = 0; /* truncate the string - this will result in invalid JSON! */
                avro_value_set_string(current_val, js);
                free(js);
                break;
            }
            if (!quiet)
                fprintf(stderr, "ERROR: Expecting JSON string for Avro string, got something else\n");
            return 1;
        } else {
            const char *js = json_string_value(json);
            if (max_str_sz && (strlen(js) > max_str_sz)) {
                /* truncate the string */
                char *jst = malloc(strlen(js));
                strcpy(jst, js);
                jst[max_str_sz] = 0;
                avro_value_set_string(current_val, jst);
                free(jst);
            } else
                avro_value_set_string(current_val, js);
        }
        break;

    case AVRO_BYTES:
        if (!json_is_string(json)) {
            if (!quiet)
                fprintf(stderr, "ERROR: Expecting JSON string for Avro string, got something else\n");
            return 1;
        }
        /* NB: Jansson uses null-terminated strings, so embedded nulls are NOT
           supported, not even escaped ones */
        const char *s = json_string_value(json);
        avro_value_set_bytes(current_val, (void *)s, strlen(s));
        break;

    case AVRO_INT32:
        if (!json_is_integer(json)) {
            if (!quiet)
                fprintf(stderr, "ERROR: Expecting JSON integer for Avro int, got something else\n");
            return 1;
        }
        avro_value_set_int(current_val, json_integer_value(json));
        break;

    case AVRO_INT64:
        if (!json_is_integer(json)) {
            if (!quiet)
                fprintf(stderr, "ERROR: Expecting JSON integer for Avro long, got something else\n");
            return 1;
        }
        avro_value_set_long(current_val, json_integer_value(json));
        break;

    case AVRO_FLOAT:
        if (!json_is_number(json)) {
            if (!quiet)
                fprintf(stderr, "ERROR: Expecting JSON number for Avro float, got something else\n");
            return 1;
        }
        avro_value_set_float(current_val, json_number_value(json));
        break;

    case AVRO_DOUBLE:
        if (!json_is_number(json)) {
            if (!quiet)
                fprintf(stderr, "ERROR: Expecting JSON number for Avro double, got something else\n");
            return 1;
        }
        avro_value_set_double(current_val, json_number_value(json));
        break;

    case AVRO_BOOLEAN:
        if (!json_is_boolean(json)) {
            if (!quiet)
                fprintf(stderr, "ERROR: Expecting JSON boolean for Avro boolean, got something else\n");
            return 1;
        }
        avro_value_set_boolean(current_val, json_is_true(json));
        break;

    case AVRO_NULL:
        if (!json_is_null(json)) {
            if (!quiet)
                fprintf(stderr, "ERROR: Expecting JSON null for Avro null, got something else\n");
            return 1;
        }
        avro_value_set_null(current_val);
        break;

    case AVRO_ENUM:
        // TODO ???
        break;

    case AVRO_ARRAY:
        if (!json_is_array(json)) {
            if (!quiet)
                fprintf(stderr, "ERROR: Expecting JSON array for Avro array, got something else\n");
            return 1;
        } else {
            int i, len = json_array_size(json);
            avro_schema_t items = avro_schema_array_items(schema);
            avro_value_t val;
            for (i=0; i<len; i++) {
                avro_value_append(current_val, &val, NULL);
                if (schema_traverse(items, json_array_get(json, i), NULL, &val, quiet, strjson, max_str_sz))
                    return 1;
            }
        }
        break;

    case AVRO_MAP:
        if (!json_is_object(json)) {
            if (!quiet)
                fprintf(stderr, "ERROR: Expecting JSON object for Avro map, got something else\n");
            return 1;
        } else {
            avro_schema_t values = avro_schema_map_values(schema);
            void *iter = json_object_iter(json);
            avro_value_t val;
            while (iter) {
                avro_value_add(current_val, json_object_iter_key(iter), &val, 0, 0);
                if (schema_traverse(values, json_object_iter_value(iter), NULL, &val, quiet, strjson, max_str_sz))
                    return 1;
                iter = json_object_iter_next(json, iter);
            }
        }
        break;

    case AVRO_UNION:
    {
        int i;
        avro_value_t branch;
        for (i=0; i<avro_schema_union_size(schema); i++) {
            avro_value_set_branch(current_val, i, &branch);
            avro_schema_t type = avro_schema_union_branch(schema, i);
            if (!schema_traverse(type, json, NULL, &branch, 1, strjson, max_str_sz))
                break;
        }
        if (i==avro_schema_union_size(schema)) {
            fprintf(stderr, "ERROR: No type in the Avro union matched the JSON type we got\n");
            return 1;
        }
        break;
    }
    case AVRO_FIXED:
        if (!json_is_string(json)) {
            if (!quiet)
                fprintf(stderr, "ERROR: Expecting JSON string for Avro fixed, got something else\n");
            return 1;
        }
        /* NB: Jansson uses null-terminated strings, so embedded nulls are NOT
           supported, not even escaped ones */
        const char *f = json_string_value(json);
        if (avro_value_set_fixed(current_val, (void *)f, strlen(f))) {
            fprintf(stderr, "ERROR: Setting Avro fixed value FAILED\n");
            return 1;
        }
        break;

    default:
        fprintf(stderr, "ERROR: Unknown type: %d\n", schema->type);
        return 1;
    }
    return 0;
}
Ejemplo n.º 29
0
/* Call the simple functions not covered by other tests of the public API */
int main()
{
    json_t *value;

    value = json_integer(1);
    if(json_typeof(value) != JSON_INTEGER)
        fail("json_typeof failed");

    if(json_is_object(value))
        fail("json_is_object failed");

    if(json_is_array(value))
        fail("json_is_array failed");

    if(json_is_string(value))
        fail("json_is_string failed");

    if(!json_is_integer(value))
        fail("json_is_integer failed");

    if(json_is_real(value))
        fail("json_is_real failed");

    if(!json_is_number(value))
        fail("json_is_number failed");

    if(json_is_true(value))
        fail("json_is_true failed");

    if(json_is_false(value))
        fail("json_is_false failed");

    if(json_is_boolean(value))
        fail("json_is_boolean failed");

    if(json_is_null(value))
        fail("json_is_null failed");

    json_decref(value);


    value = json_string("foo");
    if(!value)
        fail("json_string failed");
    if(strcmp(json_string_value(value), "foo"))
        fail("invalid string value");

    if(json_string_set(value, "bar"))
        fail("json_string_set failed");
    if(strcmp(json_string_value(value), "bar"))
        fail("invalid string value");

    json_decref(value);

    value = json_string(NULL);
    if(value)
        fail("json_string(NULL) failed");

    /* invalid UTF-8  */
    value = json_string("a\xefz");
    if(value)
        fail("json_string(<invalid utf-8>) failed");

    value = json_string_nocheck("foo");
    if(!value)
        fail("json_string_nocheck failed");
    if(strcmp(json_string_value(value), "foo"))
        fail("invalid string value");

    if(json_string_set_nocheck(value, "bar"))
        fail("json_string_set_nocheck failed");
    if(strcmp(json_string_value(value), "bar"))
        fail("invalid string value");

    json_decref(value);

    /* invalid UTF-8 */
    value = json_string_nocheck("qu\xff");
    if(!value)
        fail("json_string_nocheck failed");
    if(strcmp(json_string_value(value), "qu\xff"))
        fail("invalid string value");

    if(json_string_set_nocheck(value, "\xfd\xfe\xff"))
        fail("json_string_set_nocheck failed");
    if(strcmp(json_string_value(value), "\xfd\xfe\xff"))
        fail("invalid string value");

    json_decref(value);


    value = json_integer(123);
    if(!value)
        fail("json_integer failed");
    if(json_integer_value(value) != 123)
        fail("invalid integer value");
    if(json_number_value(value) != 123.0)
        fail("invalid number value");

    if(json_integer_set(value, 321))
        fail("json_integer_set failed");
    if(json_integer_value(value) != 321)
        fail("invalid integer value");
    if(json_number_value(value) != 321.0)
        fail("invalid number value");

    json_decref(value);

    value = json_real(123.123);
    if(!value)
        fail("json_real failed");
    if(json_real_value(value) != 123.123)
        fail("invalid integer value");
    if(json_number_value(value) != 123.123)
        fail("invalid number value");

    if(json_real_set(value, 321.321))
        fail("json_real_set failed");
    if(json_real_value(value) != 321.321)
        fail("invalid real value");
    if(json_number_value(value) != 321.321)
        fail("invalid number value");

    json_decref(value);

    value = json_true();
    if(!value)
        fail("json_true failed");
    json_decref(value);

    value = json_false();
    if(!value)
        fail("json_false failed");
    json_decref(value);

    value = json_null();
    if(!value)
        fail("json_null failed");
    json_decref(value);

    /* Test reference counting on singletons (true, false, null) */
    value = json_true();
    if(value->refcount != (size_t)-1)
      fail("refcounting true works incorrectly");
    json_decref(value);
    if(value->refcount != (size_t)-1)
      fail("refcounting true works incorrectly");
    json_incref(value);
    if(value->refcount != (size_t)-1)
      fail("refcounting true works incorrectly");

    value = json_false();
    if(value->refcount != (size_t)-1)
      fail("refcounting false works incorrectly");
    json_decref(value);
    if(value->refcount != (size_t)-1)
      fail("refcounting false works incorrectly");
    json_incref(value);
    if(value->refcount != (size_t)-1)
      fail("refcounting false works incorrectly");

    value = json_null();
    if(value->refcount != (size_t)-1)
      fail("refcounting null works incorrectly");
    json_decref(value);
    if(value->refcount != (size_t)-1)
      fail("refcounting null works incorrectly");
    json_incref(value);
    if(value->refcount != (size_t)-1)
      fail("refcounting null works incorrectly");

    return 0;
}
Ejemplo n.º 30
0
CCObject* NDKHelper::GetCCObjectFromJson(json_t *obj)
{
    if (obj == NULL)
        return NULL;
    
    if (json_is_object(obj))
    {
        CCDictionary *dictionary = new CCDictionary();
        //CCDictionary::create();
        
        const char *key;
        json_t *value;
        
        void *iter = json_object_iter(obj);
        while(iter)
        {
            key = json_object_iter_key(iter);
            value = json_object_iter_value(iter);
            
            dictionary->setObject(NDKHelper::GetCCObjectFromJson(value)->autorelease(), string(key));
            
            iter = json_object_iter_next(obj, iter);
        }
        
        return dictionary;
    }
    else if (json_is_array(obj))
    {
        size_t sizeArray = json_array_size(obj);
        CCArray *array = new CCArray();
        //CCArray::createWithCapacity(sizeArray);
        
        for (unsigned int i = 0; i < sizeArray; i++)
        {
            array->addObject(NDKHelper::GetCCObjectFromJson(json_array_get(obj, i))->autorelease());
        }
        
        return array;
    }
    else if (json_is_boolean(obj))
    {
        stringstream str;
        if (json_is_true(obj))
            str << true;
        else if (json_is_false(obj))
            str << false;
        
        CCString *ccString = new CCString(str.str());
        //CCString::create(str.str());
        return ccString;
    }
    else if (json_is_integer(obj))
    {
        stringstream str;
        str << json_integer_value(obj);
        
        CCString *ccString = new CCString(str.str());
        //CCString::create(str.str());
        return ccString;
    }
    else if (json_is_real(obj))
    {
        stringstream str;
        str << json_real_value(obj);
        
        CCString *ccString = new CCString(str.str());
        //CCString::create(str.str());
        return ccString;
    }
    else if (json_is_string(obj))
    {
        stringstream str;
        str << json_string_value(obj);
        
        CCString *ccString = new CCString(str.str());
        //CCString::create(str.str());
        return ccString;
    }
	else if (json_is_null(obj)) 
	{
		return new CCString("null");
	}
    
    return NULL;
}