From 5cec2e115fe76e57e1acef6215b413c62b676505 Mon Sep 17 00:00:00 2001 From: James Barford-Evans Date: Tue, 8 Aug 2023 23:25:56 +0100 Subject: [PATCH] Better documentation (#7) --- COPYING | 24 ++++++++++++++++++++++++ README.md | 30 ++++++++++++++++++++++++++++-- json.h | 10 +++++----- 3 files changed, 57 insertions(+), 7 deletions(-) create mode 100644 COPYING diff --git a/COPYING b/COPYING new file mode 100644 index 0000000..6148840 --- /dev/null +++ b/COPYING @@ -0,0 +1,24 @@ +Copyright (c) 2023, James W M Barford-Evans + +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. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "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 THE COPYRIGHT OWNER OR CONTRIBUTORS 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. diff --git a/README.md b/README.md index 0eb71d9..ba75cc8 100644 --- a/README.md +++ b/README.md @@ -10,8 +10,8 @@ JSON parsing simplified. Easy JSON is a lightweight and fast JSON parser with an emphasis on ease of use for parsing and accessing properties in the parsed JSON. I was wanting something -that had a small surface area, solid error handling with clear errors and a `jq` -like syntax for simplifying accessing properties off JSON. +that had a small surface area, solid error handling and a `jq` like syntax for +simplifying accessing properties off JSON. The is error handling and reporting suitable for use with multiple threads as The error state is unique per parsed JSON. Thus if you are parsing mulitple @@ -53,6 +53,32 @@ json *jsonParseWithLenAndFlags(char *raw_json, size_t buflen, int flags); - `JSON_STRNUM_FLAG` is avalible for parsing both floats and integers as strings. +## Getters & Typechecking +The following will return `1` if the `json *` is not `NULL` and there is a match +on the type: + +```c +int jsonIsObject(json *j); +int jsonIsArray(json *j); +int jsonIsNull(json *j); +int jsonIsBool(json *j); +int jsonIsString(json *j); +int jsonIsInt(json *j); +int jsonIsFloat(json *j); +``` + +And the following for getting a value from a `json *`: + +```c +json *jsonGetObject(json *J); +json *jsonGetArray(json *J); +void *jsonGetNull(json *J); +int jsonGetBool(json *J); +char *jsonGetString(json *J); +ssize_t jsonGetInt(json *J); +double jsonGetFloat(json *J); +``` + ### NULL A legitimate JSON NULL is parsed to `JSON_SENTINAL` not c's `NULL`. You can use the helper `jsonIsNull()` to determine this. diff --git a/json.h b/json.h index a848336..c66eda9 100644 --- a/json.h +++ b/json.h @@ -75,13 +75,13 @@ typedef enum JSON_ERRNO { JSON_EOF, } JSON_ERRNO; -char *jsonGetString(json *J); -double jsonGetFloat(json *J); -ssize_t jsonGetInt(json *J); -json *jsonGetArray(json *J); json *jsonGetObject(json *J); -int jsonGetBool(json *J); +json *jsonGetArray(json *J); void *jsonGetNull(json *J); +int jsonGetBool(json *J); +char *jsonGetString(json *J); +ssize_t jsonGetInt(json *J); +double jsonGetFloat(json *J); int jsonIsObject(json *j); int jsonIsArray(json *j);