Skip to content

Instantly share code, notes, and snippets.

@jabb
Created November 19, 2012 05:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jabb/4109119 to your computer and use it in GitHub Desktop.
Save jabb/4109119 to your computer and use it in GitHub Desktop.
JSON in C header file.
/* Copyright (c) 2012, Michael Patraw
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * The name of Michael Patraw may not be used to endorse or promote
* products derived from this software without specific prior written
* permission.
*
* THIS SOFTWARE IS PROVIDED BY Michael Patraw ''AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL Michael Patraw BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef JC_H
#define JC_H
#ifdef __cplusplus
extern "C" {
#endif
struct jc_atom
{
int type;
union
{
int boolean;
double number;
char *string;
void *object;
void *array;
} guts;
int ref;
void *iter;
};
enum
{
JC_NULL,
JC_BOOLEAN,
JC_NUMBER,
JC_STRING,
JC_OBJECT,
JC_ARRAY
};
/* All of these functions return NULL on an allocation failure.
*/
struct jc_atom *jc_null(void);
struct jc_atom *jc_boolean(int tf);
struct jc_atom *jc_number(double num);
struct jc_atom *jc_string(const char *str);
struct jc_atom *jc_object(void);
struct jc_atom *jc_array(void);
/* Never fails. Returns "atom" unless it was free'd, then it returns NULL. */
struct jc_atom *jc_unref(struct jc_atom *atom);
/* Never fails. Returns the same atom that was passed. */
struct jc_atom *jc_ref(struct jc_atom *atom);
/* Returns the type of the atom. */
int jc_type(struct jc_atom *atom);
/* Returns the value of the atom.
* JC_NULL: NULL
* JC_BOOLEAN: int *
* JC_INTEGER: int *
* JC_FLOATING: double *
* JC_STRING: char *
* JC_OBJECT: NULL
* JC_ARRAY: NULL
*/
void *jc_value(struct jc_atom *atom);
/* Returns the length of the object or array, or -1 if it's not a container. */
int jc_length(struct jc_atom *atom);
/* For objects:
* jc_set(struct atom *obj, const char *key, struct atom *newatom)
*
* For arrays:
* jc_set(struct atom *obj, int index, struct atom *newatom)
*
* Setting a container element to NULL unreferences it and removes the key. In
* the case of arrays, this moves the array down to fill in the space. If you
* want to reserve the spot, use an atom with the JC_NULL type.
*
* In order to set just passed the end of the array, use jc_length() as the
* index.
*
* If the index is pass the length of the array, the extra indices are filled
* with JC_NULL atoms.
*
* Returns the newly added object, or NULL on failure (unless you set to
* NULL, which always succeeds).
*/
struct jc_atom *jc_set(struct jc_atom *obj, struct jc_atom *newatom, ...);
/* For objects:
* jc_get(struct atom *obj, const char *key)
*
* For arrays:
* jc_get(struct atom *obj, int index)
*
* Returns the atom, or NULL if it doesn't exist. */
struct jc_atom *jc_get(struct jc_atom *obj, ...);
/* Same as the above functions except you can use
* periods.to.access.deeply.nested.objects.and.arrays.0.1.2.3
*/
struct jc_atom *jc_vset(struct jc_atom *obj, struct jc_atom *newatom, const char *key);
struct jc_atom *jc_vget(struct jc_atom *obj, const char *key);
/* Returns 1 when key and val are loaded with a value. */
int jc_iter(struct jc_atom *obj, void *key, struct jc_atom **val);
struct jc_atom *jc_from_string(const char *str);
char *jc_to_string(struct jc_atom *atom);
void jc_print(void *fd, struct jc_atom *atom);
#ifdef __cplusplus
}
#endif
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment