示例#1
0
文件: test.c 项目: jkristell/json
int test_lines(void)
{
	json_t *root;
	json_t *a;
	json_t *i;
	char *str = strdup ("{ \"lines\" : [\"word1\", \"word2\"] }");

	root = json_from_string (str, NULL);

	a = json_get_array (root, "lines", NULL);

	for (i = json_get_first(a); i; i = i->next) {
		printf ("w = %s\n", i->val.str);
	}

	json_free (root);
	free (str);

	return 0;
}
示例#2
0
YAAMP_JOB_TEMPLATE *coind_create_template(YAAMP_COIND *coind)
{
	if(coind->usememorypool)
		return coind_create_template_memorypool(coind);

	char params[4*1024] = "[{}]";
	if(!strcmp(coind->symbol, "PPC")) strcpy(params, "[]");

	json_value *json = rpc_call(&coind->rpc, "getblocktemplate", params);
	if(!json || json->type == json_null)
	{
		coind_error(coind, "getblocktemplate");
		return NULL;
	}

	json_value *json_result = json_get_object(json, "result");
	if(!json_result || json_result->type == json_null)
	{
		coind_error(coind, "getblocktemplate");
		json_value_free(json);

		return NULL;
	}

	json_value *json_tx = json_get_array(json_result, "transactions");
	if(!json_tx)
	{
		coind_error(coind, "getblocktemplate");
		json_value_free(json);

		return NULL;
	}

	json_value *json_coinbaseaux = json_get_object(json_result, "coinbaseaux");
	if(!json_coinbaseaux)
	{
		coind_error(coind, "getblocktemplate");
		json_value_free(json);

		return NULL;
	}

	YAAMP_JOB_TEMPLATE *templ = new YAAMP_JOB_TEMPLATE;
	memset(templ, 0, sizeof(YAAMP_JOB_TEMPLATE));

	templ->created = time(NULL);
	templ->value = json_get_int(json_result, "coinbasevalue");
	templ->height = json_get_int(json_result, "height");
	sprintf(templ->version, "%08x", (unsigned int)json_get_int(json_result, "version"));
	sprintf(templ->ntime, "%08x", (unsigned int)json_get_int(json_result, "curtime"));
	strcpy(templ->nbits, json_get_string(json_result, "bits"));
	strcpy(templ->prevhash_hex, json_get_string(json_result, "previousblockhash"));
	strcpy(templ->flags, json_get_string(json_coinbaseaux, "flags"));

//	debuglog("%s ntime %s\n", coind->symbol, templ->ntime);
//	uint64_t target = decode_compact(json_get_string(json_result, "bits"));
//	coind->difficulty = target_to_diff(target);

//	string_lower(templ->ntime);
//	string_lower(templ->nbits);

//	char target[1024];
//	strcpy(target, json_get_string(json_result, "target"));
//	uint64_t coin_target = decode_compact(templ->nbits);
//	debuglog("%s\n", templ->nbits);
//	debuglog("%s\n", target);
//	debuglog("0000%016llx\n", coin_target);

	if(coind->isaux)
	{
		json_value_free(json);

		coind_getauxblock(coind);
		return templ;
	}

	//////////////////////////////////////////////////////////////////////////////////////////

	vector<string> txhashes;
	txhashes.push_back("");

	for(int i = 0; i < json_tx->u.array.length; i++)
	{
		const char *p = json_get_string(json_tx->u.array.values[i], "hash");

		char hash_be[1024];
		memset(hash_be, 0, 1024);
		string_be(p, hash_be);

		txhashes.push_back(hash_be);

		const char *d = json_get_string(json_tx->u.array.values[i], "data");
		templ->txdata.push_back(d);
	}

	templ->txmerkles[0] = 0;
	templ->txcount = txhashes.size();
	templ->txsteps = merkle_steps(txhashes);

	vector<string>::const_iterator i;
	for(i = templ->txsteps.begin(); i != templ->txsteps.end(); ++i)
		sprintf(templ->txmerkles + strlen(templ->txmerkles), "\"%s\",", (*i).c_str());

	if(templ->txmerkles[0])
		templ->txmerkles[strlen(templ->txmerkles)-1] = 0;

//	debuglog("merkle transactions %d [%s]\n", templ->txcount, templ->txmerkles);
	ser_string_be2(templ->prevhash_hex, templ->prevhash_be, 8);

	if(!coind->pos)
		coind_aux_build_auxs(templ);

	coinbase_create(coind, templ, json_result);
	json_value_free(json);

	return templ;
}
示例#3
0
文件: test.c 项目: jkristell/json
int test_basic (void)
{
	json_t      *root;
	json_t      *o;
	json_t      *i;
	char        *str;
	const char  *s;
	int          n;
	double       d;
	int          boolval;

	str = "  {\"l1\" : 1111, "
		" \"l2\" : {\"l3\": {\"l4\": \"s2\", \"l5\": 2222 } }, "
		" \"l6\" : [1,2,3,4,5], "
		" \"l7\" : { \"l8\" : \"Hello World\" }, "
		" \"float\": 273.93,"
		" \"b1\": trUe,"
		" \"nil\": nulL,"
		" \"b2\": false} \n\t";
	str = strdup (str);

	root = json_from_string (str, NULL);
	assert (root);

	n = json_get_int (root, "l1", NULL);
	assert (n == 1111);

	d = json_get_double (root, "float", NULL);
	assert (d == 273.93);

	i = json_get_array (root, "l6", NULL);
	assert (i->type == JSON_TYPE_ARRAY);

	i = json_get_first (i);
	for (n = 0; i; n++, i = i->next) {
		int data[] = {1,2,3,4,5};
		assert (i->type == JSON_TYPE_INT);
		assert (i->val.num == data[n]);
	}

	o = json_get (root, "nil", NULL);
	assert (o->type == JSON_TYPE_NULL);

	o = json_get (root, "b1", NULL);
	assert (o->type == JSON_TYPE_TRUE);

	o = json_get (root, "b2", NULL);
	assert (o->type == JSON_TYPE_FALSE);
	
	boolval = json_get_bool (root, "b1", NULL);
	assert (boolval == 1);

	boolval = json_get_bool (root, "b2", NULL);
	assert (boolval == 0);

	boolval = json_get_bool (root, "nonexisting", NULL);
	assert (boolval == -1);

	o = json_get_object (root, "l7", NULL);
	assert (o);
	if (o) {
		s = json_get_string (o, "l8", NULL);
		printf ("str = %s\n", s);
	}

	printf ("\n");

	json_print (root);
	printf ("\n");

	json_free (root);
	free (str);
	return 0;
}