Blame src/json.hpp

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