blob: 555afd1404196b7f72ecc5c8bf66081387df77b2 [file] [log] [blame]
Brent Miller126ad952009-08-20 06:50:22 +00001/*
2 * gcc -o utf8 utf8.c -I/home/y/include -L./.libs -ljson
3*/
4
5#include <stdio.h>
6#include <string.h>
Michael Clarkc4dceae2010-10-06 16:39:20 +00007#include "config.h"
Brent Miller126ad952009-08-20 06:50:22 +00008
Michael Clarkc4dceae2010-10-06 16:39:20 +00009#include "json_inttypes.h"
10#include "json_object.h"
11#include "json_tokener.h"
12
13void print_hex( const char* s) {
14 const char *iter = s;
Brent Miller126ad952009-08-20 06:50:22 +000015 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
25int 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 Clarkc4dceae2010-10-06 16:39:20 +000034 int retval = 0;
Brent Miller126ad952009-08-20 06:50:22 +000035 if (strings_match) {
36 printf("JSON parse result is correct: %s\n", unjson);
37 printf("PASS\n");
Brent Miller126ad952009-08-20 06:50:22 +000038 } 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 Clarkc4dceae2010-10-06 16:39:20 +000045 retval = 1;
Brent Miller126ad952009-08-20 06:50:22 +000046 }
Michael Clarkc4dceae2010-10-06 16:39:20 +000047 json_object_put(parse_result);
48 return retval;
Brent Miller126ad952009-08-20 06:50:22 +000049}