27 Mar

Allocators in json.h!

While at GDC I got a feature request from the awesome folks over at Blastbay Studios requesting that they could feed an allocator to json.h, my JSON parsing library. This has long been on my TODO list, so I took the opportunity of a plane ride back from San Francisco to the Isle of Skye to remedy the situation!

The latest master version of json.h changes the signature of json_parse_ex to include two new fields:

struct json_value_s *json_parse_ex(
  const void *src,
  size_t src_size,
  size_t flags_bitset,
  void*(*alloc_func_ptr)(void *, size_t), // new field!
  void *user_data,                        // new field!
  struct json_parse_result_s *result);

alloc_func_ptr and user_data.

For recap, json.h uses 1 allocation to store the entire structure and contents of the JSON file (see my introductory post for json.h for details).

Instead of calling malloc, if alloc_func_ptr is not NULL, it will instead call the allocator and provide the user_data to the allocator as the first parameter, and the size of allocation requested as the second parameter.

I hope this added functionality proves useful to the many people who are already using json.h in their applications!