MACSio  0.9
Multi-purpose, Application-Centric, Scalable I/O Proxy App
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
test_null.c
Go to the documentation of this file.
1 /*
2 * Tests if binary strings are supported.
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 
13 int main()
14 {
15  // this test has a space after the null character. check that it's still included
16  const char *input = " \0 ";
17  const char *expected = "\" \\u0000 \"";
18  struct json_object *string = json_object_new_string_len(input, 3);
19  const char *json = json_object_to_json_string(string);
20 
21  int strings_match = !strcmp( expected, json);
22  int retval = 0;
23  if (strings_match)
24  {
25  printf("JSON write result is correct: %s\n", json);
26  printf("PASS\n");
27  } else {
28  printf("JSON write result doesn't match expected string\n");
29  printf("expected string: ");
30  printf("%s\n", expected);
31  printf("parsed string: ");
32  printf("%s\n", json);
33  printf("FAIL\n");
34  retval=1;
35  }
36  json_object_put(string);
37 
38  struct json_object *parsed_str = json_tokener_parse(expected);
39  if (parsed_str)
40  {
41  int parsed_len = json_object_get_string_len(parsed_str);
42  const char *parsed_cstr = json_object_get_string(parsed_str);
43  int ii;
44  printf("Re-parsed object string len=%d, chars=[", parsed_len);
45  for (ii = 0; ii < parsed_len ; ii++)
46  {
47  printf("%s%d", (ii ? ", " : ""), (int)parsed_cstr[ii]);
48  }
49  printf("]\n");
50  json_object_put(parsed_str);
51  }
52  else
53  {
54  printf("ERROR: failed to parse\n");
55  }
56  return retval;
57 }
struct json_object * json_object_new_string_len(const char *s, int len)
Definition: json_object.c:738
int main()
Definition: test_null.c:13
const char * json_object_get_string(struct json_object *obj)
Definition: json_object.c:751
int json_object_put(struct json_object *obj)
Definition: json_object.c:155
int json_object_get_string_len(struct json_object *obj)
Definition: json_object.c:762
const char * json_object_to_json_string(struct json_object *obj)
Definition: json_object.c:295
struct json_object * json_tokener_parse(const char *str)
Definition: json_tokener.c:158