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