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.2 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. /*
  2. * gcc -o utf8 utf8.c -I/home/y/include -L./.libs -ljson
  3. */
  4. #include "config.h"
  5. #include <stdio.h>
  6. #include <string.h>
  7. #include "json_inttypes.h"
  8. #include "json_object.h"
  9. #include "json_tokener.h"
  10. void print_hex(const char *s)
  11. {
  12. const char *iter = s;
  13. unsigned char ch;
  14. while ((ch = *iter++) != 0)
  15. {
  16. if (',' != ch)
  17. printf("%x ", ch);
  18. else
  19. printf(",");
  20. }
  21. putchar('\n');
  22. }
  23. int main(void)
  24. {
  25. const char *input = "\"\\ud840\\udd26,\\ud840\\udd27,\\ud800\\udd26,\\ud800\\udd27\"";
  26. const char *expected =
  27. "\xF0\xA0\x84\xA6,\xF0\xA0\x84\xA7,\xF0\x90\x84\xA6,\xF0\x90\x84\xA7";
  28. struct json_object *parse_result = json_tokener_parse(input);
  29. const char *unjson = json_object_get_string(parse_result);
  30. printf("input: %s\n", input);
  31. int strings_match = !strcmp(expected, unjson);
  32. int retval = 0;
  33. if (strings_match)
  34. {
  35. printf("JSON parse result is correct: %s\n", unjson);
  36. puts("PASS");
  37. }
  38. else
  39. {
  40. printf("JSON parse result doesn't match expected string\n");
  41. printf("expected string bytes: ");
  42. print_hex(expected);
  43. printf("parsed string bytes: ");
  44. print_hex(unjson);
  45. puts("FAIL");
  46. retval = 1;
  47. }
  48. json_object_put(parse_result);
  49. return retval;
  50. }