test1.c
Go to the documentation of this file.
00001 #include <stdio.h>
00002 #include <stdlib.h>
00003 #include <string.h>
00004 
00005 #include "json.h"
00006 
00007 int main(int argc, char **argv)
00008 {
00009   argc = 0; argv = 0;
00010   struct json_tokener *tok;
00011   struct json_object *my_string, *my_int, *my_object, *my_array;
00012   struct json_object *new_obj;
00013   int i;
00014 
00015   mc_set_debug(2);
00016 
00017   my_string = json_object_new_string("\t");
00018   printf("my_string=%s\n", json_object_get_string(my_string));
00019   printf("my_string.to_string()=%s\n", json_object_to_json_string(my_string));
00020   json_object_put(my_string);
00021 
00022   my_string = json_object_new_string("\\");
00023   printf("my_string=%s\n", json_object_get_string(my_string));
00024   printf("my_string.to_string()=%s\n", json_object_to_json_string(my_string));
00025   json_object_put(my_string);
00026 
00027   my_string = json_object_new_string("foo");
00028   printf("my_string=%s\n", json_object_get_string(my_string));
00029   printf("my_string.to_string()=%s\n", json_object_to_json_string(my_string));
00030 
00031   my_int = json_object_new_int(9);
00032   printf("my_int=%d\n", json_object_get_int(my_int));
00033   printf("my_int.to_string()=%s\n", json_object_to_json_string(my_int));
00034 
00035   my_array = json_object_new_array();
00036   json_object_array_add(my_array, json_object_new_int(1));
00037   json_object_array_add(my_array, json_object_new_int(2));
00038   json_object_array_add(my_array, json_object_new_int(3));
00039   json_object_array_put_idx(my_array, 4, json_object_new_int(5));
00040   printf("my_array=\n");
00041   for(i=0; i < json_object_array_length(my_array); i++) {
00042     struct json_object *obj = json_object_array_get_idx(my_array, i);
00043     printf("\t[%d]=%s\n", i, json_object_to_json_string(obj));
00044   }
00045   printf("my_array.to_string()=%s\n", json_object_to_json_string(my_array));    
00046 
00047   my_object = json_object_new_object();
00048   json_object_object_add(my_object, "abc", json_object_new_int(12));
00049   json_object_object_add(my_object, "foo", json_object_new_string("bar"));
00050   json_object_object_add(my_object, "bool0", json_object_new_boolean(0));
00051   json_object_object_add(my_object, "bool1", json_object_new_boolean(1));
00052   json_object_object_add(my_object, "baz", json_object_new_string("bang"));
00053   json_object_object_add(my_object, "baz", json_object_new_string("fark"));
00054   json_object_object_del(my_object, "baz");
00055   json_object_object_add(my_object, "arr", my_array);
00056   printf("my_object=\n");
00057   /*json_object_object_foreach(my_object, key, val) {
00058     printf("\t%s: %s\n", key, json_object_to_json_string(val));
00059   }*/
00060   printf("my_object.to_string()=%s\n", json_object_to_json_string(my_object));
00061 
00062   new_obj = json_tokener_parse("\"\003\"");
00063   printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));
00064   json_object_put(new_obj);
00065 
00066   new_obj = json_tokener_parse("/* hello */\"foo\"");
00067   printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));
00068   json_object_put(new_obj);
00069 
00070   new_obj = json_tokener_parse("// hello\n\"foo\"");
00071   printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));
00072   json_object_put(new_obj);
00073 
00074   new_obj = json_tokener_parse("\"\\u0041\\u0042\\u0043\"");
00075   printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));
00076   json_object_put(new_obj);
00077 
00078   new_obj = json_tokener_parse("null");
00079   printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));
00080   json_object_put(new_obj);
00081 
00082   new_obj = json_tokener_parse("True");
00083   printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));
00084   json_object_put(new_obj);
00085 
00086   new_obj = json_tokener_parse("12");
00087   printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));
00088   json_object_put(new_obj);
00089 
00090   new_obj = json_tokener_parse("12.3");
00091   printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));
00092   json_object_put(new_obj);
00093 
00094   new_obj = json_tokener_parse("[\"\\n\"]");
00095   printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));
00096   json_object_put(new_obj);
00097 
00098   new_obj = json_tokener_parse("[\"\\nabc\\n\"]");
00099   printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));
00100   json_object_put(new_obj);
00101 
00102   new_obj = json_tokener_parse("[null]");
00103   printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));
00104   json_object_put(new_obj);
00105 
00106   new_obj = json_tokener_parse("[]");
00107   printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));
00108   json_object_put(new_obj);
00109 
00110   new_obj = json_tokener_parse("[false]");
00111   printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));
00112   json_object_put(new_obj);
00113 
00114   new_obj = json_tokener_parse("[\"abc\",null,\"def\",12]");
00115   printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));
00116   json_object_put(new_obj);
00117 
00118   new_obj = json_tokener_parse("{}");
00119   printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));
00120   json_object_put(new_obj);
00121 
00122   new_obj = json_tokener_parse("{ \"foo\": \"bar\" }");
00123   printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));
00124   json_object_put(new_obj);
00125 
00126   new_obj = json_tokener_parse("{ \"foo\": \"bar\", \"baz\": null, \"bool0\": true }");
00127   printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));
00128   json_object_put(new_obj);
00129 
00130   new_obj = json_tokener_parse("{ \"foo\": [null, \"foo\"] }");
00131   printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));
00132   json_object_put(new_obj);
00133 
00134   new_obj = json_tokener_parse("{ \"abc\": 12, \"foo\": \"bar\", \"bool0\": false, \"bool1\": true, \"arr\": [ 1, 2, 3, null, 5 ] }");
00135   printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));
00136   json_object_put(new_obj);
00137 
00138   new_obj = json_tokener_parse("{ foo }");
00139   if(is_error(new_obj)) printf("got error as expected\n");
00140 
00141   new_obj = json_tokener_parse("foo");
00142   if(is_error(new_obj)) printf("got error as expected\n");
00143 
00144   new_obj = json_tokener_parse("{ \"foo");
00145   if(is_error(new_obj)) printf("got error as expected\n");
00146 
00147   /* test incremental parsing */
00148 
00149 printf("Hello\n");
00150   tok = json_tokener_new();
00151   new_obj = json_tokener_parse_ex(tok, "{ \"foo", 6);
00152   if(is_error(new_obj)) printf("got error as expected\n");
00153   new_obj = json_tokener_parse_ex(tok, "\": {\"bar", 8);
00154   if(is_error(new_obj)) printf("got error as expected\n");
00155   new_obj = json_tokener_parse_ex(tok, "\":13}}", 6);
00156   printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));
00157   json_object_put(new_obj);  
00158   json_tokener_free(tok);
00159 
00160   json_object_put(my_string);
00161   json_object_put(my_int);
00162   json_object_put(my_object);
00163   /*json_object_put(my_array);*/
00164 
00165         {
00166         struct json_object *laser = json_object_new_object();
00167         struct json_object *my_array = json_object_new_array();
00168         int i; for(i=0;i<20;i++) {
00169                 json_object_array_add(my_array, 
00170                         i%5 > 0 ? json_object_new_double((double)i) :
00171                                 json_tokener_parse("null")
00172                                         );
00173         }
00174         json_object_object_add(laser, "readings", my_array);
00175 
00176         printf("new_obj.to_string()=%s\n", json_object_to_json_string(laser));
00177         json_object_put(laser);
00178 }
00179 
00180   return 0;
00181 }


csm
Author(s): Andrea Censi
autogenerated on Fri May 17 2019 02:28:33