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