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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  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. const char *iter = s;
  12. unsigned char ch;
  13. while ((ch = *iter++) != 0) {
  14. if( ',' != ch)
  15. printf("%x ", ch);
  16. else
  17. printf( ",");
  18. }
  19. printf("\n");
  20. }
  21. int main() {
  22. const char *input = "\"\\ud840\\udd26,\\ud840\\udd27,\\ud800\\udd26,\\ud800\\udd27\"";
  23. const char *expected = "\xF0\xA0\x84\xA6,\xF0\xA0\x84\xA7,\xF0\x90\x84\xA6,\xF0\x90\x84\xA7";
  24. struct json_object *parse_result = json_tokener_parse((char*)input);
  25. const char *unjson = json_object_get_string(parse_result);
  26. printf("input: %s\n", input);
  27. int strings_match = !strcmp( expected, unjson);
  28. int retval = 0;
  29. if (strings_match) {
  30. printf("JSON parse result is correct: %s\n", unjson);
  31. printf("PASS\n");
  32. } else {
  33. printf("JSON parse result doesn't match expected string\n");
  34. printf("expected string bytes: ");
  35. print_hex( expected);
  36. printf("parsed string bytes: ");
  37. print_hex( unjson);
  38. printf("FAIL\n");
  39. retval = 1;
  40. }
  41. json_object_put(parse_result);
  42. return retval;
  43. }