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