Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Always provide valuestring #831

Open
mvoh opened this issue Feb 26, 2024 · 0 comments
Open

Always provide valuestring #831

mvoh opened this issue Feb 26, 2024 · 0 comments

Comments

@mvoh
Copy link

mvoh commented Feb 26, 2024

Proposal

It would be very useful to always provide valuestring no matter what data type the value is.
Example use cases:

  • Huge integers. When we use external API & cannot pass large integers as strings. Actually, JSON specification does not limit how large a number can be.
    JSON number
  • Use custom interpreter/parser function.

It would extend the possibilities of cJSON while preserving C89 compatibility.

Code demonstration:

#include <stdio.h>
#include <stdlib.h>
#include <cjson/cJSON.h>

#define JSON_RAW "{\"number\":3000000000,\"ulonglong\":\"10000000000000000000\",\"hugeint\":\"1000000000000000000000000000000\"}"

int main()
{
	cJSON *overint32, *ulonglong;
	cJSON *json = cJSON_Parse(JSON_RAW);
	overint32 = cJSON_GetObjectItemCaseSensitive(json, "number");

	if (cJSON_IsNumber(overint32)) {
		printf("IsNumber. valueint=%d, valuedouble=%f, valuestring=\"%s\"\n", overint32->valueint, overint32->valuedouble, overint32->valuestring);
	}

	ulonglong = cJSON_GetObjectItemCaseSensitive(json, "ulonglong");

	if (cJSON_IsString(ulonglong)) {
		char *endptr;
		unsigned long long int ull = strtoull(ulonglong->valuestring, &endptr, 10);
		printf("IsString. valuestring=\"%s\", unsigned long long int = %llu\n", ulonglong->valuestring, ull);
	}

	/*
	typedef struct __uint256_t { char arr[32]; } __uint256_t;
	hugeint = cJSON_GetObjectItemCaseSensitive(json, "hugeint");
	// Custom integral number interpreter
	__uint256_t value = custom_strtoint(hugeint->valuestring);
	*/

	cJSON_Delete(json);

	return 0;
}

Related issues

This issue aims to cover the following ones, which include more details and ideas:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant