test4.c 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. /*
  2. * gcc -o utf8 utf8.c -I/home/y/include -L./.libs -ljson
  3. */
  4. #include <stdio.h>
  5. #include <string.h>
  6. #include "config.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 = "\xF0\xA0\x84\xA6,\xF0\xA0\x84\xA7,\xF0\x90\x84\xA6,\xF0\x90\x84\xA7";
  27. struct json_object *parse_result = json_tokener_parse(input);
  28. const char *unjson = json_object_get_string(parse_result);
  29. printf("input: %s\n", input);
  30. int strings_match = !strcmp( expected, unjson);
  31. int retval = 0;
  32. if (strings_match)
  33. {
  34. printf("JSON parse result is correct: %s\n", unjson);
  35. puts("PASS");
  36. } else {
  37. printf("JSON parse result doesn't match expected string\n");
  38. printf("expected string bytes: ");
  39. print_hex(expected);
  40. printf("parsed string bytes: ");
  41. print_hex(unjson);
  42. puts("FAIL");
  43. retval = 1;
  44. }
  45. json_object_put(parse_result);
  46. return retval;
  47. }