Giter Club home page Giter Club logo

Comments (4)

Barenboim avatar Barenboim commented on May 29, 2024

No, we don't have any 'stringify' functions. But you may follow the test code to write one:

void print_json_value(const json_value_t *val, int depth);

void print_json_object(const json_object_t *obj, int depth)
{
	const char *name;
	const json_value_t *val;
	int n = 0;
	int i;

	printf("{\n");
	json_object_for_each(name, val, obj)
	{
		if (n != 0)
			printf(",\n");
		n++;
		for (i = 0; i < depth + 1; i++)
			printf("    ");
		printf("\"%s\": ", name);
		print_json_value(val, depth + 1);
	}

	printf("\n");
	for (i = 0; i < depth; i++)
		printf("    ");
	printf("}");
}

void print_json_array(const json_array_t *arr, int depth)
{
	const json_value_t *val;
	int n = 0;
	int i;

	printf("[\n");
	json_array_for_each(val, arr)
	{
		if (n != 0)
			printf(",\n");
		n++;
		for (i = 0; i < depth + 1; i++)
			printf("    ");
		print_json_value(val, depth + 1);
	}

	printf("\n");
	for (i = 0; i < depth; i++)
		printf("    ");
	printf("]");
}

void print_json_string(const char *str)
{
	printf("\"");
	while (*str)
	{
		switch (*str)
		{
		case '\r':
			printf("\\r");
			break;
		case '\n':
			printf("\\n");
			break;
		case '\f':
			printf("\\f");
			break;
		case '\b':
			printf("\\b");
			break;
		case '\"':
			printf("\\\"");
			break;
		case '\t':
			printf("\\t");
			break;
		case '\\':
			printf("\\\\");
			break;
		default:
			printf("%c", *str);
			break;
		}
		str++;
	}
	printf("\"");
}

void print_json_number(double number)
{
	long long integer = number;

	if (integer == number)
		printf("%lld", integer);
	else
		printf("%lf", number);
}

void print_json_value(const json_value_t *val, int depth)
{
	switch (json_value_type(val))
	{
	case JSON_VALUE_STRING:
		print_json_string(json_value_string(val));
		break;
	case JSON_VALUE_NUMBER:
		print_json_number(json_value_number(val));
		break;
	case JSON_VALUE_OBJECT:
		print_json_object(json_value_object(val), depth);
		break;
	case JSON_VALUE_ARRAY:
		print_json_array(json_value_array(val), depth);
		break;
	case JSON_VALUE_TRUE:
		printf("true");
		break;
	case JSON_VALUE_FALSE:
		printf("false");
		break;
	case JSON_VALUE_NULL:
		printf("null");
		break;
	}
}

from json-parser.

liaohongyuan avatar liaohongyuan commented on May 29, 2024

get it.I have write a conversion method According to the test. c,thanks

from json-parser.

Barenboim avatar Barenboim commented on May 29, 2024

You may check out this beautiful C++ wrapper: https://github.com/chanchann/Json

from json-parser.

liaohongyuan avatar liaohongyuan commented on May 29, 2024

Thanks for sharing and learning

from json-parser.

Related Issues (6)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.