/* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */1/* include/k5-json.h - JSON declarations */2/*3* Copyright (c) 2010 Kungliga Tekniska Högskolan4* (Royal Institute of Technology, Stockholm, Sweden).5* All rights reserved.6*7* Portions Copyright (c) 2010 Apple Inc. All rights reserved.8*9* Redistribution and use in source and binary forms, with or without10* modification, are permitted provided that the following conditions11* are met:12*13* 1. Redistributions of source code must retain the above copyright14* notice, this list of conditions and the following disclaimer.15*16* 2. Redistributions in binary form must reproduce the above copyright17* notice, this list of conditions and the following disclaimer in the18* documentation and/or other materials provided with the distribution.19*20* 3. Neither the name of the Institute nor the names of its contributors21* may be used to endorse or promote products derived from this software22* without specific prior written permission.23*24* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND25* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE26* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE27* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE28* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL29* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS30* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)31* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT32* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY33* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF34* SUCH DAMAGE.35*/36/*37* Copyright (C) 2012 by the Massachusetts Institute of Technology.38* All rights reserved.39*40* Redistribution and use in source and binary forms, with or without41* modification, are permitted provided that the following conditions42* are met:43*44* * Redistributions of source code must retain the above copyright45* notice, this list of conditions and the following disclaimer.46*47* * Redistributions in binary form must reproduce the above copyright48* notice, this list of conditions and the following disclaimer in49* the documentation and/or other materials provided with the50* distribution.51*52* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS53* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT54* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS55* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE56* COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,57* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES58* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR59* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)60* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,61* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)62* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED63* OF THE POSSIBILITY OF SUCH DAMAGE.64*/6566#ifndef K5_JSON_H67#define K5_JSON_H6869#include <stddef.h>7071#define K5_JSON_TID_NUMBER 072#define K5_JSON_TID_NULL 173#define K5_JSON_TID_BOOL 274#define K5_JSON_TID_MEMORY 12875#define K5_JSON_TID_ARRAY 12976#define K5_JSON_TID_OBJECT 13077#define K5_JSON_TID_STRING 1317879/*80* The k5_json_value C type can represent any kind of JSON value. It has no81* static type safety since it is represented using a void pointer, so be82* careful with it. Its type can be checked dynamically with k5_json_get_tid()83* and the above constants.84*/85typedef void *k5_json_value;86typedef unsigned int k5_json_tid;8788k5_json_tid k5_json_get_tid(k5_json_value val);8990/*91* k5_json_value objects are reference-counted. These functions increment and92* decrement the refcount, possibly freeing the value. k5_json_retain returns93* its argument and always succeeds. Both functions gracefully accept NULL.94*/95k5_json_value k5_json_retain(k5_json_value val);96void k5_json_release(k5_json_value val);9798/*99* If a k5_json_* function can fail, it returns 0 on success and an errno value100* on failure.101*/102103/*104* Null105*/106107typedef struct k5_json_null_st *k5_json_null;108109int k5_json_null_create(k5_json_null *null_out);110111/* Create a null value as a k5_json_value, for polymorphic convenience. */112int k5_json_null_create_val(k5_json_value *val_out);113114/*115* Boolean116*/117118typedef struct k5_json_bool_st *k5_json_bool;119120int k5_json_bool_create(int truth, k5_json_bool *val_out);121int k5_json_bool_value(k5_json_bool bval);122123/*124* Array125*/126127typedef struct k5_json_array_st *k5_json_array;128129int k5_json_array_create(k5_json_array *val_out);130size_t k5_json_array_length(k5_json_array array);131132/* Both of these functions increment the reference count on val. */133int k5_json_array_add(k5_json_array array, k5_json_value val);134void k5_json_array_set(k5_json_array array, size_t idx, k5_json_value val);135136/* Get an alias to the idx-th element of array, without incrementing the137* reference count. The caller must check idx against the array length. */138k5_json_value k5_json_array_get(k5_json_array array, size_t idx);139140/*141* Create an array from a template and a variable argument list. template142* characters are:143* v: a k5_json_value argument is read, retained, and stored144* n: no argument is read; a null value is stored145* b: an int argument is read and stored as a boolean value146* i: an int argument is read and stored as a number value147* L: a long long argument is read and stored as a number value148* s: a const char * argument is read and stored as a null or string value149* B: const void * and size_t arguments are read and stored as a base64150* string value151*/152int153k5_json_array_fmt(k5_json_array *array_out, const char *template, ...);154155/*156* Object157*/158159typedef struct k5_json_object_st *k5_json_object;160typedef void (*k5_json_object_iterator_fn)(void *arg, const char *key,161k5_json_value val);162163int k5_json_object_create(k5_json_object *val_out);164void k5_json_object_iterate(k5_json_object obj,165k5_json_object_iterator_fn func, void *arg);166167/* Return the number of mappings in an object. */168size_t k5_json_object_count(k5_json_object obj);169170/*171* Store val into object at key, incrementing val's reference count and172* releasing any previous value at key. If val is NULL, key is removed from173* obj if it exists, and obj remains unchanged if it does not.174*/175int k5_json_object_set(k5_json_object obj, const char *key, k5_json_value val);176177/* Get an alias to the object's value for key, without incrementing the178* reference count. Returns NULL if there is no value for key. */179k5_json_value k5_json_object_get(k5_json_object obj, const char *key);180181/*182* String183*/184185typedef struct k5_json_string_st *k5_json_string;186187int k5_json_string_create(const char *cstring, k5_json_string *val_out);188int k5_json_string_create_len(const void *data, size_t len,189k5_json_string *val_out);190const char *k5_json_string_utf8(k5_json_string string);191192193/* Create a base64 string value from binary data. */194int k5_json_string_create_base64(const void *data, size_t len,195k5_json_string *val_out);196197/* Decode the base64 contents of string. */198int k5_json_string_unbase64(k5_json_string string, unsigned char **data_out,199size_t *len_out);200201/*202* Number203*/204205typedef struct k5_json_number_st *k5_json_number;206207int k5_json_number_create(long long number, k5_json_number *val_out);208long long k5_json_number_value(k5_json_number number);209210/*211* JSON encoding and decoding212*/213214int k5_json_encode(k5_json_value val, char **json_out);215int k5_json_decode(const char *str, k5_json_value *val_out);216217#endif /* K5_JSON_H */218219220