├── .gitignore ├── .travis.yml ├── .travis_config.rb ├── README.md ├── example ├── example.rb └── jsonrpc.rb ├── mrbgem.rake ├── mrblib └── json.rb ├── src ├── mrb_json.c ├── parson.c └── parson.h └── test └── json.rb /.gitignore: -------------------------------------------------------------------------------- 1 | gem_* 2 | gem-* 3 | mrb-*.a 4 | src/*.o 5 | .local.vimrc 6 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | sudo: false 2 | addons: 3 | apt: 4 | packages: 5 | - gperf 6 | script: 7 | - export MRUBY_CONFIG="$TRAVIS_BUILD_DIR/.travis_config.rb" 8 | - git clone --depth 1 "https://github.com/mruby/mruby.git" 9 | - cd mruby 10 | - ./minirake all test 11 | -------------------------------------------------------------------------------- /.travis_config.rb: -------------------------------------------------------------------------------- 1 | MRuby::Build.new do |conf| 2 | toolchain :gcc 3 | enable_debug 4 | enable_test # IMPORTANT! 5 | 6 | gem :core => 'mruby-print' 7 | gem File.expand_path(File.dirname(__FILE__)) 8 | end 9 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # mruby-json 2 | 3 | [![Build Status](https://travis-ci.org/mattn/mruby-json.svg)](https://travis-ci.org/mattn/mruby-json) 4 | 5 | JSON parser for mruby 6 | 7 | ## install by mrbgems 8 | ```ruby 9 | MRuby::Build.new do |conf| 10 | 11 | # ... (snip) ... 12 | 13 | conf.gem :github => 'mattn/mruby-json' 14 | end 15 | ``` 16 | 17 | ## License 18 | 19 | MIT 20 | 21 | ## Note 22 | 23 | This repository include fork of [parson](https://github.com/kgabis/parson) library because parson only handle 11 bits precision for fixed numbers. I think original policy is right on the implementing JSON. But not useful to handle 64bit numbers on mruby. If you want to contribute to the source parson.c or parson.h, please send PR to my [fork](https://github.com/mattn/parson). 24 | 25 | ## Author 26 | 27 | Yasuhiro Matsumoto (a.k.a mattn) 28 | -------------------------------------------------------------------------------- /example/example.rb: -------------------------------------------------------------------------------- 1 | #!mruby 2 | 3 | puts JSON::parse('{"foo": "bar"}') == {"foo"=>"bar"} 4 | puts JSON::parse('{"foo": null}') == {"foo"=>nil} 5 | puts JSON::parse('[true, "foo"]')[1] == "foo" 6 | puts JSON::stringify(true) == "true" 7 | puts JSON::stringify({"foo"=>"bar"}) == '{"foo":"bar"}' 8 | puts JSON::stringify({"foo"=> 1}) == '{"foo":1}' 9 | puts JSON::stringify({"foo"=> 2.3}) == '{"foo":2.3}' 10 | puts JSON::stringify({"foo"=> nil}) == '{"foo":null}' 11 | puts JSON::stringify({true=> 3.4}) == '{"true":3.4}' 12 | puts JSON::stringify({{"foo"=> "bar"}=> 1.2}) == '{"{\"foo\"=>\"bar\"}":1.2}' 13 | puts JSON::stringify([]) == "[]" 14 | puts JSON::stringify([1,true,"foo"]) == "[1,true,\"foo\"]" 15 | -------------------------------------------------------------------------------- /example/jsonrpc.rb: -------------------------------------------------------------------------------- 1 | #!mruby 2 | 3 | class Foo 4 | def greeting(*args) 5 | puts "Hey #{args[0]}!" 6 | "hello #{args[0]}!" 7 | end 8 | end 9 | 10 | h = HTTP::Parser.new() 11 | s = UV::TCP.new() 12 | s.bind(UV::ip4_addr('127.0.0.1', 8888)) 13 | s.listen(1024) {|x| 14 | return if x != 0 15 | c = s.accept() 16 | c.read_start {|b| 17 | return unless b 18 | h.parse_request(b) {|r| 19 | if r.method == 'POST' 20 | begin 21 | rpc = JSON::parse(r.body) 22 | ret = nil 23 | if rpc.key?("method") && rpc['method'] == 'Foo.greeting' 24 | params = rpc['params'] 25 | params = params.values if params.class.to_s == 'Hash' 26 | params = [params] if params.class.to_s != 'Array' 27 | params = [nil] if params.size == 0 28 | ret = Foo.new.send('greeting', params[0]) 29 | else 30 | ret = {"error"=> "unknown method"} 31 | end 32 | rescue ArgumentError => e 33 | ret = {"error"=> e.to_s} 34 | end 35 | c.write("HTTP/1.1 200 OK\r\nConnection: close\r\n\r\n#{JSON::stringify(ret)}") {|x| 36 | c.close() if c 37 | c = nil 38 | } 39 | else 40 | c.write("HTTP/1.1 200 OK\r\nConnection: close\r\n\r\nhello world") {|x| 41 | c.close() if c 42 | c = nil 43 | } 44 | end 45 | } 46 | } 47 | } 48 | 49 | t = UV::Timer.new 50 | t.start(5000, 5000) {|x| 51 | UV::gc() 52 | GC.start 53 | } 54 | 55 | UV::run() 56 | -------------------------------------------------------------------------------- /mrbgem.rake: -------------------------------------------------------------------------------- 1 | require "#{MRUBY_ROOT}/lib/mruby/source" 2 | 3 | MRuby::Gem::Specification.new('mruby-json') do |spec| 4 | spec.license = 'MIT' 5 | spec.authors = 'mattn' 6 | spec.cc.defines << 'JSON_FIXED_NUMBER' 7 | spec.add_dependency 'mruby-metaprog', :core => 'mruby-metaprog' if MRuby::Source::MRUBY_VERSION >= '2.0.0' 8 | end 9 | -------------------------------------------------------------------------------- /mrblib/json.rb: -------------------------------------------------------------------------------- 1 | class Object 2 | def to_json() 3 | JSON::generate(self) 4 | end 5 | end 6 | 7 | module JSON 8 | class JSONError < StandardError; end 9 | class ParserError < JSONError; end 10 | end 11 | -------------------------------------------------------------------------------- /src/mrb_json.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include "parson.h" 9 | 10 | #ifdef MRB_WITHOUT_FLOAT 11 | #ifndef JSON_FIXED_NUMBER 12 | #error "Without float, fixed number must be turned on!" 13 | #endif 14 | #endif 15 | 16 | #if 1 17 | #define ARENA_SAVE \ 18 | int ai = mrb_gc_arena_save(mrb); \ 19 | if (ai == MRB_ARENA_SIZE) { \ 20 | mrb_raise(mrb, E_RUNTIME_ERROR, "arena overflow"); \ 21 | } 22 | #define ARENA_RESTORE \ 23 | mrb_gc_arena_restore(mrb, ai); 24 | #else 25 | #define ARENA_SAVE 26 | #define ARENA_RESTORE 27 | #endif 28 | 29 | #define E_PARSER_ERROR mrb_class_get_under(mrb, mrb_module_get(mrb, "JSON"), "ParserError") 30 | 31 | /********************************************************* 32 | * main 33 | *********************************************************/ 34 | static mrb_bool 35 | mrb_method_defined(mrb_state* mrb, mrb_value value, const char* name) { 36 | int ai = mrb_gc_arena_save(mrb); 37 | mrb_sym mid = mrb_intern_cstr(mrb, name); 38 | mrb_value methods = mrb_funcall(mrb, value, "public_methods", 1, mrb_false_value()); 39 | mrb_bool included = FALSE; 40 | if (mrb_array_p(methods)) { 41 | mrb_int i; 42 | for (i = 0; i < RARRAY_LEN(methods); ++i) { 43 | if (mid == mrb_symbol(RARRAY_PTR(methods)[i])) { 44 | included = TRUE; 45 | break; 46 | } 47 | } 48 | } 49 | mrb_gc_arena_restore(mrb, ai); 50 | return included; 51 | } 52 | 53 | static mrb_value 54 | pretty_cat(mrb_state* mrb, mrb_value str, int pretty) { 55 | int i; 56 | str = mrb_str_cat_cstr(mrb, str, "\n"); 57 | for (i = 0; i < pretty; i++) str = mrb_str_cat_cstr(mrb, str, " "); 58 | return str; 59 | } 60 | 61 | static mrb_value 62 | mrb_value_to_string(mrb_state* mrb, mrb_value value, int pretty) { 63 | mrb_value str; 64 | 65 | if (mrb_nil_p(value)) { 66 | return mrb_str_new_cstr(mrb, "null"); 67 | } 68 | 69 | switch (mrb_type(value)) { 70 | case MRB_TT_FIXNUM: 71 | #ifndef MRB_WITHOUT_FLOAT 72 | case MRB_TT_FLOAT: 73 | #endif 74 | case MRB_TT_TRUE: 75 | case MRB_TT_FALSE: 76 | case MRB_TT_UNDEF: 77 | str = mrb_funcall(mrb, value, "to_s", 0, NULL); 78 | break; 79 | case MRB_TT_SYMBOL: 80 | value = mrb_funcall(mrb, value, "to_s", 0, NULL); 81 | /* FALLTHROUGH */ 82 | case MRB_TT_STRING: 83 | { 84 | int ai = mrb_gc_arena_save(mrb); 85 | char* ptr = RSTRING_PTR(value); 86 | char* end = RSTRING_END(value); 87 | str = mrb_str_new_cstr(mrb, "\""); 88 | while (ptr < end && *ptr) { 89 | switch (*ptr) { 90 | case '\\': 91 | str = mrb_str_cat_cstr(mrb, str, "\\\\"); 92 | break; 93 | case '"': 94 | str = mrb_str_cat_cstr(mrb, str, "\\\""); 95 | break; 96 | case '\b': 97 | str = mrb_str_cat_cstr(mrb, str, "\\b"); 98 | break; 99 | case '\f': 100 | str = mrb_str_cat_cstr(mrb, str, "\\f"); 101 | break; 102 | case '\n': 103 | str = mrb_str_cat_cstr(mrb, str, "\\n"); 104 | break; 105 | case '\r': 106 | str = mrb_str_cat_cstr(mrb, str, "\\r"); 107 | break; 108 | case '\t': 109 | str = mrb_str_cat_cstr(mrb, str, "\\t"); 110 | break; 111 | default: 112 | // TODO: handle unicode 113 | str = mrb_str_cat(mrb, str, ptr, 1); 114 | } 115 | ptr++; 116 | } 117 | mrb_str_cat_cstr(mrb, str, "\""); 118 | mrb_gc_arena_restore(mrb, ai); 119 | } 120 | break; 121 | case MRB_TT_HASH: 122 | { 123 | mrb_value keys; 124 | int n, l; 125 | 126 | str = mrb_str_new_cstr(mrb, "{"); 127 | keys = mrb_hash_keys(mrb, value); 128 | l = RARRAY_LEN(keys); 129 | if (l == 0) { 130 | if (pretty >= 0) return mrb_str_cat_cstr(mrb, str, "\n}"); 131 | return mrb_str_cat_cstr(mrb, str, "}"); 132 | } 133 | if (pretty >= 0) str = pretty_cat(mrb, str, ++pretty); 134 | for (n = 0; n < l; n++) { 135 | mrb_value obj; 136 | int ai = mrb_gc_arena_save(mrb); 137 | mrb_value key = mrb_ary_entry(keys, n); 138 | mrb_value enckey = mrb_funcall(mrb, key, "to_s", 0, NULL); 139 | enckey = mrb_funcall(mrb, enckey, "inspect", 0, NULL); 140 | mrb_str_concat(mrb, str, enckey); 141 | mrb_str_cat_cstr(mrb, str, ":"); 142 | obj = mrb_hash_get(mrb, value, key); 143 | mrb_str_concat(mrb, str, mrb_value_to_string(mrb, obj, pretty)); 144 | if (n != l - 1) { 145 | mrb_str_cat_cstr(mrb, str, ","); 146 | if (pretty >= 0) str = pretty_cat(mrb, str, pretty); 147 | } 148 | mrb_gc_arena_restore(mrb, ai); 149 | } 150 | if (pretty >= 0) str = pretty_cat(mrb, str, --pretty); 151 | mrb_str_cat_cstr(mrb, str, "}"); 152 | break; 153 | } 154 | case MRB_TT_ARRAY: 155 | { 156 | int n, l; 157 | 158 | str = mrb_str_new_cstr(mrb, "["); 159 | l = RARRAY_LEN(value); 160 | if (l == 0) { 161 | if (pretty >= 0) return mrb_str_cat_cstr(mrb, str, "\n]"); 162 | return mrb_str_cat_cstr(mrb, str, "]"); 163 | } 164 | if (pretty >= 0) str = pretty_cat(mrb, str, ++pretty); 165 | for (n = 0; n < l; n++) { 166 | int ai = mrb_gc_arena_save(mrb); 167 | mrb_value obj = mrb_ary_entry(value, n); 168 | mrb_str_concat(mrb, str, mrb_value_to_string(mrb, obj, pretty)); 169 | if (n != l - 1) { 170 | mrb_str_cat_cstr(mrb, str, ","); 171 | if (pretty >= 0) str = pretty_cat(mrb, str, pretty); 172 | } 173 | mrb_gc_arena_restore(mrb, ai); 174 | } 175 | if (pretty >= 0) str = pretty_cat(mrb, str, --pretty); 176 | mrb_str_cat_cstr(mrb, str, "]"); 177 | break; 178 | } 179 | default: 180 | { 181 | if (mrb_method_defined(mrb, value, "to_json")) 182 | str = mrb_funcall(mrb, value, "to_json", 0, NULL); 183 | else 184 | str = mrb_value_to_string(mrb, mrb_funcall(mrb, value, "to_s", 0, NULL), pretty); 185 | } 186 | } 187 | return str; 188 | } 189 | 190 | static mrb_value 191 | json_value_to_mrb_value(mrb_state* mrb, JSON_Value* value) { 192 | mrb_value ret; 193 | switch (json_value_get_type(value)) { 194 | case JSONError: 195 | case JSONNull: 196 | ret = mrb_nil_value(); 197 | break; 198 | case JSONString: 199 | ret = mrb_str_new_cstr(mrb, json_value_get_string(value)); 200 | break; 201 | #ifdef JSON_FIXED_NUMBER 202 | case JSONFixed: 203 | ret = mrb_fixnum_value((mrb_int)json_value_get_fixed(value)); 204 | break; 205 | case JSONNumber: 206 | #ifndef MRB_WITHOUT_FLOAT 207 | ret = mrb_float_value(mrb, json_value_get_number(value)); 208 | #else 209 | mrb_raise(mrb, E_ARGUMENT_ERROR, "float value received in non-float environment!"); 210 | #endif 211 | break; 212 | #else 213 | case JSONNumber: 214 | { 215 | double d = json_value_get_number(value); 216 | if (floor(d) == d) { 217 | ret = mrb_fixnum_value(d); 218 | } 219 | else { 220 | ret = mrb_float_value(mrb, d); 221 | } 222 | } 223 | break; 224 | #endif 225 | case JSONObject: 226 | { 227 | mrb_value hash = mrb_hash_new(mrb); 228 | JSON_Object* object = json_value_get_object(value); 229 | size_t count = json_object_get_count(object); 230 | size_t n; 231 | for (n = 0; n < count; n++) { 232 | int ai = mrb_gc_arena_save(mrb); 233 | const char* name = json_object_get_name(object, n); 234 | mrb_hash_set(mrb, hash, mrb_str_new_cstr(mrb, name), 235 | json_value_to_mrb_value(mrb, json_object_get_value(object, name))); 236 | mrb_gc_arena_restore(mrb, ai); 237 | } 238 | ret = hash; 239 | } 240 | break; 241 | case JSONArray: 242 | { 243 | mrb_value ary; 244 | JSON_Array* array; 245 | size_t n, count; 246 | ary = mrb_ary_new(mrb); 247 | array = json_value_get_array(value); 248 | count = json_array_get_count(array); 249 | for (n = 0; n < count; n++) { 250 | int ai = mrb_gc_arena_save(mrb); 251 | JSON_Value* elem = json_array_get_value(array, n); 252 | mrb_ary_push(mrb, ary, json_value_to_mrb_value(mrb, elem)); 253 | mrb_gc_arena_restore(mrb, ai); 254 | } 255 | ret = ary; 256 | } 257 | break; 258 | case JSONBoolean: 259 | if (json_value_get_boolean(value)) 260 | ret = mrb_true_value(); 261 | else 262 | ret = mrb_false_value(); 263 | break; 264 | default: 265 | mrb_raise(mrb, E_ARGUMENT_ERROR, "invalid argument"); 266 | } 267 | return ret; 268 | } 269 | 270 | static mrb_value 271 | mrb_json_load(mrb_state *mrb, mrb_value self) 272 | { 273 | mrb_value value, blk; 274 | JSON_Value *root_value; 275 | mrb_value json = mrb_nil_value(); 276 | mrb_get_args(mrb, "S&", &json, &blk); 277 | 278 | root_value = json_parse_string(mrb_str_to_cstr(mrb, json)); 279 | if (!root_value) { 280 | mrb_raise(mrb, E_PARSER_ERROR, "invalid json"); 281 | } 282 | 283 | value = json_value_to_mrb_value(mrb, root_value); 284 | json_value_free(root_value); 285 | if (!mrb_nil_p(blk)) { 286 | mrb_value args[1]; 287 | args[0] = value; 288 | mrb_yield_argv(mrb, blk, 1, args); 289 | } 290 | return value; 291 | } 292 | 293 | static mrb_value 294 | mrb_json_parse(mrb_state *mrb, mrb_value self) 295 | { 296 | mrb_value value; 297 | JSON_Value *root_value; 298 | mrb_value json = mrb_nil_value(); 299 | mrb_get_args(mrb, "S", &json); 300 | 301 | root_value = json_parse_string(mrb_str_to_cstr(mrb, json)); 302 | if (!root_value) { 303 | mrb_raise(mrb, E_PARSER_ERROR, "invalid json"); 304 | } 305 | 306 | value = json_value_to_mrb_value(mrb, root_value); 307 | json_value_free(root_value); 308 | return value; 309 | } 310 | 311 | static mrb_value 312 | mrb_json_dump(mrb_state *mrb, mrb_value self) { 313 | mrb_value obj, io = mrb_nil_value(), out; 314 | mrb_get_args(mrb, "o|o", &obj, &io); 315 | out = mrb_value_to_string(mrb, obj, -1); 316 | if (mrb_nil_p(io)) { 317 | return out; 318 | } 319 | mrb_funcall(mrb, io, "write", 1, out); 320 | return io; 321 | } 322 | 323 | static mrb_value 324 | mrb_json_generate(mrb_state *mrb, mrb_value self) { 325 | mrb_value obj; 326 | mrb_get_args(mrb, "o", &obj); 327 | return mrb_value_to_string(mrb, obj, -1); 328 | } 329 | 330 | static mrb_value 331 | mrb_json_pretty_generate(mrb_state *mrb, mrb_value self) { 332 | mrb_value obj; 333 | mrb_get_args(mrb, "o", &obj); 334 | return mrb_value_to_string(mrb, obj, 0); 335 | } 336 | 337 | static mrb_value 338 | mrb_json_to_json(mrb_state *mrb, mrb_value self) { 339 | return mrb_value_to_string(mrb, self, -1); 340 | } 341 | /********************************************************* 342 | * register 343 | *********************************************************/ 344 | 345 | void 346 | mrb_mruby_json_gem_init(mrb_state* mrb) { 347 | struct RClass *_class_json = mrb_define_module(mrb, "JSON"); 348 | 349 | mrb_define_class_method(mrb, _class_json, "load", mrb_json_load, MRB_ARGS_REQ(1)); 350 | mrb_define_class_method(mrb, _class_json, "parse", mrb_json_parse, MRB_ARGS_REQ(1)); 351 | mrb_define_class_method(mrb, _class_json, "stringify", mrb_json_generate, MRB_ARGS_REQ(1)); 352 | mrb_define_class_method(mrb, _class_json, "dump", mrb_json_dump, MRB_ARGS_REQ(1)|MRB_ARGS_OPT(1)); 353 | mrb_define_class_method(mrb, _class_json, "generate", mrb_json_generate, MRB_ARGS_REQ(1)); 354 | mrb_define_class_method(mrb, _class_json, "pretty_generate", mrb_json_pretty_generate, MRB_ARGS_REQ(1)); 355 | mrb_define_class_method(mrb, mrb->object_class, "to_json", mrb_json_to_json, MRB_ARGS_NONE()); 356 | } 357 | 358 | void 359 | mrb_mruby_json_gem_final(mrb_state* mrb) { 360 | } 361 | 362 | /* vim:set et ts=2 sts=2 sw=2 tw=0: */ 363 | -------------------------------------------------------------------------------- /src/parson.c: -------------------------------------------------------------------------------- 1 | /* 2 | Parson ( http://kgabis.github.com/parson/ ) 3 | Copyright (c) 2012 - 2017 Krzysztof Gabis 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. 22 | */ 23 | #ifdef _MSC_VER 24 | #ifndef _CRT_SECURE_NO_WARNINGS 25 | #define _CRT_SECURE_NO_WARNINGS 26 | #endif /* _CRT_SECURE_NO_WARNINGS */ 27 | #endif /* _MSC_VER */ 28 | 29 | #include "parson.h" 30 | 31 | #include 32 | #include 33 | #include 34 | #include 35 | #include 36 | #include 37 | 38 | /* Apparently sscanf is not implemented in some "standard" libraries, so don't use it, if you 39 | * don't have to. */ 40 | #define sscanf THINK_TWICE_ABOUT_USING_SSCANF 41 | 42 | #define STARTING_CAPACITY 16 43 | #define MAX_NESTING 2048 44 | #define FLOAT_FORMAT "%1.17g" 45 | #ifdef PRIdMAX 46 | #define FIXED_FORMAT "%" PRIdMAX 47 | #else 48 | #define FIXED_FORMAT "%jd" 49 | #endif 50 | 51 | #define SIZEOF_TOKEN(a) (sizeof(a) - 1) 52 | #define SKIP_CHAR(str) ((*str)++) 53 | #define SKIP_WHITESPACES(str) while (isspace((unsigned char)(**str))) { SKIP_CHAR(str); } 54 | #define MAX(a, b) ((a) > (b) ? (a) : (b)) 55 | 56 | #undef malloc 57 | #undef free 58 | 59 | static JSON_Malloc_Function parson_malloc = malloc; 60 | static JSON_Free_Function parson_free = free; 61 | 62 | #define IS_CONT(b) (((unsigned char)(b) & 0xC0) == 0x80) /* is utf-8 continuation byte */ 63 | 64 | /* Type definitions */ 65 | typedef union json_value_value { 66 | char *string; 67 | double number; 68 | #ifdef JSON_FIXED_NUMBER 69 | intmax_t fixed; 70 | #endif 71 | JSON_Object *object; 72 | JSON_Array *array; 73 | int boolean; 74 | int null; 75 | } JSON_Value_Value; 76 | 77 | struct json_value_t { 78 | JSON_Value *parent; 79 | JSON_Value_Type type; 80 | JSON_Value_Value value; 81 | }; 82 | 83 | struct json_object_t { 84 | JSON_Value *wrapping_value; 85 | char **names; 86 | JSON_Value **values; 87 | size_t count; 88 | size_t capacity; 89 | }; 90 | 91 | struct json_array_t { 92 | JSON_Value *wrapping_value; 93 | JSON_Value **items; 94 | size_t count; 95 | size_t capacity; 96 | }; 97 | 98 | /* Various */ 99 | #ifndef MRB_DISABLE_STDIO 100 | static char * read_file(const char *filename); 101 | #endif 102 | static void remove_comments(char *string, const char *start_token, const char *end_token); 103 | static char * parson_strndup(const char *string, size_t n); 104 | static char * parson_strdup(const char *string); 105 | static int hex_char_to_int(char c); 106 | static int parse_utf16_hex(const char *string, unsigned int *result); 107 | static int num_bytes_in_utf8_sequence(unsigned char c); 108 | static int verify_utf8_sequence(const unsigned char *string, int *len); 109 | static int is_valid_utf8(const char *string, size_t string_len); 110 | static int is_decimal(const char *string, size_t length); 111 | 112 | /* JSON Object */ 113 | static JSON_Object * json_object_init(JSON_Value *wrapping_value); 114 | static JSON_Status json_object_add(JSON_Object *object, const char *name, JSON_Value *value); 115 | static JSON_Status json_object_resize(JSON_Object *object, size_t new_capacity); 116 | static JSON_Value * json_object_nget_value(const JSON_Object *object, const char *name, size_t n); 117 | static void json_object_free(JSON_Object *object); 118 | 119 | /* JSON Array */ 120 | static JSON_Array * json_array_init(JSON_Value *wrapping_value); 121 | static JSON_Status json_array_add(JSON_Array *array, JSON_Value *value); 122 | static JSON_Status json_array_resize(JSON_Array *array, size_t new_capacity); 123 | static void json_array_free(JSON_Array *array); 124 | 125 | /* JSON Value */ 126 | static JSON_Value * json_value_init_string_no_copy(char *string); 127 | 128 | /* Parser */ 129 | static JSON_Status skip_quotes(const char **string); 130 | static int parse_utf16(const char **unprocessed, char **processed); 131 | static char * process_string(const char *input, size_t len); 132 | static char * get_quoted_string(const char **string); 133 | static JSON_Value * parse_object_value(const char **string, size_t nesting); 134 | static JSON_Value * parse_array_value(const char **string, size_t nesting); 135 | static JSON_Value * parse_string_value(const char **string); 136 | static JSON_Value * parse_boolean_value(const char **string); 137 | static JSON_Value * parse_number_value(const char **string); 138 | static JSON_Value * parse_null_value(const char **string); 139 | static JSON_Value * parse_value(const char **string, size_t nesting); 140 | 141 | /* Serialization */ 142 | static int json_serialize_to_buffer_r(const JSON_Value *value, char *buf, int level, int is_pretty, char *num_buf); 143 | static int json_serialize_string(const char *string, char *buf); 144 | static int append_indent(char *buf, int level); 145 | static int append_string(char *buf, const char *string); 146 | 147 | /* Various */ 148 | static char * parson_strndup(const char *string, size_t n) { 149 | char *output_string = (char*)parson_malloc(n + 1); 150 | if (!output_string) { 151 | return NULL; 152 | } 153 | output_string[n] = '\0'; 154 | strncpy(output_string, string, n); 155 | return output_string; 156 | } 157 | 158 | static char * parson_strdup(const char *string) { 159 | return parson_strndup(string, strlen(string)); 160 | } 161 | 162 | static int hex_char_to_int(char c) { 163 | if (c >= '0' && c <= '9') { 164 | return c - '0'; 165 | } else if (c >= 'a' && c <= 'f') { 166 | return c - 'a' + 10; 167 | } else if (c >= 'A' && c <= 'F') { 168 | return c - 'A' + 10; 169 | } 170 | return -1; 171 | } 172 | 173 | static int parse_utf16_hex(const char *s, unsigned int *result) { 174 | int x1, x2, x3, x4; 175 | if (s[0] == '\0' || s[1] == '\0' || s[2] == '\0' || s[3] == '\0') { 176 | return 0; 177 | } 178 | x1 = hex_char_to_int(s[0]); 179 | x2 = hex_char_to_int(s[1]); 180 | x3 = hex_char_to_int(s[2]); 181 | x4 = hex_char_to_int(s[3]); 182 | if (x1 == -1 || x2 == -1 || x3 == -1 || x4 == -1) { 183 | return 0; 184 | } 185 | *result = (unsigned int)((x1 << 12) | (x2 << 8) | (x3 << 4) | x4); 186 | return 1; 187 | } 188 | 189 | static int num_bytes_in_utf8_sequence(unsigned char c) { 190 | if (c == 0xC0 || c == 0xC1 || c > 0xF4 || IS_CONT(c)) { 191 | return 0; 192 | } else if ((c & 0x80) == 0) { /* 0xxxxxxx */ 193 | return 1; 194 | } else if ((c & 0xE0) == 0xC0) { /* 110xxxxx */ 195 | return 2; 196 | } else if ((c & 0xF0) == 0xE0) { /* 1110xxxx */ 197 | return 3; 198 | } else if ((c & 0xF8) == 0xF0) { /* 11110xxx */ 199 | return 4; 200 | } 201 | return 0; /* won't happen */ 202 | } 203 | 204 | static int verify_utf8_sequence(const unsigned char *string, int *len) { 205 | unsigned int cp = 0; 206 | *len = num_bytes_in_utf8_sequence(string[0]); 207 | 208 | if (*len == 1) { 209 | cp = string[0]; 210 | } else if (*len == 2 && IS_CONT(string[1])) { 211 | cp = string[0] & 0x1F; 212 | cp = (cp << 6) | (string[1] & 0x3F); 213 | } else if (*len == 3 && IS_CONT(string[1]) && IS_CONT(string[2])) { 214 | cp = ((unsigned char)string[0]) & 0xF; 215 | cp = (cp << 6) | (string[1] & 0x3F); 216 | cp = (cp << 6) | (string[2] & 0x3F); 217 | } else if (*len == 4 && IS_CONT(string[1]) && IS_CONT(string[2]) && IS_CONT(string[3])) { 218 | cp = string[0] & 0x7; 219 | cp = (cp << 6) | (string[1] & 0x3F); 220 | cp = (cp << 6) | (string[2] & 0x3F); 221 | cp = (cp << 6) | (string[3] & 0x3F); 222 | } else { 223 | return 0; 224 | } 225 | 226 | /* overlong encodings */ 227 | if ((cp < 0x80 && *len > 1) || 228 | (cp < 0x800 && *len > 2) || 229 | (cp < 0x10000 && *len > 3)) { 230 | return 0; 231 | } 232 | 233 | /* invalid unicode */ 234 | if (cp > 0x10FFFF) { 235 | return 0; 236 | } 237 | 238 | /* surrogate halves */ 239 | if (cp >= 0xD800 && cp <= 0xDFFF) { 240 | return 0; 241 | } 242 | 243 | return 1; 244 | } 245 | 246 | static int is_valid_utf8(const char *string, size_t string_len) { 247 | int len = 0; 248 | const char *string_end = string + string_len; 249 | while (string < string_end) { 250 | if (!verify_utf8_sequence((const unsigned char*)string, &len)) { 251 | return 0; 252 | } 253 | string += len; 254 | } 255 | return 1; 256 | } 257 | 258 | static int is_decimal(const char *string, size_t length) { 259 | if (length > 1 && string[0] == '0' && string[1] != '.') { 260 | return 0; 261 | } 262 | if (length > 2 && !strncmp(string, "-0", 2) && string[2] != '.') { 263 | return 0; 264 | } 265 | while (length--) { 266 | if (strchr("xX", string[length])) { 267 | return 0; 268 | } 269 | } 270 | return 1; 271 | } 272 | 273 | #ifndef MRB_DISABLE_STDIO 274 | static char * read_file(const char * filename) { 275 | FILE *fp = fopen(filename, "r"); 276 | size_t size_to_read = 0; 277 | size_t size_read = 0; 278 | long pos; 279 | char *file_contents; 280 | if (!fp) { 281 | return NULL; 282 | } 283 | fseek(fp, 0L, SEEK_END); 284 | pos = ftell(fp); 285 | if (pos < 0) { 286 | fclose(fp); 287 | return NULL; 288 | } 289 | size_to_read = pos; 290 | rewind(fp); 291 | file_contents = (char*)parson_malloc(sizeof(char) * (size_to_read + 1)); 292 | if (!file_contents) { 293 | fclose(fp); 294 | return NULL; 295 | } 296 | size_read = fread(file_contents, 1, size_to_read, fp); 297 | if (size_read == 0 || ferror(fp)) { 298 | fclose(fp); 299 | parson_free(file_contents); 300 | return NULL; 301 | } 302 | fclose(fp); 303 | file_contents[size_read] = '\0'; 304 | return file_contents; 305 | } 306 | #endif 307 | 308 | static void remove_comments(char *string, const char *start_token, const char *end_token) { 309 | int in_string = 0, escaped = 0; 310 | size_t i; 311 | char *ptr = NULL, current_char; 312 | size_t start_token_len = strlen(start_token); 313 | size_t end_token_len = strlen(end_token); 314 | if (start_token_len == 0 || end_token_len == 0) { 315 | return; 316 | } 317 | while ((current_char = *string) != '\0') { 318 | if (current_char == '\\' && !escaped) { 319 | escaped = 1; 320 | string++; 321 | continue; 322 | } else if (current_char == '\"' && !escaped) { 323 | in_string = !in_string; 324 | } else if (!in_string && strncmp(string, start_token, start_token_len) == 0) { 325 | for(i = 0; i < start_token_len; i++) { 326 | string[i] = ' '; 327 | } 328 | string = string + start_token_len; 329 | ptr = strstr(string, end_token); 330 | if (!ptr) { 331 | return; 332 | } 333 | for (i = 0; i < (ptr - string) + end_token_len; i++) { 334 | string[i] = ' '; 335 | } 336 | string = ptr + end_token_len - 1; 337 | } 338 | escaped = 0; 339 | string++; 340 | } 341 | } 342 | 343 | /* JSON Object */ 344 | static JSON_Object * json_object_init(JSON_Value *wrapping_value) { 345 | JSON_Object *new_obj = (JSON_Object*)parson_malloc(sizeof(JSON_Object)); 346 | if (new_obj == NULL) { 347 | return NULL; 348 | } 349 | new_obj->wrapping_value = wrapping_value; 350 | new_obj->names = (char**)NULL; 351 | new_obj->values = (JSON_Value**)NULL; 352 | new_obj->capacity = 0; 353 | new_obj->count = 0; 354 | return new_obj; 355 | } 356 | 357 | static JSON_Status json_object_add(JSON_Object *object, const char *name, JSON_Value *value) { 358 | size_t index = 0; 359 | if (object == NULL || name == NULL || value == NULL) { 360 | return JSONFailure; 361 | } 362 | if (json_object_get_value(object, name) != NULL) { 363 | return JSONFailure; 364 | } 365 | if (object->count >= object->capacity) { 366 | size_t new_capacity = MAX(object->capacity * 2, STARTING_CAPACITY); 367 | if (json_object_resize(object, new_capacity) == JSONFailure) { 368 | return JSONFailure; 369 | } 370 | } 371 | index = object->count; 372 | object->names[index] = parson_strdup(name); 373 | if (object->names[index] == NULL) { 374 | return JSONFailure; 375 | } 376 | value->parent = json_object_get_wrapping_value(object); 377 | object->values[index] = value; 378 | object->count++; 379 | return JSONSuccess; 380 | } 381 | 382 | static JSON_Status json_object_resize(JSON_Object *object, size_t new_capacity) { 383 | char **temp_names = NULL; 384 | JSON_Value **temp_values = NULL; 385 | 386 | if ((object->names == NULL && object->values != NULL) || 387 | (object->names != NULL && object->values == NULL) || 388 | new_capacity == 0) { 389 | return JSONFailure; /* Shouldn't happen */ 390 | } 391 | temp_names = (char**)parson_malloc(new_capacity * sizeof(char*)); 392 | if (temp_names == NULL) { 393 | return JSONFailure; 394 | } 395 | temp_values = (JSON_Value**)parson_malloc(new_capacity * sizeof(JSON_Value*)); 396 | if (temp_values == NULL) { 397 | parson_free(temp_names); 398 | return JSONFailure; 399 | } 400 | if (object->names != NULL && object->values != NULL && object->count > 0) { 401 | memcpy(temp_names, object->names, object->count * sizeof(char*)); 402 | memcpy(temp_values, object->values, object->count * sizeof(JSON_Value*)); 403 | } 404 | parson_free(object->names); 405 | parson_free(object->values); 406 | object->names = temp_names; 407 | object->values = temp_values; 408 | object->capacity = new_capacity; 409 | return JSONSuccess; 410 | } 411 | 412 | static JSON_Value * json_object_nget_value(const JSON_Object *object, const char *name, size_t n) { 413 | size_t i, name_length; 414 | for (i = 0; i < json_object_get_count(object); i++) { 415 | name_length = strlen(object->names[i]); 416 | if (name_length != n) { 417 | continue; 418 | } 419 | if (strncmp(object->names[i], name, n) == 0) { 420 | return object->values[i]; 421 | } 422 | } 423 | return NULL; 424 | } 425 | 426 | static void json_object_free(JSON_Object *object) { 427 | size_t i; 428 | for (i = 0; i < object->count; i++) { 429 | parson_free(object->names[i]); 430 | json_value_free(object->values[i]); 431 | } 432 | parson_free(object->names); 433 | parson_free(object->values); 434 | parson_free(object); 435 | } 436 | 437 | /* JSON Array */ 438 | static JSON_Array * json_array_init(JSON_Value *wrapping_value) { 439 | JSON_Array *new_array = (JSON_Array*)parson_malloc(sizeof(JSON_Array)); 440 | if (new_array == NULL) { 441 | return NULL; 442 | } 443 | new_array->wrapping_value = wrapping_value; 444 | new_array->items = (JSON_Value**)NULL; 445 | new_array->capacity = 0; 446 | new_array->count = 0; 447 | return new_array; 448 | } 449 | 450 | static JSON_Status json_array_add(JSON_Array *array, JSON_Value *value) { 451 | if (array->count >= array->capacity) { 452 | size_t new_capacity = MAX(array->capacity * 2, STARTING_CAPACITY); 453 | if (json_array_resize(array, new_capacity) == JSONFailure) { 454 | return JSONFailure; 455 | } 456 | } 457 | value->parent = json_array_get_wrapping_value(array); 458 | array->items[array->count] = value; 459 | array->count++; 460 | return JSONSuccess; 461 | } 462 | 463 | static JSON_Status json_array_resize(JSON_Array *array, size_t new_capacity) { 464 | JSON_Value **new_items = NULL; 465 | if (new_capacity == 0) { 466 | return JSONFailure; 467 | } 468 | new_items = (JSON_Value**)parson_malloc(new_capacity * sizeof(JSON_Value*)); 469 | if (new_items == NULL) { 470 | return JSONFailure; 471 | } 472 | if (array->items != NULL && array->count > 0) { 473 | memcpy(new_items, array->items, array->count * sizeof(JSON_Value*)); 474 | } 475 | parson_free(array->items); 476 | array->items = new_items; 477 | array->capacity = new_capacity; 478 | return JSONSuccess; 479 | } 480 | 481 | static void json_array_free(JSON_Array *array) { 482 | size_t i; 483 | for (i = 0; i < array->count; i++) { 484 | json_value_free(array->items[i]); 485 | } 486 | parson_free(array->items); 487 | parson_free(array); 488 | } 489 | 490 | /* JSON Value */ 491 | static JSON_Value * json_value_init_string_no_copy(char *string) { 492 | JSON_Value *new_value = (JSON_Value*)parson_malloc(sizeof(JSON_Value)); 493 | if (!new_value) { 494 | return NULL; 495 | } 496 | new_value->parent = NULL; 497 | new_value->type = JSONString; 498 | new_value->value.string = string; 499 | return new_value; 500 | } 501 | 502 | /* Parser */ 503 | static JSON_Status skip_quotes(const char **string) { 504 | if (**string != '\"') { 505 | return JSONFailure; 506 | } 507 | SKIP_CHAR(string); 508 | while (**string != '\"') { 509 | if (**string == '\0') { 510 | return JSONFailure; 511 | } else if (**string == '\\') { 512 | SKIP_CHAR(string); 513 | if (**string == '\0') { 514 | return JSONFailure; 515 | } 516 | } 517 | SKIP_CHAR(string); 518 | } 519 | SKIP_CHAR(string); 520 | return JSONSuccess; 521 | } 522 | 523 | static int parse_utf16(const char **unprocessed, char **processed) { 524 | unsigned int cp, lead, trail; 525 | int parse_succeeded = 0; 526 | char *processed_ptr = *processed; 527 | const char *unprocessed_ptr = *unprocessed; 528 | unprocessed_ptr++; /* skips u */ 529 | parse_succeeded = parse_utf16_hex(unprocessed_ptr, &cp); 530 | if (!parse_succeeded) { 531 | return JSONFailure; 532 | } 533 | if (cp < 0x80) { 534 | processed_ptr[0] = (char)cp; /* 0xxxxxxx */ 535 | } else if (cp < 0x800) { 536 | processed_ptr[0] = ((cp >> 6) & 0x1F) | 0xC0; /* 110xxxxx */ 537 | processed_ptr[1] = ((cp) & 0x3F) | 0x80; /* 10xxxxxx */ 538 | processed_ptr += 1; 539 | } else if (cp < 0xD800 || cp > 0xDFFF) { 540 | processed_ptr[0] = ((cp >> 12) & 0x0F) | 0xE0; /* 1110xxxx */ 541 | processed_ptr[1] = ((cp >> 6) & 0x3F) | 0x80; /* 10xxxxxx */ 542 | processed_ptr[2] = ((cp) & 0x3F) | 0x80; /* 10xxxxxx */ 543 | processed_ptr += 2; 544 | } else if (cp >= 0xD800 && cp <= 0xDBFF) { /* lead surrogate (0xD800..0xDBFF) */ 545 | lead = cp; 546 | unprocessed_ptr += 4; /* should always be within the buffer, otherwise previous sscanf would fail */ 547 | if (*unprocessed_ptr++ != '\\' || *unprocessed_ptr++ != 'u') { 548 | return JSONFailure; 549 | } 550 | parse_succeeded = parse_utf16_hex(unprocessed_ptr, &trail); 551 | if (!parse_succeeded || trail < 0xDC00 || trail > 0xDFFF) { /* valid trail surrogate? (0xDC00..0xDFFF) */ 552 | return JSONFailure; 553 | } 554 | cp = ((((lead - 0xD800) & 0x3FF) << 10) | ((trail - 0xDC00) & 0x3FF)) + 0x010000; 555 | processed_ptr[0] = (((cp >> 18) & 0x07) | 0xF0); /* 11110xxx */ 556 | processed_ptr[1] = (((cp >> 12) & 0x3F) | 0x80); /* 10xxxxxx */ 557 | processed_ptr[2] = (((cp >> 6) & 0x3F) | 0x80); /* 10xxxxxx */ 558 | processed_ptr[3] = (((cp) & 0x3F) | 0x80); /* 10xxxxxx */ 559 | processed_ptr += 3; 560 | } else { /* trail surrogate before lead surrogate */ 561 | return JSONFailure; 562 | } 563 | unprocessed_ptr += 3; 564 | *processed = processed_ptr; 565 | *unprocessed = unprocessed_ptr; 566 | return JSONSuccess; 567 | } 568 | 569 | 570 | /* Copies and processes passed string up to supplied length. 571 | Example: "\u006Corem ipsum" -> lorem ipsum */ 572 | static char* process_string(const char *input, size_t len) { 573 | const char *input_ptr = input; 574 | size_t initial_size = (len + 1) * sizeof(char); 575 | size_t final_size = 0; 576 | char *output = NULL, *output_ptr = NULL, *resized_output = NULL; 577 | output = (char*)parson_malloc(initial_size); 578 | if (output == NULL) { 579 | goto error; 580 | } 581 | output_ptr = output; 582 | while ((*input_ptr != '\0') && (size_t)(input_ptr - input) < len) { 583 | if (*input_ptr == '\\') { 584 | input_ptr++; 585 | switch (*input_ptr) { 586 | case '\"': *output_ptr = '\"'; break; 587 | case '\\': *output_ptr = '\\'; break; 588 | case '/': *output_ptr = '/'; break; 589 | case 'b': *output_ptr = '\b'; break; 590 | case 'f': *output_ptr = '\f'; break; 591 | case 'n': *output_ptr = '\n'; break; 592 | case 'r': *output_ptr = '\r'; break; 593 | case 't': *output_ptr = '\t'; break; 594 | case 'u': 595 | if (parse_utf16(&input_ptr, &output_ptr) == JSONFailure) { 596 | goto error; 597 | } 598 | break; 599 | default: 600 | goto error; 601 | } 602 | } else if ((unsigned char)*input_ptr < 0x20) { 603 | goto error; /* 0x00-0x19 are invalid characters for json string (http://www.ietf.org/rfc/rfc4627.txt) */ 604 | } else { 605 | *output_ptr = *input_ptr; 606 | } 607 | output_ptr++; 608 | input_ptr++; 609 | } 610 | *output_ptr = '\0'; 611 | /* resize to new length */ 612 | final_size = (size_t)(output_ptr-output) + 1; 613 | /* todo: don't resize if final_size == initial_size */ 614 | resized_output = (char*)parson_malloc(final_size); 615 | if (resized_output == NULL) { 616 | goto error; 617 | } 618 | memcpy(resized_output, output, final_size); 619 | parson_free(output); 620 | return resized_output; 621 | error: 622 | parson_free(output); 623 | return NULL; 624 | } 625 | 626 | /* Return processed contents of a string between quotes and 627 | skips passed argument to a matching quote. */ 628 | static char * get_quoted_string(const char **string) { 629 | const char *string_start = *string; 630 | size_t string_len = 0; 631 | JSON_Status status = skip_quotes(string); 632 | if (status != JSONSuccess) { 633 | return NULL; 634 | } 635 | string_len = *string - string_start - 2; /* length without quotes */ 636 | return process_string(string_start + 1, string_len); 637 | } 638 | 639 | static JSON_Value * parse_value(const char **string, size_t nesting) { 640 | if (nesting > MAX_NESTING) { 641 | return NULL; 642 | } 643 | SKIP_WHITESPACES(string); 644 | switch (**string) { 645 | case '{': 646 | return parse_object_value(string, nesting + 1); 647 | case '[': 648 | return parse_array_value(string, nesting + 1); 649 | case '\"': 650 | return parse_string_value(string); 651 | case 'f': case 't': 652 | return parse_boolean_value(string); 653 | case '-': 654 | case '0': case '1': case '2': case '3': case '4': 655 | case '5': case '6': case '7': case '8': case '9': 656 | return parse_number_value(string); 657 | case 'n': 658 | return parse_null_value(string); 659 | default: 660 | return NULL; 661 | } 662 | } 663 | 664 | static JSON_Value * parse_object_value(const char **string, size_t nesting) { 665 | JSON_Value *output_value = json_value_init_object(), *new_value = NULL; 666 | JSON_Object *output_object = json_value_get_object(output_value); 667 | char *new_key = NULL; 668 | if (output_value == NULL || **string != '{') { 669 | return NULL; 670 | } 671 | SKIP_CHAR(string); 672 | SKIP_WHITESPACES(string); 673 | if (**string == '}') { /* empty object */ 674 | SKIP_CHAR(string); 675 | return output_value; 676 | } 677 | while (**string != '\0') { 678 | new_key = get_quoted_string(string); 679 | if (new_key == NULL) { 680 | json_value_free(output_value); 681 | return NULL; 682 | } 683 | SKIP_WHITESPACES(string); 684 | if (**string != ':') { 685 | parson_free(new_key); 686 | json_value_free(output_value); 687 | return NULL; 688 | } 689 | SKIP_CHAR(string); 690 | new_value = parse_value(string, nesting); 691 | if (new_value == NULL) { 692 | parson_free(new_key); 693 | json_value_free(output_value); 694 | return NULL; 695 | } 696 | if (json_object_add(output_object, new_key, new_value) == JSONFailure) { 697 | parson_free(new_key); 698 | json_value_free(new_value); 699 | json_value_free(output_value); 700 | return NULL; 701 | } 702 | parson_free(new_key); 703 | SKIP_WHITESPACES(string); 704 | if (**string != ',') { 705 | break; 706 | } 707 | SKIP_CHAR(string); 708 | SKIP_WHITESPACES(string); 709 | } 710 | SKIP_WHITESPACES(string); 711 | if (**string != '}' || /* Trim object after parsing is over */ 712 | json_object_resize(output_object, json_object_get_count(output_object)) == JSONFailure) { 713 | json_value_free(output_value); 714 | return NULL; 715 | } 716 | SKIP_CHAR(string); 717 | return output_value; 718 | } 719 | 720 | static JSON_Value * parse_array_value(const char **string, size_t nesting) { 721 | JSON_Value *output_value = json_value_init_array(), *new_array_value = NULL; 722 | JSON_Array *output_array = json_value_get_array(output_value); 723 | if (!output_value || **string != '[') { 724 | return NULL; 725 | } 726 | SKIP_CHAR(string); 727 | SKIP_WHITESPACES(string); 728 | if (**string == ']') { /* empty array */ 729 | SKIP_CHAR(string); 730 | return output_value; 731 | } 732 | while (**string != '\0') { 733 | new_array_value = parse_value(string, nesting); 734 | if (new_array_value == NULL) { 735 | json_value_free(output_value); 736 | return NULL; 737 | } 738 | if (json_array_add(output_array, new_array_value) == JSONFailure) { 739 | json_value_free(new_array_value); 740 | json_value_free(output_value); 741 | return NULL; 742 | } 743 | SKIP_WHITESPACES(string); 744 | if (**string != ',') { 745 | break; 746 | } 747 | SKIP_CHAR(string); 748 | SKIP_WHITESPACES(string); 749 | } 750 | SKIP_WHITESPACES(string); 751 | if (**string != ']' || /* Trim array after parsing is over */ 752 | json_array_resize(output_array, json_array_get_count(output_array)) == JSONFailure) { 753 | json_value_free(output_value); 754 | return NULL; 755 | } 756 | SKIP_CHAR(string); 757 | return output_value; 758 | } 759 | 760 | static JSON_Value * parse_string_value(const char **string) { 761 | JSON_Value *value = NULL; 762 | char *new_string = get_quoted_string(string); 763 | if (new_string == NULL) { 764 | return NULL; 765 | } 766 | value = json_value_init_string_no_copy(new_string); 767 | if (value == NULL) { 768 | parson_free(new_string); 769 | return NULL; 770 | } 771 | return value; 772 | } 773 | 774 | static JSON_Value * parse_boolean_value(const char **string) { 775 | size_t true_token_size = SIZEOF_TOKEN("true"); 776 | size_t false_token_size = SIZEOF_TOKEN("false"); 777 | if (strncmp("true", *string, true_token_size) == 0) { 778 | *string += true_token_size; 779 | return json_value_init_boolean(1); 780 | } else if (strncmp("false", *string, false_token_size) == 0) { 781 | *string += false_token_size; 782 | return json_value_init_boolean(0); 783 | } 784 | return NULL; 785 | } 786 | 787 | static JSON_Value * parse_number_value(const char **string) { 788 | char *end; 789 | double number = 0; 790 | #ifdef JSON_FIXED_NUMBER 791 | intmax_t fixed; 792 | errno = 0; 793 | fixed = strtoimax(*string, &end, 10); 794 | if (errno == 0 && INT64_MIN <= fixed && fixed <= INT64_MAX && end != NULL && 795 | (*end == 0x00 || (*end != '.' && *end != 'e' && *end != 'E'))) { 796 | *string = end; 797 | return json_value_init_fixed(fixed); 798 | } 799 | #endif 800 | errno = 0; 801 | number = strtod(*string, &end); 802 | if (errno || !is_decimal(*string, end - *string)) { 803 | return NULL; 804 | } 805 | *string = end; 806 | return json_value_init_number(number); 807 | } 808 | 809 | static JSON_Value * parse_null_value(const char **string) { 810 | size_t token_size = SIZEOF_TOKEN("null"); 811 | if (strncmp("null", *string, token_size) == 0) { 812 | *string += token_size; 813 | return json_value_init_null(); 814 | } 815 | return NULL; 816 | } 817 | 818 | /* Serialization */ 819 | #define APPEND_STRING(str) do { written = append_string(buf, (str));\ 820 | if (written < 0) { return -1; }\ 821 | if (buf != NULL) { buf += written; }\ 822 | written_total += written; } while(0) 823 | 824 | #define APPEND_INDENT(level) do { written = append_indent(buf, (level));\ 825 | if (written < 0) { return -1; }\ 826 | if (buf != NULL) { buf += written; }\ 827 | written_total += written; } while(0) 828 | 829 | static int json_serialize_to_buffer_r(const JSON_Value *value, char *buf, int level, int is_pretty, char *num_buf) 830 | { 831 | const char *key = NULL, *string = NULL; 832 | JSON_Value *temp_value = NULL; 833 | JSON_Array *array = NULL; 834 | JSON_Object *object = NULL; 835 | size_t i = 0, count = 0; 836 | double num = 0.0; 837 | #ifdef JSON_FIXED_NUMBER 838 | intmax_t inum = 0; 839 | #endif 840 | int written = -1, written_total = 0; 841 | 842 | switch (json_value_get_type(value)) { 843 | case JSONArray: 844 | array = json_value_get_array(value); 845 | count = json_array_get_count(array); 846 | APPEND_STRING("["); 847 | if (count > 0 && is_pretty) { 848 | APPEND_STRING("\n"); 849 | } 850 | for (i = 0; i < count; i++) { 851 | if (is_pretty) { 852 | APPEND_INDENT(level+1); 853 | } 854 | temp_value = json_array_get_value(array, i); 855 | written = json_serialize_to_buffer_r(temp_value, buf, level+1, is_pretty, num_buf); 856 | if (written < 0) { 857 | return -1; 858 | } 859 | if (buf != NULL) { 860 | buf += written; 861 | } 862 | written_total += written; 863 | if (i < (count - 1)) { 864 | APPEND_STRING(","); 865 | } 866 | if (is_pretty) { 867 | APPEND_STRING("\n"); 868 | } 869 | } 870 | if (count > 0 && is_pretty) { 871 | APPEND_INDENT(level); 872 | } 873 | APPEND_STRING("]"); 874 | return written_total; 875 | case JSONObject: 876 | object = json_value_get_object(value); 877 | count = json_object_get_count(object); 878 | APPEND_STRING("{"); 879 | if (count > 0 && is_pretty) { 880 | APPEND_STRING("\n"); 881 | } 882 | for (i = 0; i < count; i++) { 883 | key = json_object_get_name(object, i); 884 | if (key == NULL) { 885 | return -1; 886 | } 887 | if (is_pretty) { 888 | APPEND_INDENT(level+1); 889 | } 890 | written = json_serialize_string(key, buf); 891 | if (written < 0) { 892 | return -1; 893 | } 894 | if (buf != NULL) { 895 | buf += written; 896 | } 897 | written_total += written; 898 | APPEND_STRING(":"); 899 | if (is_pretty) { 900 | APPEND_STRING(" "); 901 | } 902 | temp_value = json_object_get_value(object, key); 903 | written = json_serialize_to_buffer_r(temp_value, buf, level+1, is_pretty, num_buf); 904 | if (written < 0) { 905 | return -1; 906 | } 907 | if (buf != NULL) { 908 | buf += written; 909 | } 910 | written_total += written; 911 | if (i < (count - 1)) { 912 | APPEND_STRING(","); 913 | } 914 | if (is_pretty) { 915 | APPEND_STRING("\n"); 916 | } 917 | } 918 | if (count > 0 && is_pretty) { 919 | APPEND_INDENT(level); 920 | } 921 | APPEND_STRING("}"); 922 | return written_total; 923 | case JSONString: 924 | string = json_value_get_string(value); 925 | if (string == NULL) { 926 | return -1; 927 | } 928 | written = json_serialize_string(string, buf); 929 | if (written < 0) { 930 | return -1; 931 | } 932 | if (buf != NULL) { 933 | buf += written; 934 | } 935 | written_total += written; 936 | return written_total; 937 | case JSONBoolean: 938 | if (json_value_get_boolean(value)) { 939 | APPEND_STRING("true"); 940 | } else { 941 | APPEND_STRING("false"); 942 | } 943 | return written_total; 944 | #ifdef JSON_FIXED_NUMBER 945 | case JSONFixed: 946 | inum = json_value_get_fixed(value); 947 | if (buf != NULL) { 948 | num_buf = buf; 949 | } 950 | written = sprintf(num_buf, FIXED_FORMAT, inum); 951 | if (written < 0) { 952 | return -1; 953 | } 954 | if (buf != NULL) { 955 | buf += written; 956 | } 957 | written_total += written; 958 | return written_total; 959 | #endif 960 | case JSONNumber: 961 | num = json_value_get_number(value); 962 | if (buf != NULL) { 963 | num_buf = buf; 964 | } 965 | written = sprintf(num_buf, FLOAT_FORMAT, num); 966 | if (written < 0) { 967 | return -1; 968 | } 969 | if (buf != NULL) { 970 | buf += written; 971 | } 972 | written_total += written; 973 | return written_total; 974 | case JSONNull: 975 | APPEND_STRING("null"); 976 | return written_total; 977 | case JSONError: 978 | return -1; 979 | default: 980 | return -1; 981 | } 982 | } 983 | 984 | static int json_serialize_string(const char *string, char *buf) { 985 | size_t i = 0, len = strlen(string); 986 | char c = '\0'; 987 | int written = -1, written_total = 0; 988 | APPEND_STRING("\""); 989 | for (i = 0; i < len; i++) { 990 | c = string[i]; 991 | switch (c) { 992 | case '\"': APPEND_STRING("\\\""); break; 993 | case '\\': APPEND_STRING("\\\\"); break; 994 | case '/': APPEND_STRING("\\/"); break; /* to make json embeddable in xml\/html */ 995 | case '\b': APPEND_STRING("\\b"); break; 996 | case '\f': APPEND_STRING("\\f"); break; 997 | case '\n': APPEND_STRING("\\n"); break; 998 | case '\r': APPEND_STRING("\\r"); break; 999 | case '\t': APPEND_STRING("\\t"); break; 1000 | case '\x00': APPEND_STRING("\\u0000"); break; 1001 | case '\x01': APPEND_STRING("\\u0001"); break; 1002 | case '\x02': APPEND_STRING("\\u0002"); break; 1003 | case '\x03': APPEND_STRING("\\u0003"); break; 1004 | case '\x04': APPEND_STRING("\\u0004"); break; 1005 | case '\x05': APPEND_STRING("\\u0005"); break; 1006 | case '\x06': APPEND_STRING("\\u0006"); break; 1007 | case '\x07': APPEND_STRING("\\u0007"); break; 1008 | /* '\x08' duplicate: '\b' */ 1009 | /* '\x09' duplicate: '\t' */ 1010 | /* '\x0a' duplicate: '\n' */ 1011 | case '\x0b': APPEND_STRING("\\u000b"); break; 1012 | /* '\x0c' duplicate: '\f' */ 1013 | /* '\x0d' duplicate: '\r' */ 1014 | case '\x0e': APPEND_STRING("\\u000e"); break; 1015 | case '\x0f': APPEND_STRING("\\u000f"); break; 1016 | case '\x10': APPEND_STRING("\\u0010"); break; 1017 | case '\x11': APPEND_STRING("\\u0011"); break; 1018 | case '\x12': APPEND_STRING("\\u0012"); break; 1019 | case '\x13': APPEND_STRING("\\u0013"); break; 1020 | case '\x14': APPEND_STRING("\\u0014"); break; 1021 | case '\x15': APPEND_STRING("\\u0015"); break; 1022 | case '\x16': APPEND_STRING("\\u0016"); break; 1023 | case '\x17': APPEND_STRING("\\u0017"); break; 1024 | case '\x18': APPEND_STRING("\\u0018"); break; 1025 | case '\x19': APPEND_STRING("\\u0019"); break; 1026 | case '\x1a': APPEND_STRING("\\u001a"); break; 1027 | case '\x1b': APPEND_STRING("\\u001b"); break; 1028 | case '\x1c': APPEND_STRING("\\u001c"); break; 1029 | case '\x1d': APPEND_STRING("\\u001d"); break; 1030 | case '\x1e': APPEND_STRING("\\u001e"); break; 1031 | case '\x1f': APPEND_STRING("\\u001f"); break; 1032 | default: 1033 | if (buf != NULL) { 1034 | buf[0] = c; 1035 | buf += 1; 1036 | } 1037 | written_total += 1; 1038 | break; 1039 | } 1040 | } 1041 | APPEND_STRING("\""); 1042 | return written_total; 1043 | } 1044 | 1045 | static int append_indent(char *buf, int level) { 1046 | int i; 1047 | int written = -1, written_total = 0; 1048 | for (i = 0; i < level; i++) { 1049 | APPEND_STRING(" "); 1050 | } 1051 | return written_total; 1052 | } 1053 | 1054 | static int append_string(char *buf, const char *string) { 1055 | if (buf == NULL) { 1056 | return (int)strlen(string); 1057 | } 1058 | return sprintf(buf, "%s", string); 1059 | } 1060 | 1061 | #undef APPEND_STRING 1062 | #undef APPEND_INDENT 1063 | 1064 | /* Parser API */ 1065 | #ifndef MRB_DISABLE_STDIO 1066 | JSON_Value * json_parse_file(const char *filename) { 1067 | char *file_contents = read_file(filename); 1068 | JSON_Value *output_value = NULL; 1069 | if (file_contents == NULL) { 1070 | return NULL; 1071 | } 1072 | output_value = json_parse_string(file_contents); 1073 | parson_free(file_contents); 1074 | return output_value; 1075 | } 1076 | 1077 | JSON_Value * json_parse_file_with_comments(const char *filename) { 1078 | char *file_contents = read_file(filename); 1079 | JSON_Value *output_value = NULL; 1080 | if (file_contents == NULL) { 1081 | return NULL; 1082 | } 1083 | output_value = json_parse_string_with_comments(file_contents); 1084 | parson_free(file_contents); 1085 | return output_value; 1086 | } 1087 | #endif 1088 | 1089 | JSON_Value * json_parse_string(const char *string) { 1090 | if (string == NULL) { 1091 | return NULL; 1092 | } 1093 | if (string[0] == '\xEF' && string[1] == '\xBB' && string[2] == '\xBF') { 1094 | string = string + 3; /* Support for UTF-8 BOM */ 1095 | } 1096 | return parse_value((const char**)&string, 0); 1097 | } 1098 | 1099 | JSON_Value * json_parse_string_with_comments(const char *string) { 1100 | JSON_Value *result = NULL; 1101 | char *string_mutable_copy = NULL, *string_mutable_copy_ptr = NULL; 1102 | string_mutable_copy = parson_strdup(string); 1103 | if (string_mutable_copy == NULL) { 1104 | return NULL; 1105 | } 1106 | remove_comments(string_mutable_copy, "/*", "*/"); 1107 | remove_comments(string_mutable_copy, "//", "\n"); 1108 | string_mutable_copy_ptr = string_mutable_copy; 1109 | result = parse_value((const char**)&string_mutable_copy_ptr, 0); 1110 | parson_free(string_mutable_copy); 1111 | return result; 1112 | } 1113 | 1114 | /* JSON Object API */ 1115 | 1116 | JSON_Value * json_object_get_value(const JSON_Object *object, const char *name) { 1117 | if (object == NULL || name == NULL) { 1118 | return NULL; 1119 | } 1120 | return json_object_nget_value(object, name, strlen(name)); 1121 | } 1122 | 1123 | const char * json_object_get_string(const JSON_Object *object, const char *name) { 1124 | return json_value_get_string(json_object_get_value(object, name)); 1125 | } 1126 | 1127 | double json_object_get_number(const JSON_Object *object, const char *name) { 1128 | return json_value_get_number(json_object_get_value(object, name)); 1129 | } 1130 | 1131 | #ifdef JSON_FIXED_NUMBER 1132 | intmax_t json_object_get_fixed(const JSON_Object *object, const char *name) { 1133 | return json_value_get_fixed(json_object_get_value(object, name)); 1134 | } 1135 | #endif 1136 | 1137 | JSON_Object * json_object_get_object(const JSON_Object *object, const char *name) { 1138 | return json_value_get_object(json_object_get_value(object, name)); 1139 | } 1140 | 1141 | JSON_Array * json_object_get_array(const JSON_Object *object, const char *name) { 1142 | return json_value_get_array(json_object_get_value(object, name)); 1143 | } 1144 | 1145 | int json_object_get_boolean(const JSON_Object *object, const char *name) { 1146 | return json_value_get_boolean(json_object_get_value(object, name)); 1147 | } 1148 | 1149 | JSON_Value * json_object_dotget_value(const JSON_Object *object, const char *name) { 1150 | const char *dot_position = strchr(name, '.'); 1151 | if (!dot_position) { 1152 | return json_object_get_value(object, name); 1153 | } 1154 | object = json_value_get_object(json_object_nget_value(object, name, dot_position - name)); 1155 | return json_object_dotget_value(object, dot_position + 1); 1156 | } 1157 | 1158 | const char * json_object_dotget_string(const JSON_Object *object, const char *name) { 1159 | return json_value_get_string(json_object_dotget_value(object, name)); 1160 | } 1161 | 1162 | double json_object_dotget_number(const JSON_Object *object, const char *name) { 1163 | return json_value_get_number(json_object_dotget_value(object, name)); 1164 | } 1165 | 1166 | #ifdef JSON_FIXED_NUMBER 1167 | intmax_t json_object_dotget_fixed(const JSON_Object *object, const char *name) { 1168 | return json_value_get_fixed(json_object_dotget_value(object, name)); 1169 | } 1170 | #endif 1171 | 1172 | JSON_Object * json_object_dotget_object(const JSON_Object *object, const char *name) { 1173 | return json_value_get_object(json_object_dotget_value(object, name)); 1174 | } 1175 | 1176 | JSON_Array * json_object_dotget_array(const JSON_Object *object, const char *name) { 1177 | return json_value_get_array(json_object_dotget_value(object, name)); 1178 | } 1179 | 1180 | int json_object_dotget_boolean(const JSON_Object *object, const char *name) { 1181 | return json_value_get_boolean(json_object_dotget_value(object, name)); 1182 | } 1183 | 1184 | size_t json_object_get_count(const JSON_Object *object) { 1185 | return object ? object->count : 0; 1186 | } 1187 | 1188 | const char * json_object_get_name(const JSON_Object *object, size_t index) { 1189 | if (object == NULL || index >= json_object_get_count(object)) { 1190 | return NULL; 1191 | } 1192 | return object->names[index]; 1193 | } 1194 | 1195 | JSON_Value * json_object_get_value_at(const JSON_Object *object, size_t index) { 1196 | if (object == NULL || index >= json_object_get_count(object)) { 1197 | return NULL; 1198 | } 1199 | return object->values[index]; 1200 | } 1201 | 1202 | JSON_Value *json_object_get_wrapping_value(const JSON_Object *object) { 1203 | return object->wrapping_value; 1204 | } 1205 | 1206 | int json_object_has_value (const JSON_Object *object, const char *name) { 1207 | return json_object_get_value(object, name) != NULL; 1208 | } 1209 | 1210 | int json_object_has_value_of_type(const JSON_Object *object, const char *name, JSON_Value_Type type) { 1211 | JSON_Value *val = json_object_get_value(object, name); 1212 | return val != NULL && json_value_get_type(val) == type; 1213 | } 1214 | 1215 | int json_object_dothas_value (const JSON_Object *object, const char *name) { 1216 | return json_object_dotget_value(object, name) != NULL; 1217 | } 1218 | 1219 | int json_object_dothas_value_of_type(const JSON_Object *object, const char *name, JSON_Value_Type type) { 1220 | JSON_Value *val = json_object_dotget_value(object, name); 1221 | return val != NULL && json_value_get_type(val) == type; 1222 | } 1223 | 1224 | /* JSON Array API */ 1225 | JSON_Value * json_array_get_value(const JSON_Array *array, size_t index) { 1226 | if (array == NULL || index >= json_array_get_count(array)) { 1227 | return NULL; 1228 | } 1229 | return array->items[index]; 1230 | } 1231 | 1232 | const char * json_array_get_string(const JSON_Array *array, size_t index) { 1233 | return json_value_get_string(json_array_get_value(array, index)); 1234 | } 1235 | 1236 | double json_array_get_number(const JSON_Array *array, size_t index) { 1237 | return json_value_get_number(json_array_get_value(array, index)); 1238 | } 1239 | 1240 | #ifdef JSON_FIXED_NUMBER 1241 | intmax_t json_array_get_fixed(const JSON_Array *array, size_t index) { 1242 | return json_value_get_fixed(json_array_get_value(array, index)); 1243 | } 1244 | #endif 1245 | 1246 | JSON_Object * json_array_get_object(const JSON_Array *array, size_t index) { 1247 | return json_value_get_object(json_array_get_value(array, index)); 1248 | } 1249 | 1250 | JSON_Array * json_array_get_array(const JSON_Array *array, size_t index) { 1251 | return json_value_get_array(json_array_get_value(array, index)); 1252 | } 1253 | 1254 | int json_array_get_boolean(const JSON_Array *array, size_t index) { 1255 | return json_value_get_boolean(json_array_get_value(array, index)); 1256 | } 1257 | 1258 | size_t json_array_get_count(const JSON_Array *array) { 1259 | return array ? array->count : 0; 1260 | } 1261 | 1262 | JSON_Value * json_array_get_wrapping_value(const JSON_Array *array) { 1263 | return array->wrapping_value; 1264 | } 1265 | 1266 | /* JSON Value API */ 1267 | JSON_Value_Type json_value_get_type(const JSON_Value *value) { 1268 | return value ? value->type : JSONError; 1269 | } 1270 | 1271 | JSON_Object * json_value_get_object(const JSON_Value *value) { 1272 | return json_value_get_type(value) == JSONObject ? value->value.object : NULL; 1273 | } 1274 | 1275 | JSON_Array * json_value_get_array(const JSON_Value *value) { 1276 | return json_value_get_type(value) == JSONArray ? value->value.array : NULL; 1277 | } 1278 | 1279 | const char * json_value_get_string(const JSON_Value *value) { 1280 | return json_value_get_type(value) == JSONString ? value->value.string : NULL; 1281 | } 1282 | 1283 | double json_value_get_number(const JSON_Value *value) { 1284 | return json_value_get_type(value) == JSONNumber ? value->value.number : 0; 1285 | } 1286 | 1287 | #ifdef JSON_FIXED_NUMBER 1288 | intmax_t json_value_get_fixed(const JSON_Value *value) { 1289 | return json_value_get_type(value) == JSONFixed ? value->value.fixed : 0; 1290 | } 1291 | #endif 1292 | 1293 | int json_value_get_boolean(const JSON_Value *value) { 1294 | return json_value_get_type(value) == JSONBoolean ? value->value.boolean : -1; 1295 | } 1296 | 1297 | JSON_Value * json_value_get_parent (const JSON_Value *value) { 1298 | return value ? value->parent : NULL; 1299 | } 1300 | 1301 | void json_value_free(JSON_Value *value) { 1302 | switch (json_value_get_type(value)) { 1303 | case JSONObject: 1304 | json_object_free(value->value.object); 1305 | break; 1306 | case JSONString: 1307 | parson_free(value->value.string); 1308 | break; 1309 | case JSONArray: 1310 | json_array_free(value->value.array); 1311 | break; 1312 | default: 1313 | break; 1314 | } 1315 | parson_free(value); 1316 | } 1317 | 1318 | JSON_Value * json_value_init_object(void) { 1319 | JSON_Value *new_value = (JSON_Value*)parson_malloc(sizeof(JSON_Value)); 1320 | if (!new_value) { 1321 | return NULL; 1322 | } 1323 | new_value->parent = NULL; 1324 | new_value->type = JSONObject; 1325 | new_value->value.object = json_object_init(new_value); 1326 | if (!new_value->value.object) { 1327 | parson_free(new_value); 1328 | return NULL; 1329 | } 1330 | return new_value; 1331 | } 1332 | 1333 | JSON_Value * json_value_init_array(void) { 1334 | JSON_Value *new_value = (JSON_Value*)parson_malloc(sizeof(JSON_Value)); 1335 | if (!new_value) { 1336 | return NULL; 1337 | } 1338 | new_value->parent = NULL; 1339 | new_value->type = JSONArray; 1340 | new_value->value.array = json_array_init(new_value); 1341 | if (!new_value->value.array) { 1342 | parson_free(new_value); 1343 | return NULL; 1344 | } 1345 | return new_value; 1346 | } 1347 | 1348 | JSON_Value * json_value_init_string(const char *string) { 1349 | char *copy = NULL; 1350 | JSON_Value *value; 1351 | size_t string_len = 0; 1352 | if (string == NULL) { 1353 | return NULL; 1354 | } 1355 | string_len = strlen(string); 1356 | if (!is_valid_utf8(string, string_len)) { 1357 | return NULL; 1358 | } 1359 | copy = parson_strndup(string, string_len); 1360 | if (copy == NULL) { 1361 | return NULL; 1362 | } 1363 | value = json_value_init_string_no_copy(copy); 1364 | if (value == NULL) { 1365 | parson_free(copy); 1366 | } 1367 | return value; 1368 | } 1369 | 1370 | JSON_Value * json_value_init_number(double number) { 1371 | JSON_Value *new_value = NULL; 1372 | if ((number * 0.0) != 0.0) { /* nan and inf test */ 1373 | return NULL; 1374 | } 1375 | new_value = (JSON_Value*)parson_malloc(sizeof(JSON_Value)); 1376 | if (new_value == NULL) { 1377 | return NULL; 1378 | } 1379 | new_value->parent = NULL; 1380 | new_value->type = JSONNumber; 1381 | new_value->value.number = number; 1382 | return new_value; 1383 | } 1384 | 1385 | #ifdef JSON_FIXED_NUMBER 1386 | JSON_Value * json_value_init_fixed(intmax_t fixed) { 1387 | JSON_Value *new_value = NULL; 1388 | new_value = (JSON_Value*)parson_malloc(sizeof(JSON_Value)); 1389 | if (new_value == NULL) { 1390 | return NULL; 1391 | } 1392 | new_value->parent = NULL; 1393 | new_value->type = JSONFixed; 1394 | new_value->value.fixed = fixed; 1395 | return new_value; 1396 | } 1397 | #endif 1398 | 1399 | JSON_Value * json_value_init_boolean(int boolean) { 1400 | JSON_Value *new_value = (JSON_Value*)parson_malloc(sizeof(JSON_Value)); 1401 | if (!new_value) { 1402 | return NULL; 1403 | } 1404 | new_value->parent = NULL; 1405 | new_value->type = JSONBoolean; 1406 | new_value->value.boolean = boolean ? 1 : 0; 1407 | return new_value; 1408 | } 1409 | 1410 | JSON_Value * json_value_init_null(void) { 1411 | JSON_Value *new_value = (JSON_Value*)parson_malloc(sizeof(JSON_Value)); 1412 | if (!new_value) { 1413 | return NULL; 1414 | } 1415 | new_value->parent = NULL; 1416 | new_value->type = JSONNull; 1417 | return new_value; 1418 | } 1419 | 1420 | JSON_Value * json_value_deep_copy(const JSON_Value *value) { 1421 | size_t i = 0; 1422 | JSON_Value *return_value = NULL, *temp_value_copy = NULL, *temp_value = NULL; 1423 | const char *temp_string = NULL, *temp_key = NULL; 1424 | char *temp_string_copy = NULL; 1425 | JSON_Array *temp_array = NULL, *temp_array_copy = NULL; 1426 | JSON_Object *temp_object = NULL, *temp_object_copy = NULL; 1427 | 1428 | switch (json_value_get_type(value)) { 1429 | case JSONArray: 1430 | temp_array = json_value_get_array(value); 1431 | return_value = json_value_init_array(); 1432 | if (return_value == NULL) { 1433 | return NULL; 1434 | } 1435 | temp_array_copy = json_value_get_array(return_value); 1436 | for (i = 0; i < json_array_get_count(temp_array); i++) { 1437 | temp_value = json_array_get_value(temp_array, i); 1438 | temp_value_copy = json_value_deep_copy(temp_value); 1439 | if (temp_value_copy == NULL) { 1440 | json_value_free(return_value); 1441 | return NULL; 1442 | } 1443 | if (json_array_add(temp_array_copy, temp_value_copy) == JSONFailure) { 1444 | json_value_free(return_value); 1445 | json_value_free(temp_value_copy); 1446 | return NULL; 1447 | } 1448 | } 1449 | return return_value; 1450 | case JSONObject: 1451 | temp_object = json_value_get_object(value); 1452 | return_value = json_value_init_object(); 1453 | if (return_value == NULL) { 1454 | return NULL; 1455 | } 1456 | temp_object_copy = json_value_get_object(return_value); 1457 | for (i = 0; i < json_object_get_count(temp_object); i++) { 1458 | temp_key = json_object_get_name(temp_object, i); 1459 | temp_value = json_object_get_value(temp_object, temp_key); 1460 | temp_value_copy = json_value_deep_copy(temp_value); 1461 | if (temp_value_copy == NULL) { 1462 | json_value_free(return_value); 1463 | return NULL; 1464 | } 1465 | if (json_object_add(temp_object_copy, temp_key, temp_value_copy) == JSONFailure) { 1466 | json_value_free(return_value); 1467 | json_value_free(temp_value_copy); 1468 | return NULL; 1469 | } 1470 | } 1471 | return return_value; 1472 | case JSONBoolean: 1473 | return json_value_init_boolean(json_value_get_boolean(value)); 1474 | case JSONNumber: 1475 | return json_value_init_number(json_value_get_number(value)); 1476 | #ifdef JSON_FIXED_NUMBER 1477 | case JSONFixed: 1478 | return json_value_init_fixed(json_value_get_fixed(value)); 1479 | #endif 1480 | case JSONString: 1481 | temp_string = json_value_get_string(value); 1482 | if (temp_string == NULL) { 1483 | return NULL; 1484 | } 1485 | temp_string_copy = parson_strdup(temp_string); 1486 | if (temp_string_copy == NULL) { 1487 | return NULL; 1488 | } 1489 | return_value = json_value_init_string_no_copy(temp_string_copy); 1490 | if (return_value == NULL) { 1491 | parson_free(temp_string_copy); 1492 | } 1493 | return return_value; 1494 | case JSONNull: 1495 | return json_value_init_null(); 1496 | case JSONError: 1497 | return NULL; 1498 | default: 1499 | return NULL; 1500 | } 1501 | } 1502 | 1503 | size_t json_serialization_size(const JSON_Value *value) { 1504 | char num_buf[1100]; /* recursively allocating buffer on stack is a bad idea, so let's do it only once */ 1505 | int res = json_serialize_to_buffer_r(value, NULL, 0, 0, num_buf); 1506 | return res < 0 ? 0 : (size_t)(res + 1); 1507 | } 1508 | 1509 | JSON_Status json_serialize_to_buffer(const JSON_Value *value, char *buf, size_t buf_size_in_bytes) { 1510 | int written = -1; 1511 | size_t needed_size_in_bytes = json_serialization_size(value); 1512 | if (needed_size_in_bytes == 0 || buf_size_in_bytes < needed_size_in_bytes) { 1513 | return JSONFailure; 1514 | } 1515 | written = json_serialize_to_buffer_r(value, buf, 0, 0, NULL); 1516 | if (written < 0) { 1517 | return JSONFailure; 1518 | } 1519 | return JSONSuccess; 1520 | } 1521 | 1522 | #ifndef MRB_DISABLE_STDIO 1523 | JSON_Status json_serialize_to_file(const JSON_Value *value, const char *filename) { 1524 | JSON_Status return_code = JSONSuccess; 1525 | FILE *fp = NULL; 1526 | char *serialized_string = json_serialize_to_string(value); 1527 | if (serialized_string == NULL) { 1528 | return JSONFailure; 1529 | } 1530 | fp = fopen(filename, "w"); 1531 | if (fp == NULL) { 1532 | json_free_serialized_string(serialized_string); 1533 | return JSONFailure; 1534 | } 1535 | if (fputs(serialized_string, fp) == EOF) { 1536 | return_code = JSONFailure; 1537 | } 1538 | if (fclose(fp) == EOF) { 1539 | return_code = JSONFailure; 1540 | } 1541 | json_free_serialized_string(serialized_string); 1542 | return return_code; 1543 | } 1544 | #endif 1545 | 1546 | char * json_serialize_to_string(const JSON_Value *value) { 1547 | JSON_Status serialization_result = JSONFailure; 1548 | size_t buf_size_bytes = json_serialization_size(value); 1549 | char *buf = NULL; 1550 | if (buf_size_bytes == 0) { 1551 | return NULL; 1552 | } 1553 | buf = (char*)parson_malloc(buf_size_bytes); 1554 | if (buf == NULL) { 1555 | return NULL; 1556 | } 1557 | serialization_result = json_serialize_to_buffer(value, buf, buf_size_bytes); 1558 | if (serialization_result == JSONFailure) { 1559 | json_free_serialized_string(buf); 1560 | return NULL; 1561 | } 1562 | return buf; 1563 | } 1564 | 1565 | size_t json_serialization_size_pretty(const JSON_Value *value) { 1566 | char num_buf[1100]; /* recursively allocating buffer on stack is a bad idea, so let's do it only once */ 1567 | int res = json_serialize_to_buffer_r(value, NULL, 0, 1, num_buf); 1568 | return res < 0 ? 0 : (size_t)(res + 1); 1569 | } 1570 | 1571 | JSON_Status json_serialize_to_buffer_pretty(const JSON_Value *value, char *buf, size_t buf_size_in_bytes) { 1572 | int written = -1; 1573 | size_t needed_size_in_bytes = json_serialization_size_pretty(value); 1574 | if (needed_size_in_bytes == 0 || buf_size_in_bytes < needed_size_in_bytes) { 1575 | return JSONFailure; 1576 | } 1577 | written = json_serialize_to_buffer_r(value, buf, 0, 1, NULL); 1578 | if (written < 0) { 1579 | return JSONFailure; 1580 | } 1581 | return JSONSuccess; 1582 | } 1583 | 1584 | #ifndef MRB_DISABLE_STDIO 1585 | JSON_Status json_serialize_to_file_pretty(const JSON_Value *value, const char *filename) { 1586 | JSON_Status return_code = JSONSuccess; 1587 | FILE *fp = NULL; 1588 | char *serialized_string = json_serialize_to_string_pretty(value); 1589 | if (serialized_string == NULL) { 1590 | return JSONFailure; 1591 | } 1592 | fp = fopen(filename, "w"); 1593 | if (fp == NULL) { 1594 | json_free_serialized_string(serialized_string); 1595 | return JSONFailure; 1596 | } 1597 | if (fputs(serialized_string, fp) == EOF) { 1598 | return_code = JSONFailure; 1599 | } 1600 | if (fclose(fp) == EOF) { 1601 | return_code = JSONFailure; 1602 | } 1603 | json_free_serialized_string(serialized_string); 1604 | return return_code; 1605 | } 1606 | #endif 1607 | 1608 | char * json_serialize_to_string_pretty(const JSON_Value *value) { 1609 | JSON_Status serialization_result = JSONFailure; 1610 | size_t buf_size_bytes = json_serialization_size_pretty(value); 1611 | char *buf = NULL; 1612 | if (buf_size_bytes == 0) { 1613 | return NULL; 1614 | } 1615 | buf = (char*)parson_malloc(buf_size_bytes); 1616 | if (buf == NULL) { 1617 | return NULL; 1618 | } 1619 | serialization_result = json_serialize_to_buffer_pretty(value, buf, buf_size_bytes); 1620 | if (serialization_result == JSONFailure) { 1621 | json_free_serialized_string(buf); 1622 | return NULL; 1623 | } 1624 | return buf; 1625 | } 1626 | 1627 | void json_free_serialized_string(char *string) { 1628 | parson_free(string); 1629 | } 1630 | 1631 | JSON_Status json_array_remove(JSON_Array *array, size_t ix) { 1632 | size_t to_move_bytes = 0; 1633 | if (array == NULL || ix >= json_array_get_count(array)) { 1634 | return JSONFailure; 1635 | } 1636 | json_value_free(json_array_get_value(array, ix)); 1637 | to_move_bytes = (json_array_get_count(array) - 1 - ix) * sizeof(JSON_Value*); 1638 | memmove(array->items + ix, array->items + ix + 1, to_move_bytes); 1639 | array->count -= 1; 1640 | return JSONSuccess; 1641 | } 1642 | 1643 | JSON_Status json_array_replace_value(JSON_Array *array, size_t ix, JSON_Value *value) { 1644 | if (array == NULL || value == NULL || value->parent != NULL || ix >= json_array_get_count(array)) { 1645 | return JSONFailure; 1646 | } 1647 | json_value_free(json_array_get_value(array, ix)); 1648 | value->parent = json_array_get_wrapping_value(array); 1649 | array->items[ix] = value; 1650 | return JSONSuccess; 1651 | } 1652 | 1653 | JSON_Status json_array_replace_string(JSON_Array *array, size_t i, const char* string) { 1654 | JSON_Value *value = json_value_init_string(string); 1655 | if (value == NULL) { 1656 | return JSONFailure; 1657 | } 1658 | if (json_array_replace_value(array, i, value) == JSONFailure) { 1659 | json_value_free(value); 1660 | return JSONFailure; 1661 | } 1662 | return JSONSuccess; 1663 | } 1664 | 1665 | JSON_Status json_array_replace_number(JSON_Array *array, size_t i, double number) { 1666 | JSON_Value *value = json_value_init_number(number); 1667 | if (value == NULL) { 1668 | return JSONFailure; 1669 | } 1670 | if (json_array_replace_value(array, i, value) == JSONFailure) { 1671 | json_value_free(value); 1672 | return JSONFailure; 1673 | } 1674 | return JSONSuccess; 1675 | } 1676 | 1677 | #ifdef JSON_FIXED_NUMBER 1678 | JSON_Status json_array_replace_fixed(JSON_Array *array, size_t i, intmax_t fixed) { 1679 | JSON_Value *value = json_value_init_fixed(fixed); 1680 | if (value == NULL) { 1681 | return JSONFailure; 1682 | } 1683 | if (json_array_replace_value(array, i, value) == JSONFailure) { 1684 | json_value_free(value); 1685 | return JSONFailure; 1686 | } 1687 | return JSONSuccess; 1688 | } 1689 | #endif 1690 | 1691 | JSON_Status json_array_replace_boolean(JSON_Array *array, size_t i, int boolean) { 1692 | JSON_Value *value = json_value_init_boolean(boolean); 1693 | if (value == NULL) { 1694 | return JSONFailure; 1695 | } 1696 | if (json_array_replace_value(array, i, value) == JSONFailure) { 1697 | json_value_free(value); 1698 | return JSONFailure; 1699 | } 1700 | return JSONSuccess; 1701 | } 1702 | 1703 | JSON_Status json_array_replace_null(JSON_Array *array, size_t i) { 1704 | JSON_Value *value = json_value_init_null(); 1705 | if (value == NULL) { 1706 | return JSONFailure; 1707 | } 1708 | if (json_array_replace_value(array, i, value) == JSONFailure) { 1709 | json_value_free(value); 1710 | return JSONFailure; 1711 | } 1712 | return JSONSuccess; 1713 | } 1714 | 1715 | JSON_Status json_array_clear(JSON_Array *array) { 1716 | size_t i = 0; 1717 | if (array == NULL) { 1718 | return JSONFailure; 1719 | } 1720 | for (i = 0; i < json_array_get_count(array); i++) { 1721 | json_value_free(json_array_get_value(array, i)); 1722 | } 1723 | array->count = 0; 1724 | return JSONSuccess; 1725 | } 1726 | 1727 | JSON_Status json_array_append_value(JSON_Array *array, JSON_Value *value) { 1728 | if (array == NULL || value == NULL || value->parent != NULL) { 1729 | return JSONFailure; 1730 | } 1731 | return json_array_add(array, value); 1732 | } 1733 | 1734 | JSON_Status json_array_append_string(JSON_Array *array, const char *string) { 1735 | JSON_Value *value = json_value_init_string(string); 1736 | if (value == NULL) { 1737 | return JSONFailure; 1738 | } 1739 | if (json_array_append_value(array, value) == JSONFailure) { 1740 | json_value_free(value); 1741 | return JSONFailure; 1742 | } 1743 | return JSONSuccess; 1744 | } 1745 | 1746 | JSON_Status json_array_append_number(JSON_Array *array, double number) { 1747 | JSON_Value *value = json_value_init_number(number); 1748 | if (value == NULL) { 1749 | return JSONFailure; 1750 | } 1751 | if (json_array_append_value(array, value) == JSONFailure) { 1752 | json_value_free(value); 1753 | return JSONFailure; 1754 | } 1755 | return JSONSuccess; 1756 | } 1757 | 1758 | #ifdef JSON_FIXED_NUMBER 1759 | JSON_Status json_array_append_fixed(JSON_Array *array, intmax_t fixed) { 1760 | JSON_Value *value = json_value_init_fixed(fixed); 1761 | if (value == NULL) { 1762 | return JSONFailure; 1763 | } 1764 | if (json_array_append_value(array, value) == JSONFailure) { 1765 | json_value_free(value); 1766 | return JSONFailure; 1767 | } 1768 | return JSONSuccess; 1769 | } 1770 | #endif 1771 | 1772 | JSON_Status json_array_append_boolean(JSON_Array *array, int boolean) { 1773 | JSON_Value *value = json_value_init_boolean(boolean); 1774 | if (value == NULL) { 1775 | return JSONFailure; 1776 | } 1777 | if (json_array_append_value(array, value) == JSONFailure) { 1778 | json_value_free(value); 1779 | return JSONFailure; 1780 | } 1781 | return JSONSuccess; 1782 | } 1783 | 1784 | JSON_Status json_array_append_null(JSON_Array *array) { 1785 | JSON_Value *value = json_value_init_null(); 1786 | if (value == NULL) { 1787 | return JSONFailure; 1788 | } 1789 | if (json_array_append_value(array, value) == JSONFailure) { 1790 | json_value_free(value); 1791 | return JSONFailure; 1792 | } 1793 | return JSONSuccess; 1794 | } 1795 | 1796 | JSON_Status json_object_set_value(JSON_Object *object, const char *name, JSON_Value *value) { 1797 | size_t i = 0; 1798 | JSON_Value *old_value; 1799 | if (object == NULL || name == NULL || value == NULL || value->parent != NULL) { 1800 | return JSONFailure; 1801 | } 1802 | old_value = json_object_get_value(object, name); 1803 | if (old_value != NULL) { /* free and overwrite old value */ 1804 | json_value_free(old_value); 1805 | for (i = 0; i < json_object_get_count(object); i++) { 1806 | if (strcmp(object->names[i], name) == 0) { 1807 | value->parent = json_object_get_wrapping_value(object); 1808 | object->values[i] = value; 1809 | return JSONSuccess; 1810 | } 1811 | } 1812 | } 1813 | /* add new key value pair */ 1814 | return json_object_add(object, name, value); 1815 | } 1816 | 1817 | JSON_Status json_object_set_string(JSON_Object *object, const char *name, const char *string) { 1818 | return json_object_set_value(object, name, json_value_init_string(string)); 1819 | } 1820 | 1821 | JSON_Status json_object_set_number(JSON_Object *object, const char *name, double number) { 1822 | return json_object_set_value(object, name, json_value_init_number(number)); 1823 | } 1824 | 1825 | #ifdef JSON_FIXED_NUMBER 1826 | JSON_Status json_object_set_fixed(JSON_Object *object, const char *name, intmax_t fixed) { 1827 | return json_object_set_value(object, name, json_value_init_fixed(fixed)); 1828 | } 1829 | #endif 1830 | 1831 | JSON_Status json_object_set_boolean(JSON_Object *object, const char *name, int boolean) { 1832 | return json_object_set_value(object, name, json_value_init_boolean(boolean)); 1833 | } 1834 | 1835 | JSON_Status json_object_set_null(JSON_Object *object, const char *name) { 1836 | return json_object_set_value(object, name, json_value_init_null()); 1837 | } 1838 | 1839 | JSON_Status json_object_dotset_value(JSON_Object *object, const char *name, JSON_Value *value) { 1840 | const char *dot_pos = NULL; 1841 | char *current_name = NULL; 1842 | JSON_Object *temp_obj = NULL; 1843 | JSON_Value *new_value = NULL; 1844 | if (object == NULL || name == NULL || value == NULL) { 1845 | return JSONFailure; 1846 | } 1847 | dot_pos = strchr(name, '.'); 1848 | if (dot_pos == NULL) { 1849 | return json_object_set_value(object, name, value); 1850 | } else { 1851 | current_name = parson_strndup(name, dot_pos - name); 1852 | temp_obj = json_object_get_object(object, current_name); 1853 | if (temp_obj == NULL) { 1854 | new_value = json_value_init_object(); 1855 | if (new_value == NULL) { 1856 | parson_free(current_name); 1857 | return JSONFailure; 1858 | } 1859 | if (json_object_add(object, current_name, new_value) == JSONFailure) { 1860 | json_value_free(new_value); 1861 | parson_free(current_name); 1862 | return JSONFailure; 1863 | } 1864 | temp_obj = json_object_get_object(object, current_name); 1865 | } 1866 | parson_free(current_name); 1867 | return json_object_dotset_value(temp_obj, dot_pos + 1, value); 1868 | } 1869 | } 1870 | 1871 | JSON_Status json_object_dotset_string(JSON_Object *object, const char *name, const char *string) { 1872 | JSON_Value *value = json_value_init_string(string); 1873 | if (value == NULL) { 1874 | return JSONFailure; 1875 | } 1876 | if (json_object_dotset_value(object, name, value) == JSONFailure) { 1877 | json_value_free(value); 1878 | return JSONFailure; 1879 | } 1880 | return JSONSuccess; 1881 | } 1882 | 1883 | JSON_Status json_object_dotset_number(JSON_Object *object, const char *name, double number) { 1884 | JSON_Value *value = json_value_init_number(number); 1885 | if (value == NULL) { 1886 | return JSONFailure; 1887 | } 1888 | if (json_object_dotset_value(object, name, value) == JSONFailure) { 1889 | json_value_free(value); 1890 | return JSONFailure; 1891 | } 1892 | return JSONSuccess; 1893 | } 1894 | 1895 | #ifdef JSON_FIXED_NUMBER 1896 | JSON_Status json_object_dotset_fixed(JSON_Object *object, const char *name, intmax_t fixed) { 1897 | JSON_Value *value = json_value_init_fixed(fixed); 1898 | if (value == NULL) { 1899 | return JSONFailure; 1900 | } 1901 | if (json_object_dotset_value(object, name, value) == JSONFailure) { 1902 | json_value_free(value); 1903 | return JSONFailure; 1904 | } 1905 | return JSONSuccess; 1906 | } 1907 | #endif 1908 | 1909 | JSON_Status json_object_dotset_boolean(JSON_Object *object, const char *name, int boolean) { 1910 | JSON_Value *value = json_value_init_boolean(boolean); 1911 | if (value == NULL) { 1912 | return JSONFailure; 1913 | } 1914 | if (json_object_dotset_value(object, name, value) == JSONFailure) { 1915 | json_value_free(value); 1916 | return JSONFailure; 1917 | } 1918 | return JSONSuccess; 1919 | } 1920 | 1921 | JSON_Status json_object_dotset_null(JSON_Object *object, const char *name) { 1922 | JSON_Value *value = json_value_init_null(); 1923 | if (value == NULL) { 1924 | return JSONFailure; 1925 | } 1926 | if (json_object_dotset_value(object, name, value) == JSONFailure) { 1927 | json_value_free(value); 1928 | return JSONFailure; 1929 | } 1930 | return JSONSuccess; 1931 | } 1932 | 1933 | JSON_Status json_object_remove(JSON_Object *object, const char *name) { 1934 | size_t i = 0, last_item_index = 0; 1935 | if (object == NULL || json_object_get_value(object, name) == NULL) { 1936 | return JSONFailure; 1937 | } 1938 | last_item_index = json_object_get_count(object) - 1; 1939 | for (i = 0; i < json_object_get_count(object); i++) { 1940 | if (strcmp(object->names[i], name) == 0) { 1941 | parson_free(object->names[i]); 1942 | json_value_free(object->values[i]); 1943 | if (i != last_item_index) { /* Replace key value pair with one from the end */ 1944 | object->names[i] = object->names[last_item_index]; 1945 | object->values[i] = object->values[last_item_index]; 1946 | } 1947 | object->count -= 1; 1948 | return JSONSuccess; 1949 | } 1950 | } 1951 | return JSONFailure; /* No execution path should end here */ 1952 | } 1953 | 1954 | JSON_Status json_object_dotremove(JSON_Object *object, const char *name) { 1955 | const char *dot_pos = strchr(name, '.'); 1956 | char *current_name = NULL; 1957 | JSON_Object *temp_obj = NULL; 1958 | if (dot_pos == NULL) { 1959 | return json_object_remove(object, name); 1960 | } else { 1961 | current_name = parson_strndup(name, dot_pos - name); 1962 | temp_obj = json_object_get_object(object, current_name); 1963 | parson_free(current_name); 1964 | if (temp_obj == NULL) { 1965 | return JSONFailure; 1966 | } 1967 | return json_object_dotremove(temp_obj, dot_pos + 1); 1968 | } 1969 | } 1970 | 1971 | JSON_Status json_object_clear(JSON_Object *object) { 1972 | size_t i = 0; 1973 | if (object == NULL) { 1974 | return JSONFailure; 1975 | } 1976 | for (i = 0; i < json_object_get_count(object); i++) { 1977 | parson_free(object->names[i]); 1978 | json_value_free(object->values[i]); 1979 | } 1980 | object->count = 0; 1981 | return JSONSuccess; 1982 | } 1983 | 1984 | JSON_Status json_validate(const JSON_Value *schema, const JSON_Value *value) { 1985 | JSON_Value *temp_schema_value = NULL, *temp_value = NULL; 1986 | JSON_Array *schema_array = NULL, *value_array = NULL; 1987 | JSON_Object *schema_object = NULL, *value_object = NULL; 1988 | JSON_Value_Type schema_type = JSONError, value_type = JSONError; 1989 | const char *key = NULL; 1990 | size_t i = 0, count = 0; 1991 | if (schema == NULL || value == NULL) { 1992 | return JSONFailure; 1993 | } 1994 | schema_type = json_value_get_type(schema); 1995 | value_type = json_value_get_type(value); 1996 | if (schema_type != value_type && schema_type != JSONNull) { /* null represents all values */ 1997 | return JSONFailure; 1998 | } 1999 | switch (schema_type) { 2000 | case JSONArray: 2001 | schema_array = json_value_get_array(schema); 2002 | value_array = json_value_get_array(value); 2003 | count = json_array_get_count(schema_array); 2004 | if (count == 0) { 2005 | return JSONSuccess; /* Empty array allows all types */ 2006 | } 2007 | /* Get first value from array, rest is ignored */ 2008 | temp_schema_value = json_array_get_value(schema_array, 0); 2009 | for (i = 0; i < json_array_get_count(value_array); i++) { 2010 | temp_value = json_array_get_value(value_array, i); 2011 | if (json_validate(temp_schema_value, temp_value) == JSONFailure) { 2012 | return JSONFailure; 2013 | } 2014 | } 2015 | return JSONSuccess; 2016 | case JSONObject: 2017 | schema_object = json_value_get_object(schema); 2018 | value_object = json_value_get_object(value); 2019 | count = json_object_get_count(schema_object); 2020 | if (count == 0) { 2021 | return JSONSuccess; /* Empty object allows all objects */ 2022 | } else if (json_object_get_count(value_object) < count) { 2023 | return JSONFailure; /* Tested object mustn't have less name-value pairs than schema */ 2024 | } 2025 | for (i = 0; i < count; i++) { 2026 | key = json_object_get_name(schema_object, i); 2027 | temp_schema_value = json_object_get_value(schema_object, key); 2028 | temp_value = json_object_get_value(value_object, key); 2029 | if (temp_value == NULL) { 2030 | return JSONFailure; 2031 | } 2032 | if (json_validate(temp_schema_value, temp_value) == JSONFailure) { 2033 | return JSONFailure; 2034 | } 2035 | } 2036 | return JSONSuccess; 2037 | case JSONString: case JSONNumber: case JSONBoolean: case JSONNull: 2038 | return JSONSuccess; /* equality already tested before switch */ 2039 | #ifdef JSON_FIXED_NUMBER 2040 | case JSONFixed: 2041 | return JSONSuccess; 2042 | #endif 2043 | case JSONError: default: 2044 | return JSONFailure; 2045 | } 2046 | } 2047 | 2048 | int json_value_equals(const JSON_Value *a, const JSON_Value *b) { 2049 | JSON_Object *a_object = NULL, *b_object = NULL; 2050 | JSON_Array *a_array = NULL, *b_array = NULL; 2051 | const char *a_string = NULL, *b_string = NULL; 2052 | const char *key = NULL; 2053 | size_t a_count = 0, b_count = 0, i = 0; 2054 | JSON_Value_Type a_type, b_type; 2055 | a_type = json_value_get_type(a); 2056 | b_type = json_value_get_type(b); 2057 | if (a_type != b_type) { 2058 | return 0; 2059 | } 2060 | switch (a_type) { 2061 | case JSONArray: 2062 | a_array = json_value_get_array(a); 2063 | b_array = json_value_get_array(b); 2064 | a_count = json_array_get_count(a_array); 2065 | b_count = json_array_get_count(b_array); 2066 | if (a_count != b_count) { 2067 | return 0; 2068 | } 2069 | for (i = 0; i < a_count; i++) { 2070 | if (!json_value_equals(json_array_get_value(a_array, i), 2071 | json_array_get_value(b_array, i))) { 2072 | return 0; 2073 | } 2074 | } 2075 | return 1; 2076 | case JSONObject: 2077 | a_object = json_value_get_object(a); 2078 | b_object = json_value_get_object(b); 2079 | a_count = json_object_get_count(a_object); 2080 | b_count = json_object_get_count(b_object); 2081 | if (a_count != b_count) { 2082 | return 0; 2083 | } 2084 | for (i = 0; i < a_count; i++) { 2085 | key = json_object_get_name(a_object, i); 2086 | if (!json_value_equals(json_object_get_value(a_object, key), 2087 | json_object_get_value(b_object, key))) { 2088 | return 0; 2089 | } 2090 | } 2091 | return 1; 2092 | case JSONString: 2093 | a_string = json_value_get_string(a); 2094 | b_string = json_value_get_string(b); 2095 | if (a_string == NULL || b_string == NULL) { 2096 | return 0; /* shouldn't happen */ 2097 | } 2098 | return strcmp(a_string, b_string) == 0; 2099 | case JSONBoolean: 2100 | return json_value_get_boolean(a) == json_value_get_boolean(b); 2101 | case JSONNumber: 2102 | return fabs(json_value_get_number(a) - json_value_get_number(b)) < 0.000001; /* EPSILON */ 2103 | #ifdef JSON_FIXED_NUMBER 2104 | case JSONFixed: 2105 | return json_value_get_fixed(a) == json_value_get_fixed(b); 2106 | #endif 2107 | case JSONError: 2108 | return 1; 2109 | case JSONNull: 2110 | return 1; 2111 | default: 2112 | return 1; 2113 | } 2114 | } 2115 | 2116 | JSON_Value_Type json_type(const JSON_Value *value) { 2117 | return json_value_get_type(value); 2118 | } 2119 | 2120 | JSON_Object * json_object (const JSON_Value *value) { 2121 | return json_value_get_object(value); 2122 | } 2123 | 2124 | JSON_Array * json_array (const JSON_Value *value) { 2125 | return json_value_get_array(value); 2126 | } 2127 | 2128 | const char * json_string (const JSON_Value *value) { 2129 | return json_value_get_string(value); 2130 | } 2131 | 2132 | double json_number (const JSON_Value *value) { 2133 | return json_value_get_number(value); 2134 | } 2135 | 2136 | #ifdef JSON_FIXED_NUMBER 2137 | intmax_t json_fixed (const JSON_Value *value) { 2138 | return json_value_get_fixed(value); 2139 | } 2140 | #endif 2141 | 2142 | int json_boolean(const JSON_Value *value) { 2143 | return json_value_get_boolean(value); 2144 | } 2145 | 2146 | void json_set_allocation_functions(JSON_Malloc_Function malloc_fun, JSON_Free_Function free_fun) { 2147 | parson_malloc = malloc_fun; 2148 | parson_free = free_fun; 2149 | } 2150 | -------------------------------------------------------------------------------- /src/parson.h: -------------------------------------------------------------------------------- 1 | /* 2 | Parson ( http://kgabis.github.com/parson/ ) 3 | Copyright (c) 2012 - 2017 Krzysztof Gabis 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. 22 | */ 23 | 24 | #ifndef parson_parson_h 25 | #define parson_parson_h 26 | 27 | #ifdef __cplusplus 28 | extern "C" 29 | { 30 | #endif 31 | 32 | #include /* size_t */ 33 | #ifdef JSON_FIXED_NUMBER 34 | #include 35 | #endif 36 | 37 | /* Types and enums */ 38 | typedef struct json_object_t JSON_Object; 39 | typedef struct json_array_t JSON_Array; 40 | typedef struct json_value_t JSON_Value; 41 | 42 | enum json_value_type { 43 | JSONError = -1, 44 | JSONNull = 1, 45 | JSONString = 2, 46 | JSONNumber = 3, 47 | #ifdef JSON_FIXED_NUMBER 48 | JSONFixed = 7, 49 | #endif 50 | JSONObject = 4, 51 | JSONArray = 5, 52 | JSONBoolean = 6 53 | }; 54 | typedef int JSON_Value_Type; 55 | 56 | enum json_result_t { 57 | JSONSuccess = 0, 58 | JSONFailure = -1 59 | }; 60 | typedef int JSON_Status; 61 | 62 | typedef void * (*JSON_Malloc_Function)(size_t); 63 | typedef void (*JSON_Free_Function)(void *); 64 | 65 | /* Call only once, before calling any other function from parson API. If not called, malloc and free 66 | from stdlib will be used for all allocations */ 67 | void json_set_allocation_functions(JSON_Malloc_Function malloc_fun, JSON_Free_Function free_fun); 68 | 69 | #ifndef MRB_DISABLE_STDIO 70 | /* Parses first JSON value in a file, returns NULL in case of error */ 71 | JSON_Value * json_parse_file(const char *filename); 72 | 73 | /* Parses first JSON value in a file and ignores comments (/ * * / and //), 74 | returns NULL in case of error */ 75 | JSON_Value * json_parse_file_with_comments(const char *filename); 76 | #endif 77 | 78 | /* Parses first JSON value in a string, returns NULL in case of error */ 79 | JSON_Value * json_parse_string(const char *string); 80 | 81 | /* Parses first JSON value in a string and ignores comments (/ * * / and //), 82 | returns NULL in case of error */ 83 | JSON_Value * json_parse_string_with_comments(const char *string); 84 | 85 | /* Serialization */ 86 | size_t json_serialization_size(const JSON_Value *value); /* returns 0 on fail */ 87 | JSON_Status json_serialize_to_buffer(const JSON_Value *value, char *buf, size_t buf_size_in_bytes); 88 | #ifndef MRB_DISABLE_STDIO 89 | JSON_Status json_serialize_to_file(const JSON_Value *value, const char *filename); 90 | #endif 91 | char * json_serialize_to_string(const JSON_Value *value); 92 | 93 | /* Pretty serialization */ 94 | size_t json_serialization_size_pretty(const JSON_Value *value); /* returns 0 on fail */ 95 | JSON_Status json_serialize_to_buffer_pretty(const JSON_Value *value, char *buf, size_t buf_size_in_bytes); 96 | #ifndef MRB_DISABLE_STDIO 97 | JSON_Status json_serialize_to_file_pretty(const JSON_Value *value, const char *filename); 98 | #endif 99 | char * json_serialize_to_string_pretty(const JSON_Value *value); 100 | 101 | void json_free_serialized_string(char *string); /* frees string from json_serialize_to_string and json_serialize_to_string_pretty */ 102 | 103 | /* Comparing */ 104 | int json_value_equals(const JSON_Value *a, const JSON_Value *b); 105 | 106 | /* Validation 107 | This is *NOT* JSON Schema. It validates json by checking if object have identically 108 | named fields with matching types. 109 | For example schema {"name":"", "age":0} will validate 110 | {"name":"Joe", "age":25} and {"name":"Joe", "age":25, "gender":"m"}, 111 | but not {"name":"Joe"} or {"name":"Joe", "age":"Cucumber"}. 112 | In case of arrays, only first value in schema is checked against all values in tested array. 113 | Empty objects ({}) validate all objects, empty arrays ([]) validate all arrays, 114 | null validates values of every type. 115 | */ 116 | JSON_Status json_validate(const JSON_Value *schema, const JSON_Value *value); 117 | 118 | /* 119 | * JSON Object 120 | */ 121 | JSON_Value * json_object_get_value (const JSON_Object *object, const char *name); 122 | const char * json_object_get_string (const JSON_Object *object, const char *name); 123 | JSON_Object * json_object_get_object (const JSON_Object *object, const char *name); 124 | JSON_Array * json_object_get_array (const JSON_Object *object, const char *name); 125 | double json_object_get_number (const JSON_Object *object, const char *name); /* returns 0 on fail */ 126 | #ifdef JSON_FIXED_NUMBER 127 | intmax_t json_object_get_fixed (const JSON_Object *object, const char *name); /* returns 0 on fail */ 128 | #endif 129 | int json_object_get_boolean(const JSON_Object *object, const char *name); /* returns -1 on fail */ 130 | 131 | /* dotget functions enable addressing values with dot notation in nested objects, 132 | just like in structs or c++/java/c# objects (e.g. objectA.objectB.value). 133 | Because valid names in JSON can contain dots, some values may be inaccessible 134 | this way. */ 135 | JSON_Value * json_object_dotget_value (const JSON_Object *object, const char *name); 136 | const char * json_object_dotget_string (const JSON_Object *object, const char *name); 137 | JSON_Object * json_object_dotget_object (const JSON_Object *object, const char *name); 138 | JSON_Array * json_object_dotget_array (const JSON_Object *object, const char *name); 139 | double json_object_dotget_number (const JSON_Object *object, const char *name); /* returns 0 on fail */ 140 | #ifdef JSON_FIXED_NUMBER 141 | intmax_t json_object_dotget_fixed (const JSON_Object *object, const char *name); 142 | #endif 143 | int json_object_dotget_boolean(const JSON_Object *object, const char *name); /* returns -1 on fail */ 144 | 145 | /* Functions to get available names */ 146 | size_t json_object_get_count (const JSON_Object *object); 147 | const char * json_object_get_name (const JSON_Object *object, size_t index); 148 | JSON_Value * json_object_get_value_at(const JSON_Object *object, size_t index); 149 | JSON_Value * json_object_get_wrapping_value(const JSON_Object *object); 150 | 151 | /* Functions to check if object has a value with a specific name. Returned value is 1 if object has 152 | * a value and 0 if it doesn't. dothas functions behave exactly like dotget functions. */ 153 | int json_object_has_value (const JSON_Object *object, const char *name); 154 | int json_object_has_value_of_type(const JSON_Object *object, const char *name, JSON_Value_Type type); 155 | 156 | int json_object_dothas_value (const JSON_Object *object, const char *name); 157 | int json_object_dothas_value_of_type(const JSON_Object *object, const char *name, JSON_Value_Type type); 158 | 159 | /* Creates new name-value pair or frees and replaces old value with a new one. 160 | * json_object_set_value does not copy passed value so it shouldn't be freed afterwards. */ 161 | JSON_Status json_object_set_value(JSON_Object *object, const char *name, JSON_Value *value); 162 | JSON_Status json_object_set_string(JSON_Object *object, const char *name, const char *string); 163 | JSON_Status json_object_set_number(JSON_Object *object, const char *name, double number); 164 | #ifdef JSON_FIXED_NUMBER 165 | JSON_Status json_object_set_fixed(JSON_Object *object, const char *name, intmax_t fixed); 166 | #endif 167 | JSON_Status json_object_set_boolean(JSON_Object *object, const char *name, int boolean); 168 | JSON_Status json_object_set_null(JSON_Object *object, const char *name); 169 | 170 | /* Works like dotget functions, but creates whole hierarchy if necessary. 171 | * json_object_dotset_value does not copy passed value so it shouldn't be freed afterwards. */ 172 | JSON_Status json_object_dotset_value(JSON_Object *object, const char *name, JSON_Value *value); 173 | JSON_Status json_object_dotset_string(JSON_Object *object, const char *name, const char *string); 174 | JSON_Status json_object_dotset_number(JSON_Object *object, const char *name, double number); 175 | #ifdef JSON_FIXED_NUMBER 176 | JSON_Status json_object_dotset_fixed(JSON_Object *object, const char *name, intmax_t fixed); 177 | #endif 178 | JSON_Status json_object_dotset_boolean(JSON_Object *object, const char *name, int boolean); 179 | JSON_Status json_object_dotset_null(JSON_Object *object, const char *name); 180 | 181 | /* Frees and removes name-value pair */ 182 | JSON_Status json_object_remove(JSON_Object *object, const char *name); 183 | 184 | /* Works like dotget function, but removes name-value pair only on exact match. */ 185 | JSON_Status json_object_dotremove(JSON_Object *object, const char *key); 186 | 187 | /* Removes all name-value pairs in object */ 188 | JSON_Status json_object_clear(JSON_Object *object); 189 | 190 | /* 191 | *JSON Array 192 | */ 193 | JSON_Value * json_array_get_value (const JSON_Array *array, size_t index); 194 | const char * json_array_get_string (const JSON_Array *array, size_t index); 195 | JSON_Object * json_array_get_object (const JSON_Array *array, size_t index); 196 | JSON_Array * json_array_get_array (const JSON_Array *array, size_t index); 197 | double json_array_get_number (const JSON_Array *array, size_t index); /* returns 0 on fail */ 198 | #ifdef JSON_FIXED_NUMBER 199 | intmax_t json_array_get_fixed (const JSON_Array *array, size_t index); 200 | #endif 201 | int json_array_get_boolean(const JSON_Array *array, size_t index); /* returns -1 on fail */ 202 | size_t json_array_get_count (const JSON_Array *array); 203 | JSON_Value * json_array_get_wrapping_value(const JSON_Array *array); 204 | 205 | /* Frees and removes value at given index, does nothing and returns JSONFailure if index doesn't exist. 206 | * Order of values in array may change during execution. */ 207 | JSON_Status json_array_remove(JSON_Array *array, size_t i); 208 | 209 | /* Frees and removes from array value at given index and replaces it with given one. 210 | * Does nothing and returns JSONFailure if index doesn't exist. 211 | * json_array_replace_value does not copy passed value so it shouldn't be freed afterwards. */ 212 | JSON_Status json_array_replace_value(JSON_Array *array, size_t i, JSON_Value *value); 213 | JSON_Status json_array_replace_string(JSON_Array *array, size_t i, const char* string); 214 | JSON_Status json_array_replace_number(JSON_Array *array, size_t i, double number); 215 | #ifdef JSON_FIXED_NUMBER 216 | JSON_Status json_array_replace_fixed(JSON_Array *array, size_t i, intmax_t fixed); 217 | #endif 218 | JSON_Status json_array_replace_boolean(JSON_Array *array, size_t i, int boolean); 219 | JSON_Status json_array_replace_null(JSON_Array *array, size_t i); 220 | 221 | /* Frees and removes all values from array */ 222 | JSON_Status json_array_clear(JSON_Array *array); 223 | 224 | /* Appends new value at the end of array. 225 | * json_array_append_value does not copy passed value so it shouldn't be freed afterwards. */ 226 | JSON_Status json_array_append_value(JSON_Array *array, JSON_Value *value); 227 | JSON_Status json_array_append_string(JSON_Array *array, const char *string); 228 | JSON_Status json_array_append_number(JSON_Array *array, double number); 229 | #ifdef JSON_FIXED_NUMBER 230 | JSON_Status json_array_append_fixed(JSON_Array *array, intmax_t fixed); 231 | #endif 232 | JSON_Status json_array_append_boolean(JSON_Array *array, int boolean); 233 | JSON_Status json_array_append_null(JSON_Array *array); 234 | 235 | /* 236 | *JSON Value 237 | */ 238 | JSON_Value * json_value_init_object (void); 239 | JSON_Value * json_value_init_array (void); 240 | JSON_Value * json_value_init_string (const char *string); /* copies passed string */ 241 | JSON_Value * json_value_init_number (double number); 242 | #ifdef JSON_FIXED_NUMBER 243 | JSON_Value * json_value_init_fixed (intmax_t fixed); 244 | #endif 245 | JSON_Value * json_value_init_boolean(int boolean); 246 | JSON_Value * json_value_init_null (void); 247 | JSON_Value * json_value_deep_copy (const JSON_Value *value); 248 | void json_value_free (JSON_Value *value); 249 | 250 | JSON_Value_Type json_value_get_type (const JSON_Value *value); 251 | JSON_Object * json_value_get_object (const JSON_Value *value); 252 | JSON_Array * json_value_get_array (const JSON_Value *value); 253 | const char * json_value_get_string (const JSON_Value *value); 254 | double json_value_get_number (const JSON_Value *value); 255 | #ifdef JSON_FIXED_NUMBER 256 | intmax_t json_value_get_fixed (const JSON_Value *value); 257 | #endif 258 | int json_value_get_boolean(const JSON_Value *value); 259 | JSON_Value * json_value_get_parent (const JSON_Value *value); 260 | 261 | /* Same as above, but shorter */ 262 | JSON_Value_Type json_type (const JSON_Value *value); 263 | JSON_Object * json_object (const JSON_Value *value); 264 | JSON_Array * json_array (const JSON_Value *value); 265 | const char * json_string (const JSON_Value *value); 266 | double json_number (const JSON_Value *value); 267 | #ifdef JSON_FIXED_NUMBER 268 | intmax_t json_fixed (const JSON_Value *value); 269 | #endif 270 | int json_boolean(const JSON_Value *value); 271 | 272 | #ifdef __cplusplus 273 | } 274 | #endif 275 | 276 | #endif 277 | -------------------------------------------------------------------------------- /test/json.rb: -------------------------------------------------------------------------------- 1 | assert('parse object') do 2 | assert_equal({"foo"=>"bar"}, JSON.parse('{"foo": "bar"}')) 3 | end 4 | assert('parse null') do 5 | assert_equal({"foo"=>nil}, JSON.parse('{"foo": null}')) 6 | end 7 | assert('parse array') do 8 | assert_equal "foo", JSON.parse('[true, "foo"]')[1] 9 | end 10 | assert('parse multi-byte') do 11 | assert_equal({"あいうえお"=>"かきくけこ"}, JSON.parse('{"あいうえお": "かきくけこ"}')) 12 | end 13 | assert('parse object with numbers') do 14 | assert_equal({"latitude"=>33.6526,"longitude"=>177.96063}, JSON.parse('{"latitude": 33.6526,"longitude": 177.96063}')) 15 | end 16 | assert('stringify boolean') do 17 | assert_equal "true", JSON.stringify(true) 18 | end 19 | assert('stringify symbol') do 20 | assert_equal "\"symbol\"", JSON.stringify(:symbol) 21 | end 22 | assert('strnigify object with numeric value') do 23 | assert_equal '{"foo":"bar"}', JSON.stringify({"foo"=>"bar"}) 24 | end 25 | assert('strnigify object with string value') do 26 | assert_equal '{"foo":1}', JSON.stringify({"foo"=> 1}) 27 | end 28 | assert('stringify object with float value') do 29 | assert_equal '{"foo":2.5}', JSON.stringify({"foo"=> 2.5}) 30 | end 31 | assert('stringify object with nil value') do 32 | assert_equal '{"foo":null}', JSON.stringify({"foo"=> nil}) 33 | end 34 | assert('stringify object with object key and float value') do 35 | assert_equal '{"{\"foo\"=>\"bar\"}":1.5}', JSON.stringify({{"foo"=> "bar"}=> 1.5}) 36 | end 37 | assert('stringify empty array') do 38 | assert_equal "[]", JSON.stringify([]) 39 | end 40 | assert('strnigify array with few elements') do 41 | assert_equal "[1,true,\"foo\"]", JSON.stringify([1,true,"foo"]) 42 | end 43 | assert('stringify object with several keys') do 44 | assert_equal '{"bar":2,"foo":1}', JSON.stringify({"bar"=> 2, "foo"=>1}) 45 | end 46 | assert('stringify multi-byte') do 47 | assert_equal '{"foo":"ふー","bar":"ばー"}', JSON.stringify({"foo"=>"ふー", "bar"=> "ばー"}) 48 | end 49 | assert('stringify escaped') do 50 | assert_equal '["\\\\"]', JSON.stringify(['\\']) 51 | end 52 | assert('stringify escaped quote') do 53 | assert_equal '["\\\\\\\\\""]', JSON.stringify(['\\\"']) 54 | s = JSON.stringify(['\\\"']) 55 | assert_equal '[', s[0] 56 | assert_equal '"', s[1] 57 | assert_equal '\\', s[2]; assert_equal '\\', s[3] 58 | assert_equal '\\', s[4]; assert_equal '\\', s[5] 59 | assert_equal '\\', s[6]; assert_equal '"', s[7] 60 | assert_equal '"', s[8] 61 | assert_equal ']', s[9] 62 | end 63 | assert('stringify object with to_json') do 64 | class Foo 65 | def to_json 66 | '{"foo":"foo"}' 67 | end 68 | end 69 | assert_equal '{"foo":"foo"}', JSON.stringify(Foo.new) 70 | end 71 | assert('stringify object with to_s') do 72 | class Bar 73 | def to_s 74 | "bar" 75 | end 76 | end 77 | assert_equal '"bar"', JSON.stringify(Bar.new) 78 | end 79 | assert('stringify object without to_s') do 80 | class Baz 81 | end 82 | s = JSON.stringify(Baz.new) 83 | assert_equal "\"# "bar"}.to_json 87 | end 88 | assert('String#to_json') do 89 | assert_equal '"foo"', "foo".to_json 90 | end 91 | assert('Fixnum#to_json') do 92 | assert_equal '1', 1.to_json 93 | end 94 | assert('TrueClass#to_json') do 95 | assert_equal 'true', true.to_json 96 | end 97 | assert('FalseClass#to_json') do 98 | assert_equal 'false', false.to_json 99 | end 100 | assert('Array#to_json') do 101 | assert_equal '[1,3,true,["foo"]]', [1 ,3, true,["foo"]].to_json 102 | end 103 | assert('Array#to_json') do 104 | assert_equal '[1,3,true,["foo"]]', [1 ,3, true,["foo"]].to_json 105 | end 106 | assert('pretty cat 🐱') do 107 | assert_equal "true", JSON.pretty_generate(true) 108 | assert_equal "1.2", JSON.pretty_generate(1.2) 109 | assert_equal "[\n]", JSON.pretty_generate([]) 110 | assert_equal "{\n}", JSON.pretty_generate({}) 111 | want =< [1,2,[{"baz" => true}, 3]]}) 126 | end 127 | assert('dump') do 128 | class DummyWriter 129 | def initialize();@s = '';end 130 | def write(s);@s += s;end 131 | def to_s();@s;end 132 | end 133 | w = DummyWriter.new 134 | JSON.dump(123, w) 135 | assert_equal "123", w.to_s 136 | end 137 | assert('ParserError') do 138 | assert_raise(JSON::ParserError) { JSON.parse('{') } 139 | end 140 | assert('load') do 141 | assert_equal({"foo"=>"bar"}, JSON.load('{"foo": "bar"}')) 142 | 143 | o = nil 144 | JSON.load '{"foo": "bar"}' do |x| o = x; end 145 | assert_equal({"foo"=>"bar"}, o) 146 | 147 | o = nil 148 | assert_raise(JSON::ParserError) { JSON.load '{' {|x| o = x} } 149 | assert_equal(nil, o) 150 | end 151 | --------------------------------------------------------------------------------