2022-07-28 15:15:04 +00:00
|
|
|
/*
|
2022-12-26 15:52:52 +00:00
|
|
|
* Copyright (C) 2022-2023 Jordan Bancino <@jordan:bancino.net>
|
2022-07-28 15:15:04 +00:00
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person
|
|
|
|
* obtaining a copy of this software and associated documentation files
|
|
|
|
* (the "Software"), to deal in the Software without restriction,
|
|
|
|
* including without limitation the rights to use, copy, modify, merge,
|
|
|
|
* publish, distribute, sublicense, and/or sell copies of the Software,
|
|
|
|
* and to permit persons to whom the Software is furnished to do so,
|
|
|
|
* subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be
|
2022-07-28 16:00:52 +00:00
|
|
|
* included in all copies or portions of the Software.
|
2022-07-28 15:15:04 +00:00
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
|
|
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
|
|
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
|
|
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
|
|
|
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
|
|
|
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
|
|
|
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
|
|
* SOFTWARE.
|
|
|
|
*/
|
2022-07-26 02:56:14 +00:00
|
|
|
#include <Json.h>
|
|
|
|
|
2022-10-13 16:18:42 +00:00
|
|
|
#include <Memory.h>
|
2023-01-08 04:38:06 +00:00
|
|
|
#include <Str.h>
|
2022-07-27 17:47:12 +00:00
|
|
|
|
2022-07-26 02:56:14 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stddef.h>
|
|
|
|
#include <stdlib.h>
|
2022-07-27 17:47:12 +00:00
|
|
|
#include <string.h>
|
2022-08-02 20:58:15 +00:00
|
|
|
#include <ctype.h>
|
2023-03-10 18:46:03 +00:00
|
|
|
#include <errno.h>
|
2022-07-26 02:56:14 +00:00
|
|
|
|
|
|
|
struct JsonValue
|
|
|
|
{
|
|
|
|
JsonType type;
|
|
|
|
union
|
|
|
|
{
|
|
|
|
HashMap *object;
|
|
|
|
Array *array;
|
|
|
|
char *string;
|
2022-07-27 13:47:22 +00:00
|
|
|
long integer;
|
2022-07-26 02:56:14 +00:00
|
|
|
double floating;
|
|
|
|
int boolean:1;
|
|
|
|
} as;
|
|
|
|
};
|
|
|
|
|
2022-08-02 20:58:15 +00:00
|
|
|
typedef enum JsonToken
|
|
|
|
{
|
|
|
|
TOKEN_UNKNOWN,
|
|
|
|
TOKEN_COLON,
|
|
|
|
TOKEN_COMMA,
|
|
|
|
TOKEN_OBJECT_OPEN,
|
|
|
|
TOKEN_OBJECT_CLOSE,
|
|
|
|
TOKEN_ARRAY_OPEN,
|
|
|
|
TOKEN_ARRAY_CLOSE,
|
|
|
|
TOKEN_STRING,
|
|
|
|
TOKEN_INTEGER,
|
|
|
|
TOKEN_FLOAT,
|
|
|
|
TOKEN_BOOLEAN,
|
|
|
|
TOKEN_NULL,
|
|
|
|
TOKEN_EOF
|
|
|
|
} JsonToken;
|
|
|
|
|
|
|
|
typedef struct JsonParserState
|
|
|
|
{
|
|
|
|
FILE *stream;
|
|
|
|
|
|
|
|
JsonToken tokenType;
|
|
|
|
char *token;
|
2022-08-09 17:19:14 +00:00
|
|
|
size_t tokenLen;
|
2022-08-02 20:58:15 +00:00
|
|
|
} JsonParserState;
|
|
|
|
|
2022-07-26 02:56:14 +00:00
|
|
|
JsonType
|
|
|
|
JsonValueType(JsonValue * value)
|
|
|
|
{
|
|
|
|
if (!value)
|
|
|
|
{
|
|
|
|
return JSON_NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return value->type;
|
|
|
|
}
|
|
|
|
|
|
|
|
static JsonValue *
|
|
|
|
JsonValueAllocate(void)
|
|
|
|
{
|
2022-10-13 16:18:42 +00:00
|
|
|
return Malloc(sizeof(JsonValue));
|
2022-07-26 02:56:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
JsonValue *
|
|
|
|
JsonValueObject(HashMap * object)
|
|
|
|
{
|
|
|
|
JsonValue *value;
|
|
|
|
|
|
|
|
if (!object)
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
value = JsonValueAllocate();
|
|
|
|
if (!value)
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
value->type = JSON_OBJECT;
|
|
|
|
value->as.object = object;
|
|
|
|
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
|
|
|
HashMap *
|
|
|
|
JsonValueAsObject(JsonValue * value)
|
|
|
|
{
|
|
|
|
if (!value || value->type != JSON_OBJECT)
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return value->as.object;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
JsonValue *
|
|
|
|
JsonValueArray(Array * array)
|
|
|
|
{
|
|
|
|
JsonValue *value;
|
|
|
|
|
|
|
|
if (!array)
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
value = JsonValueAllocate();
|
|
|
|
if (!value)
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
value->type = JSON_ARRAY;
|
|
|
|
value->as.array = array;
|
|
|
|
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
|
|
|
Array *
|
|
|
|
JsonValueAsArray(JsonValue * value)
|
|
|
|
{
|
|
|
|
if (!value || value->type != JSON_ARRAY)
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return value->as.array;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
JsonValue *
|
|
|
|
JsonValueString(char *string)
|
|
|
|
{
|
|
|
|
JsonValue *value;
|
|
|
|
|
|
|
|
if (!string)
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
value = JsonValueAllocate();
|
|
|
|
if (!value)
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
value->type = JSON_STRING;
|
2023-02-24 00:17:56 +00:00
|
|
|
|
|
|
|
value->as.string = StrDuplicate(string);
|
|
|
|
if (!value->as.string)
|
|
|
|
{
|
|
|
|
Free(value);
|
|
|
|
return NULL;
|
|
|
|
}
|
2022-07-26 02:56:14 +00:00
|
|
|
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
|
|
|
char *
|
|
|
|
JsonValueAsString(JsonValue * value)
|
|
|
|
{
|
|
|
|
if (!value || value->type != JSON_STRING)
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return value->as.string;
|
|
|
|
}
|
|
|
|
|
|
|
|
JsonValue *
|
2022-07-27 13:47:22 +00:00
|
|
|
JsonValueInteger(long integer)
|
2022-07-26 02:56:14 +00:00
|
|
|
{
|
|
|
|
JsonValue *value;
|
|
|
|
|
|
|
|
value = JsonValueAllocate();
|
|
|
|
if (!value)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
value->type = JSON_INTEGER;
|
|
|
|
value->as.integer = integer;
|
|
|
|
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
2022-07-27 15:30:46 +00:00
|
|
|
long
|
2022-07-26 02:56:14 +00:00
|
|
|
JsonValueAsInteger(JsonValue * value)
|
|
|
|
{
|
|
|
|
if (!value || value->type != JSON_INTEGER)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return value->as.integer;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
JsonValue *
|
|
|
|
JsonValueFloat(double floating)
|
|
|
|
{
|
|
|
|
JsonValue *value;
|
|
|
|
|
|
|
|
value = JsonValueAllocate();
|
|
|
|
if (!value)
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
value->type = JSON_FLOAT;
|
|
|
|
value->as.floating = floating;
|
|
|
|
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
|
|
|
double
|
|
|
|
JsonValueAsFloat(JsonValue * value)
|
|
|
|
{
|
|
|
|
if (!value || value->type != JSON_FLOAT)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return value->as.floating;
|
|
|
|
}
|
|
|
|
|
|
|
|
JsonValue *
|
|
|
|
JsonValueBoolean(int boolean)
|
|
|
|
{
|
|
|
|
JsonValue *value;
|
|
|
|
|
|
|
|
value = JsonValueAllocate();
|
|
|
|
if (!value)
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
value->type = JSON_BOOLEAN;
|
|
|
|
value->as.boolean = boolean;
|
|
|
|
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
JsonValueAsBoolean(JsonValue * value)
|
|
|
|
{
|
|
|
|
if (!value || value->type != JSON_BOOLEAN)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return value->as.boolean;
|
|
|
|
}
|
|
|
|
|
|
|
|
JsonValue *
|
|
|
|
JsonValueNull(void)
|
|
|
|
{
|
|
|
|
JsonValue *value;
|
|
|
|
|
|
|
|
value = JsonValueAllocate();
|
|
|
|
if (!value)
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
value->type = JSON_NULL;
|
|
|
|
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
JsonValueFree(JsonValue * value)
|
|
|
|
{
|
2022-07-27 21:27:34 +00:00
|
|
|
size_t i;
|
|
|
|
Array *arr;
|
|
|
|
|
2022-07-26 02:56:14 +00:00
|
|
|
if (!value)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-07-27 21:27:34 +00:00
|
|
|
switch (value->type)
|
|
|
|
{
|
|
|
|
case JSON_OBJECT:
|
|
|
|
JsonFree(value->as.object);
|
|
|
|
break;
|
|
|
|
case JSON_ARRAY:
|
|
|
|
arr = value->as.array;
|
|
|
|
for (i = 0; i < ArraySize(arr); i++)
|
|
|
|
{
|
|
|
|
JsonValueFree((JsonValue *) ArrayGet(arr, i));
|
|
|
|
}
|
|
|
|
ArrayFree(arr);
|
|
|
|
break;
|
2022-07-29 16:32:46 +00:00
|
|
|
case JSON_STRING:
|
2022-10-13 16:18:42 +00:00
|
|
|
Free(value->as.string);
|
2022-07-29 16:32:46 +00:00
|
|
|
break;
|
2022-07-27 21:27:34 +00:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2022-10-13 16:18:42 +00:00
|
|
|
Free(value);
|
2022-07-26 02:56:14 +00:00
|
|
|
}
|
|
|
|
|
2022-07-27 21:27:34 +00:00
|
|
|
void
|
2022-07-26 17:08:46 +00:00
|
|
|
JsonEncodeString(const char *str, FILE * out)
|
2022-07-26 13:10:17 +00:00
|
|
|
{
|
2022-07-26 17:08:46 +00:00
|
|
|
size_t i;
|
|
|
|
char c;
|
|
|
|
|
|
|
|
fputc('"', out);
|
|
|
|
|
|
|
|
i = 0;
|
|
|
|
while ((c = str[i]) != '\0')
|
|
|
|
{
|
|
|
|
switch (c)
|
|
|
|
{
|
|
|
|
case '\\':
|
|
|
|
case '"':
|
|
|
|
case '/':
|
|
|
|
fputc('\\', out);
|
|
|
|
fputc(c, out);
|
|
|
|
break;
|
|
|
|
case '\b':
|
|
|
|
fputs("\\b", out);
|
|
|
|
break;
|
|
|
|
case '\t':
|
|
|
|
fputs("\\t", out);
|
|
|
|
break;
|
|
|
|
case '\n':
|
|
|
|
fputs("\\n", out);
|
|
|
|
break;
|
|
|
|
case '\f':
|
|
|
|
fputs("\\f", out);
|
|
|
|
break;
|
|
|
|
case '\r':
|
|
|
|
fputs("\\r", out);
|
|
|
|
break;
|
2022-07-27 17:47:12 +00:00
|
|
|
default: /* Assume UTF-8 input */
|
2022-07-27 21:27:34 +00:00
|
|
|
/*
|
|
|
|
* RFC 4627: "All Unicode characters may be placed
|
|
|
|
* within the quotation marks except for the characters
|
|
|
|
* that must be escaped: quotation mark, reverse solidus,
|
|
|
|
* and the control characters (U+0000 through U+001F)."
|
|
|
|
*
|
|
|
|
* This technically covers the above cases for backspace,
|
|
|
|
* tab, newline, feed, and carriage return characters,
|
|
|
|
* but we can save bytes if we encode those as their
|
|
|
|
* more human-readable representation.
|
|
|
|
*/
|
|
|
|
if (c <= 0x001F)
|
|
|
|
{
|
2022-09-07 00:53:30 +00:00
|
|
|
fprintf(out, "\\u%04x", c);
|
2022-07-27 21:27:34 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
fputc(c, out);
|
|
|
|
}
|
2022-07-26 17:08:46 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
|
|
|
|
fputc('"', out);
|
|
|
|
}
|
|
|
|
|
|
|
|
static char *
|
|
|
|
JsonDecodeString(FILE * in)
|
|
|
|
{
|
|
|
|
const size_t strBlockSize = 16;
|
|
|
|
|
2022-07-27 17:47:12 +00:00
|
|
|
size_t i;
|
2022-07-26 17:08:46 +00:00
|
|
|
size_t len;
|
|
|
|
size_t allocated;
|
|
|
|
char *str;
|
2023-01-02 18:22:04 +00:00
|
|
|
int c;
|
2022-07-27 17:47:12 +00:00
|
|
|
char a[5];
|
|
|
|
|
|
|
|
unsigned long utf8;
|
|
|
|
char *utf8Ptr;
|
2022-07-26 17:08:46 +00:00
|
|
|
|
|
|
|
len = 0;
|
|
|
|
allocated = strBlockSize;
|
|
|
|
|
2022-10-13 16:18:42 +00:00
|
|
|
str = Malloc(allocated * sizeof(char));
|
2022-07-26 17:08:46 +00:00
|
|
|
if (!str)
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
while ((c = fgetc(in)) != EOF)
|
|
|
|
{
|
2022-08-02 20:58:15 +00:00
|
|
|
if (c <= 0x001F)
|
|
|
|
{
|
|
|
|
/* Bad byte; these must be escaped */
|
2022-10-13 16:18:42 +00:00
|
|
|
Free(str);
|
2022-08-02 20:58:15 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2022-07-26 17:08:46 +00:00
|
|
|
switch (c)
|
|
|
|
{
|
|
|
|
case '"':
|
2023-02-23 03:46:05 +00:00
|
|
|
if (len >= allocated)
|
|
|
|
{
|
|
|
|
char *tmp;
|
|
|
|
|
|
|
|
allocated += 1;
|
|
|
|
tmp = Realloc(str, allocated * sizeof(char));
|
|
|
|
if (!tmp)
|
|
|
|
{
|
|
|
|
Free(str);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
str = tmp;
|
|
|
|
}
|
2022-07-26 17:08:46 +00:00
|
|
|
str[len] = '\0';
|
|
|
|
return str;
|
|
|
|
break;
|
|
|
|
case '\\':
|
|
|
|
c = fgetc(in);
|
|
|
|
switch (c)
|
|
|
|
{
|
|
|
|
case '\\':
|
|
|
|
case '"':
|
|
|
|
case '/':
|
2022-07-27 17:47:12 +00:00
|
|
|
a[0] = c;
|
|
|
|
a[1] = '\0';
|
2022-07-26 17:08:46 +00:00
|
|
|
break;
|
|
|
|
case 'b':
|
2022-07-27 17:47:12 +00:00
|
|
|
a[0] = '\b';
|
|
|
|
a[1] = '\0';
|
2022-07-26 17:08:46 +00:00
|
|
|
break;
|
|
|
|
case 't':
|
2022-07-27 17:47:12 +00:00
|
|
|
a[0] = '\t';
|
|
|
|
a[1] = '\0';
|
2022-07-26 17:08:46 +00:00
|
|
|
break;
|
|
|
|
case 'n':
|
2022-07-27 17:47:12 +00:00
|
|
|
a[0] = '\n';
|
|
|
|
a[1] = '\0';
|
2022-07-26 17:08:46 +00:00
|
|
|
break;
|
|
|
|
case 'f':
|
2022-07-27 17:47:12 +00:00
|
|
|
a[0] = '\f';
|
|
|
|
a[1] = '\0';
|
2022-07-26 17:08:46 +00:00
|
|
|
break;
|
|
|
|
case 'r':
|
2022-07-27 17:47:12 +00:00
|
|
|
a[0] = '\r';
|
|
|
|
a[1] = '\0';
|
2022-07-26 17:08:46 +00:00
|
|
|
break;
|
|
|
|
case 'u':
|
2022-07-27 17:47:12 +00:00
|
|
|
/* Read \uXXXX point into a 4-byte buffer */
|
|
|
|
if (fscanf(in, "%04lx", &utf8) != 1)
|
2022-07-26 17:08:46 +00:00
|
|
|
{
|
|
|
|
/* Bad hex value */
|
2022-10-13 16:18:42 +00:00
|
|
|
Free(str);
|
2022-07-26 17:08:46 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
2022-07-27 17:47:12 +00:00
|
|
|
|
2022-07-27 21:27:34 +00:00
|
|
|
if (utf8 == 0)
|
|
|
|
{
|
|
|
|
/*
|
2022-11-23 17:31:57 +00:00
|
|
|
* We read in a 0000, null. There is no
|
2022-07-27 21:27:34 +00:00
|
|
|
* circumstance in which putting a null
|
|
|
|
* character into our buffer will end well.
|
|
|
|
*
|
|
|
|
* There's also really no legitimate use
|
|
|
|
* for the null character in our JSON anyway;
|
|
|
|
* it's likely an attempted exploit.
|
|
|
|
*
|
|
|
|
* So lets just strip it out. Don't even
|
|
|
|
* include it in the string. There should be
|
|
|
|
* no harm in ignoring it.
|
|
|
|
*/
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2022-07-27 17:47:12 +00:00
|
|
|
/* Encode the 4-byte UTF-8 buffer into a series
|
|
|
|
* of 1-byte characters */
|
2023-01-08 04:38:06 +00:00
|
|
|
utf8Ptr = StrUtf8Encode(utf8);
|
2022-07-27 17:47:12 +00:00
|
|
|
if (!utf8Ptr)
|
|
|
|
{
|
|
|
|
/* Mem error */
|
2022-10-13 16:18:42 +00:00
|
|
|
Free(str);
|
2022-07-27 17:47:12 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2023-01-08 04:38:06 +00:00
|
|
|
/* Move the output of StrUtf8Encode() into our
|
|
|
|
* local buffer */
|
2022-07-27 17:47:12 +00:00
|
|
|
strcpy(a, utf8Ptr);
|
2022-10-13 16:18:42 +00:00
|
|
|
Free(utf8Ptr);
|
2022-07-26 17:08:46 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
/* Bad escape value */
|
2022-10-13 16:18:42 +00:00
|
|
|
Free(str);
|
2022-07-26 17:08:46 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
2022-07-27 17:47:12 +00:00
|
|
|
a[0] = c;
|
|
|
|
a[1] = '\0';
|
2022-07-26 17:08:46 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2022-07-27 17:47:12 +00:00
|
|
|
/* Append buffer a */
|
|
|
|
i = 0;
|
|
|
|
while (a[i] != '\0')
|
2022-07-26 17:08:46 +00:00
|
|
|
{
|
2022-07-27 17:47:12 +00:00
|
|
|
if (len >= allocated)
|
2022-07-26 17:08:46 +00:00
|
|
|
{
|
2022-07-27 17:47:12 +00:00
|
|
|
char *tmp;
|
|
|
|
|
|
|
|
allocated += strBlockSize;
|
2022-10-13 16:18:42 +00:00
|
|
|
tmp = Realloc(str, allocated * sizeof(char));
|
2022-07-27 17:47:12 +00:00
|
|
|
if (!tmp)
|
|
|
|
{
|
2022-10-13 16:18:42 +00:00
|
|
|
Free(str);
|
2022-07-27 17:47:12 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
str = tmp;
|
2022-07-26 17:08:46 +00:00
|
|
|
}
|
|
|
|
|
2022-07-27 17:47:12 +00:00
|
|
|
str[len] = a[i];
|
|
|
|
len++;
|
|
|
|
i++;
|
2022-07-26 17:08:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-13 16:18:42 +00:00
|
|
|
Free(str);
|
2022-07-26 17:08:46 +00:00
|
|
|
return NULL;
|
2022-07-26 13:10:17 +00:00
|
|
|
}
|
|
|
|
|
2022-07-27 21:27:34 +00:00
|
|
|
void
|
2023-03-08 17:15:43 +00:00
|
|
|
JsonEncodeValue(JsonValue * value, FILE * out, int level)
|
2022-07-26 02:56:14 +00:00
|
|
|
{
|
|
|
|
size_t i;
|
|
|
|
size_t len;
|
|
|
|
Array *arr;
|
|
|
|
|
|
|
|
switch (value->type)
|
|
|
|
{
|
|
|
|
case JSON_OBJECT:
|
2023-03-08 17:15:43 +00:00
|
|
|
JsonEncode(value->as.object, out, level >= 0 ? level : level);
|
2022-07-26 02:56:14 +00:00
|
|
|
break;
|
|
|
|
case JSON_ARRAY:
|
|
|
|
arr = value->as.array;
|
|
|
|
len = ArraySize(arr);
|
|
|
|
|
|
|
|
fputc('[', out);
|
|
|
|
for (i = 0; i < len; i++)
|
|
|
|
{
|
2023-03-08 17:15:43 +00:00
|
|
|
if (level >= 0)
|
|
|
|
{
|
|
|
|
fprintf(out, "\n%*s", level + 2, "");
|
|
|
|
}
|
|
|
|
JsonEncodeValue(ArrayGet(arr, i), out, level >= 0 ? level + 2 : level);
|
2022-07-26 02:56:14 +00:00
|
|
|
if (i < len - 1)
|
|
|
|
{
|
|
|
|
fputc(',', out);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-08 17:15:43 +00:00
|
|
|
if (level >= 0)
|
|
|
|
{
|
|
|
|
fprintf(out, "\n%*s", level, "");
|
|
|
|
}
|
2022-07-26 02:56:14 +00:00
|
|
|
fputc(']', out);
|
|
|
|
break;
|
|
|
|
case JSON_STRING:
|
2022-07-26 17:08:46 +00:00
|
|
|
JsonEncodeString(value->as.string, out);
|
2022-07-26 02:56:14 +00:00
|
|
|
break;
|
|
|
|
case JSON_INTEGER:
|
2022-07-27 13:47:22 +00:00
|
|
|
fprintf(out, "%ld", value->as.integer);
|
2022-07-26 02:56:14 +00:00
|
|
|
break;
|
|
|
|
case JSON_FLOAT:
|
|
|
|
fprintf(out, "%f", value->as.floating);
|
|
|
|
break;
|
|
|
|
case JSON_BOOLEAN:
|
|
|
|
if (value->as.boolean)
|
|
|
|
{
|
|
|
|
fputs("true", out);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
fputs("false", out);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case JSON_NULL:
|
|
|
|
fputs("null", out);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2023-03-08 17:15:43 +00:00
|
|
|
JsonEncode(HashMap * object, FILE * out, int level)
|
2022-07-26 02:56:14 +00:00
|
|
|
{
|
|
|
|
size_t index;
|
|
|
|
size_t count;
|
|
|
|
char *key;
|
|
|
|
JsonValue *value;
|
|
|
|
|
|
|
|
if (!object || !out)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
count = 0;
|
|
|
|
while (HashMapIterate(object, &key, (void **) &value))
|
|
|
|
{
|
|
|
|
count++;
|
|
|
|
}
|
|
|
|
|
|
|
|
fputc('{', out);
|
2023-03-08 17:15:43 +00:00
|
|
|
if (level >= 0)
|
|
|
|
{
|
|
|
|
fputc('\n', out);
|
|
|
|
}
|
2022-07-26 02:56:14 +00:00
|
|
|
|
|
|
|
index = 0;
|
|
|
|
while (HashMapIterate(object, &key, (void **) &value))
|
|
|
|
{
|
2023-03-08 17:15:43 +00:00
|
|
|
if (level >= 0)
|
|
|
|
{
|
|
|
|
fprintf(out, "%*s", level + 2, "");
|
|
|
|
}
|
|
|
|
|
2022-07-26 17:08:46 +00:00
|
|
|
JsonEncodeString(key, out);
|
2023-03-08 17:15:43 +00:00
|
|
|
|
2022-07-26 17:08:46 +00:00
|
|
|
fputc(':', out);
|
2023-03-08 17:15:43 +00:00
|
|
|
if (level >= 0)
|
|
|
|
{
|
|
|
|
fputc(' ', out);
|
|
|
|
}
|
|
|
|
|
|
|
|
JsonEncodeValue(value, out, level >= 0 ? level + 2 : level);
|
2022-07-26 02:56:14 +00:00
|
|
|
|
|
|
|
if (index < count - 1)
|
|
|
|
{
|
|
|
|
fputc(',', out);
|
|
|
|
}
|
|
|
|
|
2023-03-08 17:15:43 +00:00
|
|
|
if (level >= 0)
|
|
|
|
{
|
|
|
|
fputc('\n', out);
|
|
|
|
}
|
|
|
|
|
2022-07-26 02:56:14 +00:00
|
|
|
index++;
|
|
|
|
}
|
|
|
|
|
2023-03-08 17:15:43 +00:00
|
|
|
if (level >= 0)
|
|
|
|
{
|
|
|
|
fprintf(out, "%*s", level, "");
|
|
|
|
}
|
2022-07-26 02:56:14 +00:00
|
|
|
fputc('}', out);
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
2022-07-27 21:27:34 +00:00
|
|
|
|
|
|
|
void
|
|
|
|
JsonFree(HashMap * object)
|
|
|
|
{
|
|
|
|
char *key;
|
|
|
|
JsonValue *value;
|
|
|
|
|
|
|
|
while (HashMapIterate(object, &key, (void **) &value))
|
|
|
|
{
|
|
|
|
JsonValueFree(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
HashMapFree(object);
|
|
|
|
}
|
2022-08-02 20:58:15 +00:00
|
|
|
|
2022-08-03 01:47:27 +00:00
|
|
|
static int
|
2022-08-02 20:58:15 +00:00
|
|
|
JsonConsumeWhitespace(JsonParserState * state)
|
|
|
|
{
|
|
|
|
int c;
|
2023-03-10 18:48:52 +00:00
|
|
|
|
2023-03-10 18:46:03 +00:00
|
|
|
while (1)
|
|
|
|
{
|
|
|
|
c = fgetc(state->stream);
|
|
|
|
|
|
|
|
if (feof(state->stream))
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ferror(state->stream))
|
|
|
|
{
|
|
|
|
if (errno == EAGAIN)
|
|
|
|
{
|
|
|
|
clearerr(state->stream);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2022-08-02 20:58:15 +00:00
|
|
|
|
2023-03-10 18:46:03 +00:00
|
|
|
if (!isspace(c))
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2022-08-03 01:47:27 +00:00
|
|
|
|
|
|
|
return c;
|
2022-08-02 20:58:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
JsonTokenSeek(JsonParserState * state)
|
|
|
|
{
|
2022-08-03 01:47:27 +00:00
|
|
|
int c = JsonConsumeWhitespace(state);
|
2022-08-02 20:58:15 +00:00
|
|
|
|
2022-08-03 01:47:27 +00:00
|
|
|
if (feof(state->stream))
|
|
|
|
{
|
|
|
|
state->tokenType = TOKEN_EOF;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (state->token)
|
|
|
|
{
|
2022-10-13 16:18:42 +00:00
|
|
|
Free(state->token);
|
2022-08-03 01:47:27 +00:00
|
|
|
state->token = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (c)
|
|
|
|
{
|
|
|
|
case ':':
|
|
|
|
state->tokenType = TOKEN_COLON;
|
|
|
|
break;
|
|
|
|
case ',':
|
|
|
|
state->tokenType = TOKEN_COMMA;
|
|
|
|
break;
|
|
|
|
case '{':
|
|
|
|
state->tokenType = TOKEN_OBJECT_OPEN;
|
|
|
|
break;
|
|
|
|
case '}':
|
|
|
|
state->tokenType = TOKEN_OBJECT_CLOSE;
|
|
|
|
break;
|
|
|
|
case '[':
|
|
|
|
state->tokenType = TOKEN_ARRAY_OPEN;
|
|
|
|
break;
|
|
|
|
case ']':
|
|
|
|
state->tokenType = TOKEN_ARRAY_CLOSE;
|
|
|
|
break;
|
|
|
|
case '"':
|
|
|
|
state->token = JsonDecodeString(state->stream);
|
|
|
|
if (!state->token)
|
|
|
|
{
|
|
|
|
state->tokenType = TOKEN_EOF;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
state->tokenType = TOKEN_STRING;
|
2022-11-17 23:51:45 +00:00
|
|
|
state->tokenLen = strlen(state->token);
|
2022-08-03 01:47:27 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
if (c == '-' || isdigit(c))
|
|
|
|
{
|
|
|
|
int isFloat = 0;
|
|
|
|
size_t allocated = 16;
|
|
|
|
|
2022-08-09 17:19:14 +00:00
|
|
|
state->tokenLen = 1;
|
2022-10-13 16:18:42 +00:00
|
|
|
state->token = Malloc(allocated);
|
2022-08-03 01:47:27 +00:00
|
|
|
if (!state->token)
|
|
|
|
{
|
|
|
|
state->tokenType = TOKEN_EOF;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
state->token[0] = c;
|
|
|
|
|
|
|
|
while ((c = fgetc(state->stream)) != EOF)
|
|
|
|
{
|
|
|
|
if (c == '.')
|
|
|
|
{
|
2022-11-18 14:51:44 +00:00
|
|
|
if (state->tokenLen > 1 && !isFloat)
|
2022-08-03 01:47:27 +00:00
|
|
|
{
|
|
|
|
isFloat = 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
state->tokenType = TOKEN_UNKNOWN;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2022-11-18 14:51:44 +00:00
|
|
|
else if (!isdigit(c))
|
2022-08-03 01:47:27 +00:00
|
|
|
{
|
|
|
|
ungetc(c, state->stream);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2022-08-09 17:19:14 +00:00
|
|
|
if (state->tokenLen >= allocated)
|
2022-08-03 01:47:27 +00:00
|
|
|
{
|
|
|
|
char *tmp;
|
|
|
|
|
|
|
|
allocated += 16;
|
|
|
|
|
2022-10-13 16:18:42 +00:00
|
|
|
tmp = Realloc(state->token, allocated);
|
2022-08-03 01:47:27 +00:00
|
|
|
if (!tmp)
|
|
|
|
{
|
|
|
|
state->tokenType = TOKEN_EOF;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
state->token = tmp;
|
|
|
|
}
|
|
|
|
|
2022-08-09 17:19:14 +00:00
|
|
|
state->token[state->tokenLen] = c;
|
|
|
|
state->tokenLen++;
|
2022-08-03 01:47:27 +00:00
|
|
|
}
|
|
|
|
|
2022-08-09 17:19:14 +00:00
|
|
|
if (state->token[state->tokenLen - 1] == '.')
|
2022-08-03 01:47:27 +00:00
|
|
|
{
|
|
|
|
state->tokenType = TOKEN_UNKNOWN;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-08-09 17:19:14 +00:00
|
|
|
state->token[state->tokenLen] = '\0';
|
2022-08-03 01:47:27 +00:00
|
|
|
if (isFloat)
|
|
|
|
{
|
|
|
|
state->tokenType = TOKEN_FLOAT;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
state->tokenType = TOKEN_INTEGER;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-08-09 17:19:14 +00:00
|
|
|
state->tokenLen = 8;
|
2022-10-13 16:18:42 +00:00
|
|
|
state->token = Malloc(state->tokenLen);
|
2022-08-03 01:47:27 +00:00
|
|
|
if (!state->token)
|
|
|
|
{
|
|
|
|
state->tokenType = TOKEN_EOF;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-11-18 14:51:44 +00:00
|
|
|
state->token[0] = c;
|
|
|
|
|
2022-08-03 01:47:27 +00:00
|
|
|
switch (c)
|
|
|
|
{
|
|
|
|
case 't':
|
2022-11-18 14:51:44 +00:00
|
|
|
if (!fgets(state->token + 1, 4, state->stream))
|
2022-08-10 00:18:13 +00:00
|
|
|
{
|
|
|
|
state->tokenType = TOKEN_EOF;
|
2022-12-14 14:54:06 +00:00
|
|
|
Free(state->token);
|
|
|
|
state->token = NULL;
|
2022-08-10 00:18:13 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-08-03 01:47:27 +00:00
|
|
|
if (!strcmp("true", state->token))
|
|
|
|
{
|
|
|
|
state->tokenType = TOKEN_BOOLEAN;
|
2022-08-09 17:19:14 +00:00
|
|
|
state->tokenLen = 5;
|
2022-08-03 01:47:27 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
state->tokenType = TOKEN_UNKNOWN;
|
2022-12-14 14:54:06 +00:00
|
|
|
Free(state->token);
|
|
|
|
state->token = NULL;
|
2022-08-03 01:47:27 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'f':
|
2022-11-18 14:51:44 +00:00
|
|
|
if (!fgets(state->token + 1, 5, state->stream))
|
2022-08-10 00:18:13 +00:00
|
|
|
{
|
|
|
|
state->tokenType = TOKEN_EOF;
|
2022-12-14 14:54:06 +00:00
|
|
|
Free(state->token);
|
|
|
|
state->token = NULL;
|
2022-08-10 00:18:13 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-08-03 01:47:27 +00:00
|
|
|
if (!strcmp("false", state->token))
|
|
|
|
{
|
|
|
|
state->tokenType = TOKEN_BOOLEAN;
|
2022-08-09 17:19:14 +00:00
|
|
|
state->tokenLen = 6;
|
2022-08-03 01:47:27 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
state->tokenType = TOKEN_UNKNOWN;
|
2022-12-14 14:54:06 +00:00
|
|
|
Free(state->token);
|
|
|
|
state->token = NULL;
|
2022-08-03 01:47:27 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'n':
|
2022-11-18 14:51:44 +00:00
|
|
|
if (!fgets(state->token + 1, 4, state->stream))
|
2022-08-10 00:18:13 +00:00
|
|
|
{
|
|
|
|
state->tokenType = TOKEN_EOF;
|
2022-12-14 14:54:06 +00:00
|
|
|
Free(state->token);
|
|
|
|
state->token = NULL;
|
2022-08-10 00:18:13 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-08-09 17:19:14 +00:00
|
|
|
if (!strcmp("null", state->token))
|
2022-08-03 01:47:27 +00:00
|
|
|
{
|
|
|
|
state->tokenType = TOKEN_NULL;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
state->tokenType = TOKEN_UNKNOWN;
|
2022-12-14 14:54:06 +00:00
|
|
|
Free(state->token);
|
|
|
|
state->token = NULL;
|
2022-08-03 01:47:27 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
state->tokenType = TOKEN_UNKNOWN;
|
2022-12-14 14:54:06 +00:00
|
|
|
Free(state->token);
|
|
|
|
state->token = NULL;
|
2022-08-03 01:47:27 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-08-02 20:58:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
JsonExpect(JsonParserState * state, JsonToken token)
|
|
|
|
{
|
|
|
|
return state->tokenType == token;
|
|
|
|
}
|
|
|
|
|
2022-08-09 17:19:14 +00:00
|
|
|
static Array *
|
|
|
|
JsonDecodeArray(JsonParserState *);
|
|
|
|
|
|
|
|
static HashMap *
|
|
|
|
JsonDecodeObject(JsonParserState *);
|
|
|
|
|
|
|
|
static JsonValue *
|
|
|
|
JsonDecodeValue(JsonParserState * state)
|
|
|
|
{
|
|
|
|
JsonValue *value;
|
|
|
|
char *strValue;
|
|
|
|
|
|
|
|
switch (state->tokenType)
|
|
|
|
{
|
|
|
|
case TOKEN_OBJECT_OPEN:
|
|
|
|
value = JsonValueObject(JsonDecodeObject(state));
|
|
|
|
break;
|
|
|
|
case TOKEN_ARRAY_OPEN:
|
|
|
|
value = JsonValueArray(JsonDecodeArray(state));
|
|
|
|
break;
|
|
|
|
case TOKEN_STRING:
|
2023-01-09 15:18:59 +00:00
|
|
|
strValue = Malloc(state->tokenLen + 1);
|
2022-08-09 17:19:14 +00:00
|
|
|
if (!strValue)
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
strcpy(strValue, state->token);
|
|
|
|
value = JsonValueString(strValue);
|
2023-02-24 00:17:56 +00:00
|
|
|
Free(strValue);
|
2022-08-09 17:19:14 +00:00
|
|
|
break;
|
|
|
|
case TOKEN_INTEGER:
|
|
|
|
value = JsonValueInteger(atol(state->token));
|
|
|
|
break;
|
|
|
|
case TOKEN_FLOAT:
|
|
|
|
value = JsonValueFloat(atof(state->token));
|
|
|
|
break;
|
|
|
|
case TOKEN_BOOLEAN:
|
|
|
|
value = JsonValueBoolean(state->token[0] == 't');
|
|
|
|
break;
|
|
|
|
case TOKEN_NULL:
|
|
|
|
value = JsonValueNull();
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
value = NULL;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
2022-08-02 20:58:15 +00:00
|
|
|
static HashMap *
|
|
|
|
JsonDecodeObject(JsonParserState * state)
|
|
|
|
{
|
2022-08-09 17:19:14 +00:00
|
|
|
HashMap *obj = HashMapCreate();
|
2023-02-26 15:07:46 +00:00
|
|
|
int comma = 0;
|
2022-08-09 17:19:14 +00:00
|
|
|
|
|
|
|
if (!obj)
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
|
|
|
JsonTokenSeek(state);
|
|
|
|
if (JsonExpect(state, TOKEN_STRING))
|
|
|
|
{
|
2022-11-17 23:51:45 +00:00
|
|
|
char *key = Malloc(state->tokenLen + 1);
|
2022-08-09 17:19:14 +00:00
|
|
|
JsonValue *value;
|
|
|
|
|
|
|
|
if (!key)
|
|
|
|
{
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
strcpy(key, state->token);
|
|
|
|
|
|
|
|
JsonTokenSeek(state);
|
|
|
|
if (!JsonExpect(state, TOKEN_COLON))
|
|
|
|
{
|
2022-11-17 23:51:45 +00:00
|
|
|
Free(key);
|
2022-08-09 17:19:14 +00:00
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
JsonTokenSeek(state);
|
|
|
|
value = JsonDecodeValue(state);
|
|
|
|
|
|
|
|
if (!value)
|
|
|
|
{
|
2022-11-17 23:51:45 +00:00
|
|
|
Free(key);
|
2022-08-09 17:19:14 +00:00
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
2022-12-06 21:26:33 +00:00
|
|
|
/* If there's an existing value at this key, discard it. */
|
|
|
|
JsonValueFree(HashMapSet(obj, key, value));
|
2023-02-23 23:19:23 +00:00
|
|
|
Free(key);
|
2022-08-09 17:19:14 +00:00
|
|
|
|
|
|
|
JsonTokenSeek(state);
|
|
|
|
|
|
|
|
if (JsonExpect(state, TOKEN_COMMA))
|
|
|
|
{
|
2023-02-26 15:07:46 +00:00
|
|
|
comma = 1;
|
2022-08-09 17:19:14 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2023-02-26 15:07:46 +00:00
|
|
|
if (JsonExpect(state, TOKEN_OBJECT_CLOSE))
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2022-08-09 17:19:14 +00:00
|
|
|
goto error;
|
|
|
|
}
|
2023-02-26 15:07:46 +00:00
|
|
|
else if (!comma && JsonExpect(state, TOKEN_OBJECT_CLOSE))
|
2022-08-09 17:19:14 +00:00
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
} while (!JsonExpect(state, TOKEN_EOF));
|
|
|
|
|
|
|
|
return obj;
|
|
|
|
error:
|
|
|
|
JsonFree(obj);
|
2022-08-02 20:58:15 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static Array *
|
|
|
|
JsonDecodeArray(JsonParserState * state)
|
|
|
|
{
|
2022-08-09 17:19:14 +00:00
|
|
|
Array *arr = ArrayCreate();
|
|
|
|
size_t i;
|
2023-02-26 15:07:46 +00:00
|
|
|
int comma = 0;
|
2022-08-09 17:19:14 +00:00
|
|
|
|
|
|
|
if (!arr)
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
|
|
|
JsonValue *value;
|
|
|
|
|
|
|
|
JsonTokenSeek(state);
|
2023-02-26 15:07:46 +00:00
|
|
|
|
|
|
|
if (!comma && JsonExpect(state, TOKEN_ARRAY_CLOSE))
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2022-08-09 17:19:14 +00:00
|
|
|
value = JsonDecodeValue(state);
|
|
|
|
|
|
|
|
if (!value)
|
|
|
|
{
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
ArrayAdd(arr, value);
|
|
|
|
|
|
|
|
JsonTokenSeek(state);
|
|
|
|
|
2023-02-26 15:07:46 +00:00
|
|
|
if (JsonExpect(state, TOKEN_COMMA))
|
2022-08-09 17:19:14 +00:00
|
|
|
{
|
2023-02-26 15:07:46 +00:00
|
|
|
comma = 1;
|
|
|
|
continue;
|
2022-08-09 17:19:14 +00:00
|
|
|
}
|
|
|
|
|
2023-02-26 15:07:46 +00:00
|
|
|
if (JsonExpect(state, TOKEN_ARRAY_CLOSE))
|
2022-08-09 17:19:14 +00:00
|
|
|
{
|
2023-02-26 15:07:46 +00:00
|
|
|
break;
|
2022-08-09 17:19:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
goto error;
|
|
|
|
} while (!JsonExpect(state, TOKEN_EOF));
|
|
|
|
|
2022-11-18 14:51:44 +00:00
|
|
|
return arr;
|
2022-08-09 17:19:14 +00:00
|
|
|
error:
|
|
|
|
for (i = 0; i < ArraySize(arr); i++)
|
|
|
|
{
|
|
|
|
JsonValueFree((JsonValue *) ArrayGet(arr, i));
|
|
|
|
}
|
|
|
|
ArrayFree(arr);
|
2022-08-02 20:58:15 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
HashMap *
|
|
|
|
JsonDecode(FILE * stream)
|
|
|
|
{
|
2022-11-17 23:51:45 +00:00
|
|
|
HashMap *result;
|
|
|
|
|
2022-08-02 20:58:15 +00:00
|
|
|
JsonParserState state;
|
|
|
|
|
|
|
|
state.stream = stream;
|
2022-08-03 01:47:27 +00:00
|
|
|
state.token = NULL;
|
2022-08-02 20:58:15 +00:00
|
|
|
|
2022-08-09 17:19:14 +00:00
|
|
|
JsonTokenSeek(&state);
|
|
|
|
if (!JsonExpect(&state, TOKEN_OBJECT_OPEN))
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2022-11-17 23:51:45 +00:00
|
|
|
result = JsonDecodeObject(&state);
|
|
|
|
|
|
|
|
if (state.token)
|
|
|
|
{
|
|
|
|
Free(state.token);
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
2022-08-02 20:58:15 +00:00
|
|
|
}
|
2023-02-16 18:49:19 +00:00
|
|
|
|
|
|
|
JsonValue *
|
2023-02-17 03:23:25 +00:00
|
|
|
JsonGet(HashMap * json, size_t nArgs,...)
|
2023-02-16 18:49:19 +00:00
|
|
|
{
|
|
|
|
va_list argp;
|
|
|
|
|
|
|
|
HashMap *tmp = json;
|
|
|
|
JsonValue *val = NULL;
|
|
|
|
size_t i;
|
|
|
|
|
2023-02-17 03:23:25 +00:00
|
|
|
if (!json || !nArgs)
|
2023-02-16 18:49:19 +00:00
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
va_start(argp, nArgs);
|
|
|
|
for (i = 0; i < nArgs - 1; i++)
|
|
|
|
{
|
|
|
|
char *key = va_arg(argp, char *);
|
|
|
|
|
|
|
|
val = HashMapGet(tmp, key);
|
|
|
|
if (!val)
|
|
|
|
{
|
|
|
|
goto finish;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (JsonValueType(val) != JSON_OBJECT)
|
|
|
|
{
|
|
|
|
val = NULL;
|
|
|
|
goto finish;
|
|
|
|
}
|
|
|
|
|
|
|
|
tmp = JsonValueAsObject(val);
|
|
|
|
}
|
|
|
|
|
|
|
|
val = HashMapGet(tmp, va_arg(argp, char *));
|
|
|
|
|
|
|
|
finish:
|
|
|
|
va_end(argp);
|
|
|
|
return val;
|
|
|
|
}
|
|
|
|
|
|
|
|
JsonValue *
|
2023-02-17 03:23:25 +00:00
|
|
|
JsonSet(HashMap * json, JsonValue * newVal, size_t nArgs,...)
|
2023-02-16 18:49:19 +00:00
|
|
|
{
|
|
|
|
HashMap *tmp = json;
|
|
|
|
JsonValue *val = NULL;
|
|
|
|
size_t i;
|
|
|
|
|
|
|
|
va_list argp;
|
|
|
|
|
|
|
|
if (!json || !newVal || !nArgs)
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
va_start(argp, nArgs);
|
|
|
|
|
|
|
|
for (i = 0; i < nArgs - 1; i++)
|
|
|
|
{
|
|
|
|
char *key = va_arg(argp, char *);
|
|
|
|
|
|
|
|
val = HashMapGet(tmp, key);
|
|
|
|
if (!val)
|
|
|
|
{
|
2023-03-06 22:21:56 +00:00
|
|
|
val = JsonValueObject(HashMapCreate());
|
|
|
|
HashMapSet(tmp, key, val);
|
2023-02-16 18:49:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (JsonValueType(val) != JSON_OBJECT)
|
|
|
|
{
|
|
|
|
val = NULL;
|
|
|
|
goto finish;
|
|
|
|
}
|
|
|
|
|
|
|
|
tmp = JsonValueAsObject(val);
|
|
|
|
}
|
|
|
|
|
|
|
|
val = HashMapSet(tmp, va_arg(argp, char *), newVal);
|
|
|
|
|
|
|
|
finish:
|
|
|
|
va_end(argp);
|
|
|
|
return val;
|
|
|
|
}
|