joeylib2/tools/common/toolJson.h

51 lines
1.6 KiB
C

// Part of JoeyLib, Copyright (c) 2026 Scott Duensing, under the MIT License.
// See LICENSE.md for license information.
// SPDX-License-Identifier: MIT
//
// toolJson.h - a small JSON reader for the JoeyLib host tools (Tiled's TMJ
// and TSJ): a document as a tree of values. Numbers are doubles, with a
// note of whether they were written as integers; strings are UTF-8 with
// their escapes decoded; an object keeps its members in order, and a key
// given twice reads as its last value (as Python's json does).
#ifndef TOOL_JSON_H
#define TOOL_JSON_H
#include <stddef.h>
#include <stdbool.h>
#define TOOL_JSON_ERROR_BYTES 160u
typedef enum ToolJsonTypeE {
TOOL_JSON_NULL = 0,
TOOL_JSON_BOOL,
TOOL_JSON_NUMBER,
TOOL_JSON_STRING,
TOOL_JSON_ARRAY,
TOOL_JSON_OBJECT
} ToolJsonTypeE;
// A value. An array's items, or an object's values with their keys, are
// items[0..count).
typedef struct ToolJsonT {
ToolJsonTypeE type;
bool boolean;
double number;
bool integer;
char *string;
struct ToolJsonT **items;
char **keys;
size_t count;
} ToolJsonT;
// The document's value, or NULL with the reason in err.
ToolJsonT *toolJsonParse(const char *text, size_t len, char err[TOOL_JSON_ERROR_BYTES]);
// Free a value and everything in it.
void toolJsonFree(ToolJsonT *value);
// An object's member, the last of that key; NULL when value is no object
// or has no such member.
const ToolJsonT *toolJsonGet(const ToolJsonT *value, const char *key);
#endif