Version 7.0
Copyright © 2021 Lowell D. Thomas
APG
… an ABNF Parser Generator
json.h
Go to the documentation of this file.
1 /* *************************************************************************************
2  Copyright (c) 2021, Lowell D. Thomas
3  All rights reserved.
4 
5  This file is part of APG Version 7.0.
6  APG Version 7.0 may be used under the terms of the BSD 2-Clause License.
7 
8  Redistribution and use in source and binary forms, with or without
9  modification, are permitted provided that the following conditions are met:
10 
11  1. Redistributions of source code must retain the above copyright notice, this
12  list of conditions and the following disclaimer.
13 
14  2. Redistributions in binary form must reproduce the above copyright notice,
15  this list of conditions and the following disclaimer in the documentation
16  and/or other materials provided with the distribution.
17 
18  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21  DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
22  FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24  SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
25  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
26  OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 
29 * *************************************************************************************/
30 #ifndef _JSON_H_
31 #define _JSON_H_
32 
42 #include "../utilities/utilities.h"
43 
47 #define JSON_ID_OBJECT 11
49 #define JSON_ID_ARRAY 13
50 #define JSON_ID_STRING 14
51 #define JSON_ID_NUMBER 15
52 #define JSON_ID_TRUE 16
53 #define JSON_ID_FALSE 17
54 #define JSON_ID_NULL 18
55 
57 
61 #define JSON_ID_FLOAT 19
63 #define JSON_ID_SIGNED 20
64 #define JSON_ID_UNSIGNED 21
65 
67 
73 typedef struct {
79  union {
80  double dFloat;
81  uint64_t uiUnsigned;
82  int64_t iSigned;
83  };
88 } json_number;
89 
99 typedef struct json_value_tag {
109  union{
112  struct{
116  };
117  };
118 } json_value;
119 
120 // constructor/destructor
122 void* vpJsonCtor(exception* spEx);
124 void vJsonDtor(void *vpCtx);
125 abool bJsonValidate(void* vpCtx);
127 
133 void* vpJsonReadFile(void *vpCtx, const char *cpFileName);
135 void* vpJsonReadArray(void *vpCtx, uint8_t *ucpData, aint uiDataLen);
136 uint8_t* ucpJsonWrite(void* vpCtx, json_value* spValue, aint* uipCount);
138 
142 void* vpJsonFindKeyA(void *vpCtx, const char *cpKey, json_value* spValue);
144 void* vpJsonFindKeyU(void *vpCtx, const uint32_t *uipKey, aint uiLength, json_value* spValue);
145 void* vpJsonTree(void* vpCtx, json_value* spValue);
146 void* vpJsonChildren(void* vpCtx, json_value* spValue);
148 
151 void vJsonIteratorDtor(void* vpIteratorCtx);
153 json_value* spJsonIteratorFirst(void* vpIteratorCtx);
154 json_value* spJsonIteratorLast(void* vpIteratorCtx);
155 json_value* spJsonIteratorNext(void* vpIteratorCtx);
156 json_value* spJsonIteratorPrev(void* vpIteratorCtx);
157 aint uiJsonIteratorCount(void* vpIteratorCtx);
159 
167 void* vpJsonBuildCtor(void* vpJsonCtx);
169 void vJsonBuildDtor(void* vpBuildCtx);
170 void vJsonBuildClear(void* vpBuildCtx);
171 aint uiJsonBuildMakeStringA(void* vpBuildCtx, const char* cpString);
172 aint uiJsonBuildMakeStringU(void* vpBuildCtx, const uint32_t* uipData, aint uiLength);
173 aint uiJsonBuildMakeNumberF(void* vpBuildCtx, double dNumber);
174 aint uiJsonBuildMakeNumberS(void* vpBuildCtx, int64_t iNumber);
175 aint uiJsonBuildMakeNumberU(void* vpBuildCtx, uint64_t uiNumber);
176 aint uiJsonBuildMakeTrue(void* vpBuildCtx);
177 aint uiJsonBuildMakeFalse(void* vpBuildCtx);
178 aint uiJsonBuildMakeNull(void* vpBuildCtx);
179 aint uiJsonBuildMakeObject(void* vpBuildCtx);
180 aint uiJsonBuildMakeArray(void* vpBuildCtx);
181 aint uiJsonBuildAddToObject(void* vpBuildCtx, aint uiObject, aint uiKey, aint uiAdd);
182 aint uiJsonBuildAddToArray(void* vpBuildCtx, aint uiArray, aint uiAdd);
183 void* vpJsonBuild(void* vpBuildCtx, aint uiRoot);
185 
187 void vJsonDisplayValue(void *vpCtx, json_value *spValue, aint uiDepth);
189 void vJsonDisplayInput(void *vpCtx, abool bShowLines);
190 //void vJsonSetTraceFile(void *vpCtx, const char *cpTraceOutput);
192 
302 #endif /* _JSON_H_ */
json_number::iSigned
int64_t iSigned
If uiType = JSON_ID_SIGNED, the signed int value.
Definition: json.h:82
json_number::dFloat
double dFloat
If uiType = JSON_ID_FLOAT, the floating point value.
Definition: json.h:80
json_value_tag::spString
u32_phrase * spString
Pointer to the string value if uiId = JSON_ID_STRING.
Definition: json.h:110
vJsonDisplayInput
void vJsonDisplayInput(void *vpCtx, abool bShowLines)
Display the input JSON byte stream.
Definition: json.c:365
vpJsonReadFile
void * vpJsonReadFile(void *vpCtx, const char *cpFileName)
The JSON file reader.
Definition: json.c:207
uiJsonBuildMakeTrue
aint uiJsonBuildMakeTrue(void *vpBuildCtx)
Make a JSON true value.
Definition: builder.c:403
bJsonValidate
abool bJsonValidate(void *vpCtx)
Validate a JSON context pointer.
Definition: json.c:188
uiJsonBuildMakeObject
aint uiJsonBuildMakeObject(void *vpBuildCtx)
Make a JSON object value.
Definition: builder.c:445
json_number
The structure of a JSON number value.
Definition: json.h:73
uiJsonBuildMakeStringA
aint uiJsonBuildMakeStringA(void *vpBuildCtx, const char *cpString)
Make a string value from a null-terminated ASCII string.
Definition: builder.c:283
uiJsonBuildMakeStringU
aint uiJsonBuildMakeStringU(void *vpBuildCtx, const uint32_t *uipData, aint uiLength)
Make a string value from UTF-32 code points.
Definition: builder.c:223
vpJsonBuild
void * vpJsonBuild(void *vpBuildCtx, aint uiRoot)
Build the JSON object.
Definition: builder.c:603
vJsonBuildDtor
void vJsonBuildDtor(void *vpBuildCtx)
The builder object destructor.
Definition: builder.c:154
vpJsonCtor
void * vpJsonCtor(exception *spEx)
The JSON constructor.
Definition: json.c:115
uiJsonBuildMakeFalse
aint uiJsonBuildMakeFalse(void *vpBuildCtx)
Make a JSON false value.
Definition: builder.c:417
vpJsonTree
void * vpJsonTree(void *vpCtx, json_value *spValue)
Initialize the iterator to walk a value tree.
Definition: json.c:501
uiJsonBuildMakeArray
aint uiJsonBuildMakeArray(void *vpBuildCtx)
Makea JSON array value.
Definition: builder.c:459
vpJsonFindKeyA
void * vpJsonFindKeyA(void *vpCtx, const char *cpKey, json_value *spValue)
Find JSON values with a specified ASCII key.
Definition: json.c:434
aint
uint_fast32_t aint
The APG parser's unsigned integer type.
Definition: apg.h:79
json_value_tag::uiId
aint uiId
The type of value. One of.
Definition: json.h:100
spJsonIteratorFirst
json_value * spJsonIteratorFirst(void *vpIteratorCtx)
Find the first value in the list represented by this iterator.
Definition: json.c:613
spJsonIteratorPrev
json_value * spJsonIteratorPrev(void *vpIteratorCtx)
Find the prev value in the list represented by this iterator.
Definition: json.c:671
uiJsonBuildMakeNumberU
aint uiJsonBuildMakeNumberU(void *vpBuildCtx, uint64_t uiNumber)
Make a JSON unsigned integer number value.
Definition: builder.c:383
uiJsonBuildAddToObject
aint uiJsonBuildAddToObject(void *vpBuildCtx, aint uiObject, aint uiKey, aint uiAdd)
Add a child value to a parent object value.
Definition: builder.c:479
exception
A structure to describe the type and location of a caught exception.
Definition: exception.h:47
vpJsonFindKeyU
void * vpJsonFindKeyU(void *vpCtx, const uint32_t *uipKey, aint uiLength, json_value *spValue)
Find JSON values with the specified 32-bit Unicode key.
Definition: json.c:474
vpJsonBuildCtor
void * vpJsonBuildCtor(void *vpJsonCtx)
The builder object constructor.
Definition: builder.c:124
json_value_tag::uiChildCount
aint uiChildCount
The number of child values if uiId is JSON_ID_OBJECT or JSON_ID_ARRAY.
Definition: json.h:115
vJsonDisplayValue
void vJsonDisplayValue(void *vpCtx, json_value *spValue, aint uiDepth)
Display a value and optionally the values in the branch below, if any.
Definition: json.c:412
json_value_tag::spNumber
json_number * spNumber
Pointer to the number value if uiId = JSON_ID_NUMBER.
Definition: json.h:111
uiJsonIteratorCount
aint uiJsonIteratorCount(void *vpIteratorCtx)
Find the number of values in the list represented by this iterator.
Definition: json.c:691
vpJsonReadArray
void * vpJsonReadArray(void *vpCtx, uint8_t *ucpData, aint uiDataLen)
The JSON array reader.
Definition: json.c:289
vJsonDtor
void vJsonDtor(void *vpCtx)
The JSON Parser component destructor.
Definition: json.c:165
spJsonIteratorNext
json_value * spJsonIteratorNext(void *vpIteratorCtx)
Find the next value in the list represented by this iterator.
Definition: json.c:647
json_value_tag::spKey
u32_phrase * spKey
Points to the associated key string if this is a member of a JSON object. Otherwise,...
Definition: json.h:108
ucpJsonWrite
uint8_t * ucpJsonWrite(void *vpCtx, json_value *spValue, aint *uipCount)
Converts a sub-tree of values into UTF-8 byte stream of JSON text.
Definition: json.c:329
json_number::uiUnsigned
uint64_t uiUnsigned
If uiType = JSON_ID_UNSIGNED, the unsigned int value.
Definition: json.h:81
abool
uint8_t abool
abool is the APG bool type.
Definition: apg.h:140
vJsonBuildClear
void vJsonBuildClear(void *vpBuildCtx)
Clears all memory associated with this builder object.
Definition: builder.c:184
vJsonIteratorDtor
void vJsonIteratorDtor(void *vpIteratorCtx)
The JSON iterator destructor.
Definition: json.c:591
vpJsonChildren
void * vpJsonChildren(void *vpCtx, json_value *spValue)
Initialize the iterator over the children of the given value as the parent node.
Definition: json.c:530
json_value_tag::sppChildren
struct json_value_tag ** sppChildren
Points to a list of child value pointers if uiId is JSON_ID_OBJECT or JSON_ID_ARRAY.
Definition: json.h:113
json_number::uiType
aint uiType
Identifies the number type. One of.
Definition: json.h:74
u32_phrase
Defines a pointer to an array of 32-bit unsigned integers plus its length. Typically needed by Unicod...
Definition: lib.h:73
json_value_tag
The structure of a JSON value.
Definition: json.h:99
uiJsonBuildMakeNumberF
aint uiJsonBuildMakeNumberF(void *vpBuildCtx, double dNumber)
Make a JSON floating point number value.
Definition: builder.c:336
uiJsonBuildMakeNumberS
aint uiJsonBuildMakeNumberS(void *vpBuildCtx, int64_t iNumber)
Make a JSON signed integer number value.
Definition: builder.c:358
uiJsonBuildMakeNull
aint uiJsonBuildMakeNull(void *vpBuildCtx)
Make a JSON null value.
Definition: builder.c:431
spJsonIteratorLast
json_value * spJsonIteratorLast(void *vpIteratorCtx)
Find the last value in the list represented by this iterator.
Definition: json.c:627
uiJsonBuildAddToArray
aint uiJsonBuildAddToArray(void *vpBuildCtx, aint uiArray, aint uiAdd)
Add a child value to a parent array value.
Definition: builder.c:550
APG Version 7.0 is licensed under the 2-Clause BSD License,
an Open Source Initiative Approved License.