Viewing file: json_parser.h (2.68 KB) -rw-r--r-- Select action/file-type: (+) | (+) | (+) | Code (+) | Session (+) | (+) | SDB (+) | (+) | (+) | (+) | (+) | (+) |
/* * File: json_parser.h * * The data routines and data structures here constitue a very * simple parser that will parse a JSON string into a tree of * of structs, each struct representing an fundamental JSON type. * */ #ifndef __JSON_PARSER_H__ #define __JSON_PARSER_H__
#include <stddef.h> #include <stdlib.h> #include <stdio.h> #include <unistd.h>
/* * JSON Types. * * This enum is used to identify the primative JSON * type represented by a JSON_Value_t */ typedef enum { JSON_TYPE_NULL, JSON_TYPE_STRING, JSON_TYPE_NUMBER, JSON_TYPE_BOOL, JSON_TYPE_OBJECT, JSON_TYPE_ARRAY, JSON_TYPE_ERROR }JSON_Type;
struct JSON_Object; struct JSON_Array;
/* * A JSON Value. * * A JSON value can be an object, an array, a string, a number, a boolean * or a NULL. This structure wraps all of those into a union. */ typedef struct { JSON_Type type; /* Indicates the type of value */ union { char *string; /* The value of a string */ float number; /* The value of a number */ struct JSON_Object *obj; /* The value of a object */ struct JSON_Array *array; /* The value of a array */ } u; }JSON_Value_t;
/* * A JSON Pair. * * A Pair is a name-value structure. It is not actally a JSON type * but rather is a constituent of an JSON_Object_t */ typedef struct { char *name; /* The name field. */ JSON_Value_t *value; /* The value field. */ }JSON_Pair_t;
/* * A JSON Object. * * An object is implelemented as a array of N-V Pairs */ typedef struct JSON_Object { JSON_Pair_t **pairs; /* The Array of Pairs */ size_t num_pairs; /* The size of the Array */ }JSON_Object_t;
/* * A JSON Array. * * An Array is implemented as a list of values. */ typedef struct JSON_Array { JSON_Value_t **values; /* The array of values */ size_t num_values; /* The size of the Array */ }JSON_Array_t;
/* * Parse a string into a JSON Object. */ JSON_Object_t *ParseJSONObject(char **strptr);
/* * Free a JSON Object. */ void FreeJSONObject(JSON_Object_t *obj);
/* * Format and output a JSON object. */ void DisplayJSONObject(FILE *f, JSON_Object_t *obj);
/* * Format and output a JSON value. */ void DisplayJSONValue(FILE* f, JSON_Value_t *v);
/* * Find a specific value in an object. */ JSON_Value_t *Find(JSON_Object_t *obj, char *name);
/* * Return a specified JSON pair from an object. */ JSON_Pair_t *GetPairFromObject(JSON_Object_t *obj, unsigned idx);
/* * Return a specified JSON value from an array. */ JSON_Value_t *GetValueFromArray(JSON_Array_t *arr, unsigned idx);
#endif // __JSON_PARSER_H__
|