Expose a lower-level API for encoding C string.

Add json_encode_string() as another API for directly encode
C string into JSON string.
pull/49/head
t-k- 8 years ago
parent c22be794ce
commit e3176df55d
  1. 16
      parson.c
  2. 1
      parson.h
  3. 10
      tests.c

@ -924,6 +924,22 @@ static int json_serialize_string(const char *string, char *buf) {
return written_total; return written_total;
} }
char *json_encode_string(const char *string) {
char *enc_str = NULL;
int buf_size_bytes = json_serialize_string(string, NULL);
if (buf_size_bytes <= 0) {
return NULL;
}
enc_str = (char*)malloc(buf_size_bytes + 1);
if (enc_str == NULL)
return NULL;
json_serialize_string(string, enc_str);
return enc_str;
}
static int append_indent(char *buf, int level) { static int append_indent(char *buf, int level) {
int i; int i;
int written = -1, written_total = 0; int written = -1, written_total = 0;

@ -75,6 +75,7 @@ JSON_Value * json_parse_string(const char *string);
JSON_Value * json_parse_string_with_comments(const char *string); JSON_Value * json_parse_string_with_comments(const char *string);
/* Serialization */ /* Serialization */
char * json_encode_string(const char *string);
size_t json_serialization_size(const JSON_Value *value); /* returns 0 on fail */ size_t json_serialization_size(const JSON_Value *value); /* returns 0 on fail */
JSON_Status json_serialize_to_buffer(const JSON_Value *value, char *buf, size_t buf_size_in_bytes); JSON_Status json_serialize_to_buffer(const JSON_Value *value, char *buf, size_t buf_size_in_bytes);
JSON_Status json_serialize_to_file(const JSON_Value *value, const char *filename); JSON_Status json_serialize_to_file(const JSON_Value *value, const char *filename);

@ -48,6 +48,7 @@ void test_suite_6(void); /* Test value comparing verification */
void test_suite_7(void); /* Test schema validation */ void test_suite_7(void); /* Test schema validation */
void test_suite_8(void); /* Test serialization */ void test_suite_8(void); /* Test serialization */
void test_suite_9(void); /* Test serialization (pretty) */ void test_suite_9(void); /* Test serialization (pretty) */
void test_suite_10(void); /* Test string encoding */
void print_commits_info(const char *username, const char *repo); void print_commits_info(const char *username, const char *repo);
void persistence_example(void); void persistence_example(void);
@ -74,6 +75,7 @@ int main() {
test_suite_7(); test_suite_7();
test_suite_8(); test_suite_8();
test_suite_9(); test_suite_9();
test_suite_10();
printf("Tests failed: %d\n", tests_failed); printf("Tests failed: %d\n", tests_failed);
printf("Tests passed: %d\n", tests_passed); printf("Tests passed: %d\n", tests_passed);
return 0; return 0;
@ -465,6 +467,14 @@ void test_suite_9(void) {
TEST(STREQ(file_contents, serialized)); TEST(STREQ(file_contents, serialized));
} }
void test_suite_10(void) {
const char *raw_str = "\"\\/\b\f\n\r\t\"test";
char *enc_str = json_encode_string(raw_str);
TEST(STREQ(enc_str, "\"\\\"\\\\\\/\\b\\f\\n\\r\\t\\\"test\""));
free(enc_str);
}
void print_commits_info(const char *username, const char *repo) { void print_commits_info(const char *username, const char *repo) {
JSON_Value *root_value; JSON_Value *root_value;
JSON_Array *commits; JSON_Array *commits;

Loading…
Cancel
Save