Blame src/json.hpp

Packit Service 7770af
/*
Packit Service 7770af
  Copyright (C) 2011 Joseph A. Adams (joeyadams3.14159@gmail.com)
Packit Service 7770af
  All rights reserved.
Packit Service 7770af
Packit Service 7770af
  Permission is hereby granted, free of charge, to any person obtaining a copy
Packit Service 7770af
  of this software and associated documentation files (the "Software"), to deal
Packit Service 7770af
  in the Software without restriction, including without limitation the rights
Packit Service 7770af
  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
Packit Service 7770af
  copies of the Software, and to permit persons to whom the Software is
Packit Service 7770af
  furnished to do so, subject to the following conditions:
Packit Service 7770af
Packit Service 7770af
  The above copyright notice and this permission notice shall be included in
Packit Service 7770af
  all copies or substantial portions of the Software.
Packit Service 7770af
Packit Service 7770af
  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
Packit Service 7770af
  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
Packit Service 7770af
  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
Packit Service 7770af
  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
Packit Service 7770af
  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
Packit Service 7770af
  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
Packit Service 7770af
  THE SOFTWARE.
Packit Service 7770af
*/
Packit Service 7770af
Packit Service 7770af
#ifndef CCAN_JSON_H
Packit Service 7770af
#define CCAN_JSON_H
Packit Service 7770af
Packit Service 7770af
#include <stdbool.h>
Packit Service 7770af
#include <stddef.h>
Packit Service 7770af
Packit Service 7770af
typedef enum {
Packit Service 7770af
  JSON_NULL,
Packit Service 7770af
  JSON_BOOL,
Packit Service 7770af
  JSON_STRING,
Packit Service 7770af
  JSON_NUMBER,
Packit Service 7770af
  JSON_ARRAY,
Packit Service 7770af
  JSON_OBJECT,
Packit Service 7770af
} JsonTag;
Packit Service 7770af
Packit Service 7770af
typedef struct JsonNode JsonNode;
Packit Service 7770af
Packit Service 7770af
struct JsonNode
Packit Service 7770af
{
Packit Service 7770af
  /* only if parent is an object or array (NULL otherwise) */
Packit Service 7770af
  JsonNode *parent;
Packit Service 7770af
  JsonNode *prev, *next;
Packit Service 7770af
Packit Service 7770af
  /* only if parent is an object (NULL otherwise) */
Packit Service 7770af
  char *key; /* Must be valid UTF-8. */
Packit Service 7770af
Packit Service 7770af
  JsonTag tag;
Packit Service 7770af
  union {
Packit Service 7770af
    /* JSON_BOOL */
Packit Service 7770af
    bool bool_;
Packit Service 7770af
Packit Service 7770af
    /* JSON_STRING */
Packit Service 7770af
    char *string_; /* Must be valid UTF-8. */
Packit Service 7770af
Packit Service 7770af
    /* JSON_NUMBER */
Packit Service 7770af
    double number_;
Packit Service 7770af
Packit Service 7770af
    /* JSON_ARRAY */
Packit Service 7770af
    /* JSON_OBJECT */
Packit Service 7770af
    struct {
Packit Service 7770af
      JsonNode *head, *tail;
Packit Service 7770af
    } children;
Packit Service 7770af
  };
Packit Service 7770af
};
Packit Service 7770af
Packit Service 7770af
/*** Encoding, decoding, and validation ***/
Packit Service 7770af
Packit Service 7770af
JsonNode   *json_decode         (const char *json);
Packit Service 7770af
char       *json_encode         (const JsonNode *node);
Packit Service 7770af
char       *json_encode_string  (const char *str);
Packit Service 7770af
char       *json_stringify      (const JsonNode *node, const char *space);
Packit Service 7770af
void        json_delete         (JsonNode *node);
Packit Service 7770af
Packit Service 7770af
bool        json_validate       (const char *json);
Packit Service 7770af
Packit Service 7770af
/*** Lookup and traversal ***/
Packit Service 7770af
Packit Service 7770af
JsonNode   *json_find_element   (JsonNode *array, int index);
Packit Service 7770af
JsonNode   *json_find_member    (JsonNode *object, const char *key);
Packit Service 7770af
Packit Service 7770af
JsonNode   *json_first_child    (const JsonNode *node);
Packit Service 7770af
Packit Service 7770af
#define json_foreach(i, object_or_array)            \
Packit Service 7770af
  for ((i) = json_first_child(object_or_array);   \
Packit Service 7770af
     (i) != NULL;                               \
Packit Service 7770af
     (i) = (i)->next)
Packit Service 7770af
Packit Service 7770af
/*** Construction and manipulation ***/
Packit Service 7770af
Packit Service 7770af
JsonNode *json_mknull(void);
Packit Service 7770af
JsonNode *json_mkbool(bool b);
Packit Service 7770af
JsonNode *json_mkstring(const char *s);
Packit Service 7770af
JsonNode *json_mknumber(double n);
Packit Service 7770af
JsonNode *json_mkarray(void);
Packit Service 7770af
JsonNode *json_mkobject(void);
Packit Service 7770af
Packit Service 7770af
void json_append_element(JsonNode *array, JsonNode *element);
Packit Service 7770af
void json_prepend_element(JsonNode *array, JsonNode *element);
Packit Service 7770af
void json_append_member(JsonNode *object, const char *key, JsonNode *value);
Packit Service 7770af
void json_prepend_member(JsonNode *object, const char *key, JsonNode *value);
Packit Service 7770af
Packit Service 7770af
void json_remove_from_parent(JsonNode *node);
Packit Service 7770af
Packit Service 7770af
/*** Debugging ***/
Packit Service 7770af
Packit Service 7770af
/*
Packit Service 7770af
 * Look for structure and encoding problems in a JsonNode or its descendents.
Packit Service 7770af
 *
Packit Service 7770af
 * If a problem is detected, return false, writing a description of the problem
Packit Service 7770af
 * to errmsg (unless errmsg is NULL).
Packit Service 7770af
 */
Packit Service 7770af
bool json_check(const JsonNode *node, char errmsg[256]);
Packit Service 7770af
Packit Service 7770af
#endif