You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

test4.c 1.8 kB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /*
  2. * gcc -o utf8 utf8.c -I/home/y/include -L./.libs -ljson
  3. */
  4. #include "config.h"
  5. #include <assert.h>
  6. #include <stdio.h>
  7. #include <string.h>
  8. #include <stdlib.h>
  9. #include "json_inttypes.h"
  10. #include "json_object.h"
  11. #include "json_tokener.h"
  12. void print_hex(const char *s)
  13. {
  14. const char *iter = s;
  15. unsigned char ch;
  16. while ((ch = *iter++) != 0)
  17. {
  18. if (',' != ch)
  19. printf("%x ", ch);
  20. else
  21. printf(",");
  22. }
  23. putchar('\n');
  24. }
  25. static void test_lot_of_adds(void);
  26. static void test_lot_of_adds()
  27. {
  28. int ii;
  29. char key[50];
  30. json_object *jobj = json_object_new_object();
  31. assert(jobj != NULL);
  32. for (ii = 0; ii < 500; ii++)
  33. {
  34. snprintf(key, sizeof(key), "k%d", ii);
  35. json_object *iobj = json_object_new_int(ii);
  36. assert(iobj != NULL);
  37. if (json_object_object_add(jobj, key, iobj))
  38. {
  39. fprintf(stderr, "FAILED to add object #%d\n", ii);
  40. abort();
  41. }
  42. }
  43. printf("%s\n", json_object_to_json_string(jobj));
  44. assert(json_object_object_length(jobj) == 500);
  45. json_object_put(jobj);
  46. }
  47. int main(void)
  48. {
  49. const char *input = "\"\\ud840\\udd26,\\ud840\\udd27,\\ud800\\udd26,\\ud800\\udd27\"";
  50. const char *expected =
  51. "\xF0\xA0\x84\xA6,\xF0\xA0\x84\xA7,\xF0\x90\x84\xA6,\xF0\x90\x84\xA7";
  52. struct json_object *parse_result = json_tokener_parse(input);
  53. const char *unjson = json_object_get_string(parse_result);
  54. printf("input: %s\n", input);
  55. int strings_match = !strcmp(expected, unjson);
  56. int retval = 0;
  57. if (strings_match)
  58. {
  59. printf("JSON parse result is correct: %s\n", unjson);
  60. puts("PASS");
  61. }
  62. else
  63. {
  64. printf("JSON parse result doesn't match expected string\n");
  65. printf("expected string bytes: ");
  66. print_hex(expected);
  67. printf("parsed string bytes: ");
  68. print_hex(unjson);
  69. puts("FAIL");
  70. retval = 1;
  71. }
  72. json_object_put(parse_result);
  73. test_lot_of_adds();
  74. return retval;
  75. }