2018-08-26 15:28:51 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018 Calvin Rose
|
|
|
|
*
|
|
|
|
* 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 included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2018-09-06 02:18:42 +00:00
|
|
|
#include <janet/janet.h>
|
2018-08-26 15:28:51 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <errno.h>
|
|
|
|
|
|
|
|
/*****************/
|
|
|
|
/* JSON Decoding */
|
|
|
|
/*****************/
|
|
|
|
|
|
|
|
/* Check if a character is whitespace */
|
|
|
|
static int white(uint8_t c) {
|
|
|
|
return c == '\t' || c == '\n' || c == ' ' || c == '\r';
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Skip whitespace */
|
|
|
|
static void skipwhite(const char **p) {
|
|
|
|
const char *cp = *p;
|
|
|
|
for (;;) {
|
|
|
|
if (white(*cp))
|
|
|
|
cp++;
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
*p = cp;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get a hex digit value */
|
|
|
|
static int hexdig(char dig) {
|
|
|
|
if (dig >= '0' && dig <= '9')
|
|
|
|
return dig - '0';
|
|
|
|
if (dig >= 'a' && dig <= 'f')
|
|
|
|
return 10 + dig - 'a';
|
|
|
|
if (dig >= 'A' && dig <= 'F')
|
|
|
|
return 10 + dig - 'A';
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Read the hex value for a unicode escape */
|
2018-10-04 21:25:46 +00:00
|
|
|
static const char *decode_utf16_escape(const char *p, uint32_t *outpoint) {
|
2018-08-26 15:28:51 +00:00
|
|
|
if (!p[0] || !p[1] || !p[2] || !p[3])
|
|
|
|
return "unexpected end of source";
|
|
|
|
int d1 = hexdig(p[0]);
|
|
|
|
int d2 = hexdig(p[1]);
|
|
|
|
int d3 = hexdig(p[2]);
|
|
|
|
int d4 = hexdig(p[3]);
|
|
|
|
if (d1 < 0 || d2 < 0 || d3 < 0 || d4 < 0)
|
|
|
|
return "invalid hex digit";
|
|
|
|
*outpoint = d4 | (d3 << 4) | (d2 << 8) | (d1 << 12);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2018-09-09 16:13:32 +00:00
|
|
|
/* Parse a string. Also handles the conversion of utf-16 to
|
|
|
|
* utf-8. */
|
2018-10-04 21:25:46 +00:00
|
|
|
static const char *decode_string(const char **p, Janet *out) {
|
2018-09-06 02:18:42 +00:00
|
|
|
JanetBuffer *buffer = janet_buffer(0);
|
2018-08-26 15:28:51 +00:00
|
|
|
const char *cp = *p;
|
|
|
|
while (*cp != '"') {
|
|
|
|
uint8_t b = (uint8_t) *cp;
|
|
|
|
if (b < 32) return "invalid character in string";
|
|
|
|
if (b == '\\') {
|
|
|
|
cp++;
|
|
|
|
switch(*cp) {
|
|
|
|
default:
|
|
|
|
return "unknown string escape";
|
|
|
|
case 'b':
|
|
|
|
b = '\b';
|
|
|
|
break;
|
|
|
|
case 'f':
|
|
|
|
b = '\f';
|
|
|
|
break;
|
|
|
|
case 'n':
|
|
|
|
b = '\n';
|
|
|
|
break;
|
|
|
|
case 'r':
|
|
|
|
b = '\r';
|
|
|
|
break;
|
|
|
|
case 't':
|
|
|
|
b = '\t';
|
|
|
|
break;
|
|
|
|
case '"':
|
|
|
|
b = '"';
|
|
|
|
break;
|
|
|
|
case '\\':
|
|
|
|
b = '\\';
|
|
|
|
break;
|
|
|
|
case 'u':
|
|
|
|
{
|
|
|
|
/* Get codepoint and check for surrogate pair */
|
|
|
|
uint32_t codepoint;
|
|
|
|
const char *err = decode_utf16_escape(cp + 1, &codepoint);
|
|
|
|
if (err) return err;
|
|
|
|
if (codepoint >= 0xDC00 && codepoint <= 0xDFFF) {
|
|
|
|
return "unexpected utf-16 low surrogate";
|
|
|
|
} else if (codepoint >= 0xD800 && codepoint <= 0xDBFF) {
|
|
|
|
if (cp[5] != '\\') return "expected utf-16 low surrogate pair";
|
|
|
|
if (cp[6] != 'u') return "expected utf-16 low surrogate pair";
|
|
|
|
uint32_t lowsur;
|
|
|
|
const char *err = decode_utf16_escape(cp + 7, &lowsur);
|
|
|
|
if (err) return err;
|
|
|
|
if (lowsur < 0xDC00 || lowsur > 0xDFFF)
|
|
|
|
return "expected utf-16 low surrogate pair";
|
|
|
|
codepoint = ((codepoint - 0xD800) << 10) +
|
|
|
|
(lowsur - 0xDC00) + 0x10000;
|
|
|
|
cp += 11;
|
|
|
|
} else {
|
|
|
|
cp += 5;
|
|
|
|
}
|
|
|
|
/* Write codepoint */
|
|
|
|
if (codepoint <= 0x7F) {
|
2018-09-06 02:18:42 +00:00
|
|
|
janet_buffer_push_u8(buffer, codepoint);
|
2018-08-26 15:28:51 +00:00
|
|
|
} else if (codepoint <= 0x7FF) {
|
2018-09-06 02:18:42 +00:00
|
|
|
janet_buffer_push_u8(buffer, ((codepoint >> 6) & 0x1F) | 0xC0);
|
|
|
|
janet_buffer_push_u8(buffer, ((codepoint >> 0) & 0x3F) | 0x80);
|
2018-08-26 15:28:51 +00:00
|
|
|
} else if (codepoint <= 0xFFFF) {
|
2018-09-06 02:18:42 +00:00
|
|
|
janet_buffer_push_u8(buffer, ((codepoint >> 12) & 0x0F) | 0xE0);
|
|
|
|
janet_buffer_push_u8(buffer, ((codepoint >> 6) & 0x3F) | 0x80);
|
|
|
|
janet_buffer_push_u8(buffer, ((codepoint >> 0) & 0x3F) | 0x80);
|
2018-08-26 15:28:51 +00:00
|
|
|
} else {
|
2018-09-06 02:18:42 +00:00
|
|
|
janet_buffer_push_u8(buffer, ((codepoint >> 18) & 0x07) | 0xF0);
|
|
|
|
janet_buffer_push_u8(buffer, ((codepoint >> 12) & 0x3F) | 0x80);
|
|
|
|
janet_buffer_push_u8(buffer, ((codepoint >> 6) & 0x3F) | 0x80);
|
|
|
|
janet_buffer_push_u8(buffer, ((codepoint >> 0) & 0x3F) | 0x80);
|
2018-08-26 15:28:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
2018-09-06 02:18:42 +00:00
|
|
|
janet_buffer_push_u8(buffer, b);
|
2018-08-26 15:28:51 +00:00
|
|
|
cp++;
|
|
|
|
}
|
2018-09-06 02:18:42 +00:00
|
|
|
*out = janet_stringv(buffer->data, buffer->count);
|
2018-08-26 15:28:51 +00:00
|
|
|
*p = cp + 1;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2018-09-06 02:18:42 +00:00
|
|
|
static const char *decode_one(const char **p, Janet *out, int depth) {
|
2018-08-26 15:28:51 +00:00
|
|
|
|
|
|
|
/* Prevent stack overflow */
|
2018-09-06 02:18:42 +00:00
|
|
|
if (depth > JANET_RECURSION_GUARD) goto recurdepth;
|
2018-08-26 15:28:51 +00:00
|
|
|
|
|
|
|
/* Skip leading whitepspace */
|
|
|
|
skipwhite(p);
|
|
|
|
|
|
|
|
/* Main switch */
|
|
|
|
switch (**p) {
|
|
|
|
default:
|
|
|
|
goto badchar;
|
|
|
|
case '\0':
|
|
|
|
goto eos;
|
|
|
|
/* Numbers */
|
|
|
|
case '-': case '0': case '1' : case '2': case '3' : case '4':
|
|
|
|
case '5': case '6': case '7' : case '8': case '9':
|
|
|
|
{
|
|
|
|
errno = 0;
|
|
|
|
char *end = NULL;
|
|
|
|
double x = strtod(*p, &end);
|
|
|
|
if (end == *p) goto badnum;
|
|
|
|
*p = end;
|
2018-09-06 02:18:42 +00:00
|
|
|
*out = janet_wrap_real(x);
|
2018-08-26 15:28:51 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
/* false, null, true */
|
|
|
|
case 'f':
|
|
|
|
{
|
|
|
|
const char *cp = *p;
|
|
|
|
if (cp[1] != 'a' || cp[2] != 'l' || cp[3] != 's' || cp[4] != 'e')
|
|
|
|
goto badident;
|
2018-09-06 02:18:42 +00:00
|
|
|
*out = janet_wrap_false();
|
2018-08-26 15:28:51 +00:00
|
|
|
*p = cp + 5;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 'n':
|
|
|
|
{
|
|
|
|
const char *cp = *p;
|
|
|
|
|
|
|
|
if (cp[1] != 'u' || cp[2] != 'l' || cp[3] != 'l')
|
|
|
|
goto badident;
|
2018-09-06 02:18:42 +00:00
|
|
|
*out = janet_wrap_nil();
|
2018-08-26 15:28:51 +00:00
|
|
|
*p = cp + 4;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 't':
|
|
|
|
{
|
|
|
|
const char *cp = *p;
|
|
|
|
if (cp[1] != 'r' || cp[2] != 'u' || cp[3] != 'e')
|
|
|
|
goto badident;
|
2018-09-06 02:18:42 +00:00
|
|
|
*out = janet_wrap_true();
|
2018-08-26 15:28:51 +00:00
|
|
|
*p = cp + 4;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
/* String */
|
|
|
|
case '"':
|
|
|
|
{
|
|
|
|
const char *cp = *p + 1;
|
|
|
|
const char *start = cp;
|
|
|
|
while (*cp >= 32 && *cp != '"' && *cp != '\\')
|
|
|
|
cp++;
|
|
|
|
/* Only use a buffer for strings with escapes, else just copy
|
|
|
|
* memory from source */
|
|
|
|
if (*cp == '\\') {
|
|
|
|
*p = *p + 1;
|
|
|
|
const char *err = decode_string(p, out);
|
|
|
|
if (err) return err;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (*cp != '"') goto badchar;
|
|
|
|
*p = cp + 1;
|
2018-09-06 02:18:42 +00:00
|
|
|
*out = janet_stringv((const uint8_t *)start, cp - start);
|
2018-08-26 15:28:51 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
/* Array */
|
|
|
|
case '[':
|
|
|
|
{
|
|
|
|
*p = *p + 1;
|
2018-09-06 02:18:42 +00:00
|
|
|
JanetArray *array = janet_array(0);
|
2018-08-26 15:28:51 +00:00
|
|
|
const char *err;
|
2018-09-06 02:18:42 +00:00
|
|
|
Janet subval;
|
2018-08-26 15:28:51 +00:00
|
|
|
skipwhite(p);
|
|
|
|
while (**p != ']') {
|
|
|
|
err = decode_one(p, &subval, depth + 1);
|
|
|
|
if (err) return err;
|
2018-09-06 02:18:42 +00:00
|
|
|
janet_array_push(array, subval);
|
2018-08-26 15:28:51 +00:00
|
|
|
skipwhite(p);
|
|
|
|
if (**p == ']') break;
|
|
|
|
if (**p != ',') goto wantcomma;
|
|
|
|
*p = *p + 1;
|
|
|
|
}
|
|
|
|
*p = *p + 1;
|
2018-09-06 02:18:42 +00:00
|
|
|
*out = janet_wrap_array(array);
|
2018-08-26 15:28:51 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
/* Object */
|
|
|
|
case '{':
|
|
|
|
{
|
|
|
|
*p = *p + 1;
|
2018-09-06 02:18:42 +00:00
|
|
|
JanetTable *table = janet_table(0);
|
2018-08-26 15:28:51 +00:00
|
|
|
const char *err;
|
2018-09-06 02:18:42 +00:00
|
|
|
Janet subkey, subval;
|
2018-08-26 15:28:51 +00:00
|
|
|
skipwhite(p);
|
|
|
|
while (**p != '}') {
|
|
|
|
skipwhite(p);
|
|
|
|
if (**p != '"') goto wantstring;
|
|
|
|
err = decode_one(p, &subkey, depth + 1);
|
|
|
|
if (err) return err;
|
|
|
|
skipwhite(p);
|
|
|
|
if (**p != ':') goto wantcolon;
|
|
|
|
*p = *p + 1;
|
|
|
|
err = decode_one(p, &subval, depth + 1);
|
|
|
|
if (err) return err;
|
2018-09-06 02:18:42 +00:00
|
|
|
janet_table_put(table, subkey, subval);
|
2018-08-26 15:28:51 +00:00
|
|
|
skipwhite(p);
|
|
|
|
if (**p == '}') break;
|
|
|
|
if (**p != ',') goto wantcomma;
|
|
|
|
*p = *p + 1;
|
|
|
|
}
|
|
|
|
*p = *p + 1;
|
2018-09-06 02:18:42 +00:00
|
|
|
*out = janet_wrap_table(table);
|
2018-08-26 15:28:51 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Good return */
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
/* Errors */
|
|
|
|
recurdepth:
|
|
|
|
return "recured too deeply";
|
|
|
|
eos:
|
|
|
|
return "unexpected end of source";
|
|
|
|
badident:
|
|
|
|
return "bad identifier";
|
|
|
|
badnum:
|
|
|
|
return "bad number";
|
|
|
|
wantcomma:
|
|
|
|
return "expected comma";
|
|
|
|
wantcolon:
|
|
|
|
return "expected colon";
|
|
|
|
badchar:
|
|
|
|
return "unexpected character";
|
|
|
|
wantstring:
|
|
|
|
return "expected json string";
|
|
|
|
}
|
|
|
|
|
2018-09-06 02:18:42 +00:00
|
|
|
static int json_decode(JanetArgs args) {
|
|
|
|
Janet ret;
|
|
|
|
JANET_FIXARITY(args, 1);
|
2018-08-26 15:28:51 +00:00
|
|
|
const char *err;
|
|
|
|
const char *start;
|
|
|
|
const char *p;
|
2018-09-06 02:18:42 +00:00
|
|
|
if (janet_checktype(args.v[0], JANET_BUFFER)) {
|
|
|
|
JanetBuffer *buffer = janet_unwrap_buffer(args.v[0]);
|
2018-08-26 15:28:51 +00:00
|
|
|
/* Ensure 0 padded */
|
2018-09-06 02:18:42 +00:00
|
|
|
janet_buffer_push_u8(buffer, 0);
|
2018-08-26 15:28:51 +00:00
|
|
|
start = p = (const char *)buffer->data;
|
|
|
|
err = decode_one(&p, &ret, 0);
|
|
|
|
buffer->count--;
|
|
|
|
} else {
|
|
|
|
const uint8_t *bytes;
|
|
|
|
int32_t len;
|
2018-09-06 02:18:42 +00:00
|
|
|
JANET_ARG_BYTES(bytes, len, args, 0);
|
2018-08-26 15:28:51 +00:00
|
|
|
start = p = (const char *)bytes;
|
|
|
|
err = decode_one(&p, &ret, 0);
|
|
|
|
}
|
|
|
|
/* Check trailing values */
|
|
|
|
if (!err) {
|
|
|
|
skipwhite(&p);
|
|
|
|
if (*p)
|
|
|
|
err = "unexpected extra token";
|
|
|
|
}
|
|
|
|
if (err) {
|
2018-09-06 02:18:42 +00:00
|
|
|
JANET_THROWV(args, janet_wrap_string(janet_formatc(
|
2018-08-26 15:28:51 +00:00
|
|
|
"decode error at postion %d: %s",
|
|
|
|
p - start,
|
|
|
|
err)));
|
|
|
|
}
|
2018-09-06 02:18:42 +00:00
|
|
|
JANET_RETURN(args, ret);
|
2018-08-26 15:28:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*****************/
|
|
|
|
/* JSON Encoding */
|
|
|
|
/*****************/
|
|
|
|
|
|
|
|
typedef struct {
|
2018-09-06 02:18:42 +00:00
|
|
|
JanetBuffer *buffer;
|
2018-08-26 15:28:51 +00:00
|
|
|
int32_t indent;
|
|
|
|
const uint8_t *tab;
|
|
|
|
const uint8_t *newline;
|
|
|
|
int32_t tablen;
|
|
|
|
int32_t newlinelen;
|
|
|
|
} Encoder;
|
|
|
|
|
|
|
|
static const char *encode_newline(Encoder *e) {
|
2018-09-06 02:18:42 +00:00
|
|
|
if (janet_buffer_push_bytes(e->buffer, e->newline, e->newlinelen))
|
2018-08-26 15:28:51 +00:00
|
|
|
return "buffer overflow";
|
2018-10-04 21:25:46 +00:00
|
|
|
/* Skip loop if no tab string */
|
|
|
|
if (e->tablen) {
|
|
|
|
for (int32_t i = 0; i < e->indent; i++)
|
|
|
|
if (janet_buffer_push_bytes(e->buffer, e->tab, e->tablen))
|
|
|
|
return "buffer overflow";
|
|
|
|
}
|
2018-08-26 15:28:51 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2018-09-06 02:18:42 +00:00
|
|
|
static const char *encode_one(Encoder *e, Janet x, int depth) {
|
|
|
|
switch(janet_type(x)) {
|
2018-08-26 15:28:51 +00:00
|
|
|
default:
|
|
|
|
goto badtype;
|
2018-09-06 02:18:42 +00:00
|
|
|
case JANET_NIL:
|
2018-08-26 15:28:51 +00:00
|
|
|
{
|
2018-09-06 02:18:42 +00:00
|
|
|
if (janet_buffer_push_cstring(e->buffer, "null"))
|
2018-08-26 15:28:51 +00:00
|
|
|
goto overflow;
|
|
|
|
}
|
|
|
|
break;
|
2018-09-06 02:18:42 +00:00
|
|
|
case JANET_FALSE:
|
2018-08-26 15:28:51 +00:00
|
|
|
{
|
2018-09-06 02:18:42 +00:00
|
|
|
if (janet_buffer_push_cstring(e->buffer, "false"))
|
2018-08-26 15:28:51 +00:00
|
|
|
goto overflow;
|
|
|
|
}
|
|
|
|
break;
|
2018-09-06 02:18:42 +00:00
|
|
|
case JANET_TRUE:
|
2018-08-26 15:28:51 +00:00
|
|
|
{
|
2018-09-06 02:18:42 +00:00
|
|
|
if (janet_buffer_push_cstring(e->buffer, "true"))
|
2018-08-26 15:28:51 +00:00
|
|
|
goto overflow;
|
|
|
|
}
|
|
|
|
break;
|
2018-09-06 02:18:42 +00:00
|
|
|
case JANET_INTEGER:
|
2018-08-26 15:28:51 +00:00
|
|
|
{
|
|
|
|
char cbuf[20];
|
2018-09-06 02:18:42 +00:00
|
|
|
sprintf(cbuf, "%d", janet_unwrap_integer(x));
|
|
|
|
if (janet_buffer_push_cstring(e->buffer, cbuf))
|
2018-08-26 15:28:51 +00:00
|
|
|
goto overflow;
|
|
|
|
}
|
|
|
|
break;
|
2018-09-06 02:18:42 +00:00
|
|
|
case JANET_REAL:
|
2018-08-26 15:28:51 +00:00
|
|
|
{
|
|
|
|
char cbuf[25];
|
2018-09-06 02:18:42 +00:00
|
|
|
sprintf(cbuf, "%.17g", janet_unwrap_real(x));
|
|
|
|
if (janet_buffer_push_cstring(e->buffer, cbuf))
|
2018-08-26 15:28:51 +00:00
|
|
|
goto overflow;
|
|
|
|
}
|
|
|
|
break;
|
2018-09-06 02:18:42 +00:00
|
|
|
case JANET_STRING:
|
|
|
|
case JANET_SYMBOL:
|
|
|
|
case JANET_BUFFER:
|
2018-08-26 15:28:51 +00:00
|
|
|
{
|
|
|
|
const uint8_t *bytes;
|
|
|
|
const uint8_t *c;
|
|
|
|
const uint8_t *end;
|
|
|
|
int32_t len;
|
2018-09-06 02:18:42 +00:00
|
|
|
janet_bytes_view(x, &bytes, &len);
|
|
|
|
if (janet_buffer_push_u8(e->buffer, '"')) goto overflow;
|
2018-08-26 15:28:51 +00:00
|
|
|
c = bytes;
|
|
|
|
end = bytes + len;
|
|
|
|
while (c < end) {
|
|
|
|
|
|
|
|
/* get codepoint */
|
|
|
|
uint32_t codepoint;
|
|
|
|
if (*c < 0x80) {
|
|
|
|
/* one byte */
|
|
|
|
codepoint = *c++;
|
|
|
|
} else if (*c < 0xE0) {
|
|
|
|
/* two bytes */
|
|
|
|
if (c + 2 > end) goto overflow;
|
|
|
|
codepoint = ((c[0] & 0x1F) << 6) |
|
|
|
|
(c[1] & 0x3F);
|
|
|
|
c += 2;
|
|
|
|
} else if (*c < 0xF0) {
|
|
|
|
/* three bytes */
|
|
|
|
if (c + 3 > end) goto overflow;
|
|
|
|
codepoint = ((c[0] & 0x0F) << 12) |
|
|
|
|
((c[1] & 0x3F) << 6) |
|
|
|
|
(c[2] & 0x3F);
|
|
|
|
c += 3;
|
|
|
|
} else if (*c < 0xF8) {
|
|
|
|
/* four bytes */
|
|
|
|
if (c + 4 > end) goto overflow;
|
|
|
|
codepoint = ((c[0] & 0x07) << 18) |
|
|
|
|
((c[1] & 0x3F) << 12) |
|
|
|
|
((c[3] & 0x3F) << 6) |
|
|
|
|
(c[3] & 0x3F);
|
|
|
|
c += 4;
|
|
|
|
} else {
|
|
|
|
/* invalid */
|
|
|
|
goto invalidutf8;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* write codepoint */
|
|
|
|
if (codepoint > 0x1F && codepoint < 0x80) {
|
|
|
|
/* Normal, no escape */
|
|
|
|
if (codepoint == '\\' || codepoint == '"')
|
2018-09-06 02:18:42 +00:00
|
|
|
if (janet_buffer_push_u8(e->buffer, '\\'))
|
2018-08-26 15:28:51 +00:00
|
|
|
goto overflow;
|
2018-09-06 02:18:42 +00:00
|
|
|
if (janet_buffer_push_u8(e->buffer, (uint8_t) codepoint))
|
2018-08-26 15:28:51 +00:00
|
|
|
goto overflow;
|
|
|
|
} else if (codepoint < 0x10000) {
|
|
|
|
/* One unicode escape */
|
|
|
|
uint8_t buf[6];
|
|
|
|
buf[0] = '\\';
|
|
|
|
buf[1] = 'u';
|
|
|
|
buf[2] = (codepoint >> 12) & 0xF;
|
|
|
|
buf[3] = (codepoint >> 8) & 0xF;
|
|
|
|
buf[4] = (codepoint >> 4) & 0xF;
|
|
|
|
buf[5] = codepoint & 0xF;
|
2018-09-06 02:18:42 +00:00
|
|
|
if (janet_buffer_push_bytes(e->buffer, buf, sizeof(buf)))
|
2018-08-26 15:28:51 +00:00
|
|
|
goto overflow;
|
|
|
|
} else {
|
|
|
|
/* Two unicode escapes (surrogate pair) */
|
|
|
|
uint32_t hi, lo;
|
|
|
|
uint8_t buf[12];
|
|
|
|
hi = ((codepoint - 0x10000) >> 10) + 0xD800;
|
|
|
|
lo = ((codepoint - 0x10000) & 0x3FF) + 0xDC00;
|
|
|
|
buf[0] = '\\';
|
|
|
|
buf[1] = 'u';
|
|
|
|
buf[2] = (hi >> 12) & 0xF;
|
|
|
|
buf[3] = (hi >> 8) & 0xF;
|
|
|
|
buf[4] = (hi >> 4) & 0xF;
|
|
|
|
buf[5] = hi & 0xF;
|
|
|
|
buf[6] = '\\';
|
|
|
|
buf[7] = 'u';
|
|
|
|
buf[8] = (lo >> 12) & 0xF;
|
|
|
|
buf[9] = (lo >> 8) & 0xF;
|
|
|
|
buf[10] = (lo >> 4) & 0xF;
|
|
|
|
buf[11] = lo & 0xF;
|
2018-09-06 02:18:42 +00:00
|
|
|
if (janet_buffer_push_bytes(e->buffer, buf, sizeof(buf)))
|
2018-08-26 15:28:51 +00:00
|
|
|
goto overflow;
|
|
|
|
}
|
|
|
|
}
|
2018-09-06 02:18:42 +00:00
|
|
|
if (janet_buffer_push_u8(e->buffer, '"')) goto overflow;
|
2018-08-26 15:28:51 +00:00
|
|
|
}
|
|
|
|
break;
|
2018-09-06 02:18:42 +00:00
|
|
|
case JANET_TUPLE:
|
|
|
|
case JANET_ARRAY:
|
2018-08-26 15:28:51 +00:00
|
|
|
{
|
|
|
|
const char *err;
|
2018-09-06 02:18:42 +00:00
|
|
|
const Janet *items;
|
2018-08-26 15:28:51 +00:00
|
|
|
int32_t len;
|
2018-09-06 02:18:42 +00:00
|
|
|
janet_indexed_view(x, &items, &len);
|
|
|
|
if (janet_buffer_push_u8(e->buffer, '[')) goto overflow;
|
2018-08-26 15:28:51 +00:00
|
|
|
e->indent++;
|
|
|
|
if ((err = encode_newline(e))) return err;
|
|
|
|
for (int32_t i = 0; i < len; i++) {
|
|
|
|
if ((err = encode_newline(e))) return err;
|
|
|
|
if ((err = encode_one(e, items[i], depth + 1)))
|
|
|
|
return err;
|
2018-09-06 02:18:42 +00:00
|
|
|
if (janet_buffer_push_u8(e->buffer, ','))
|
2018-08-26 15:28:51 +00:00
|
|
|
goto overflow;
|
|
|
|
}
|
|
|
|
e->indent--;
|
|
|
|
if (e->buffer->data[e->buffer->count - 1] == ',') {
|
|
|
|
e->buffer->count--;
|
|
|
|
if ((err = encode_newline(e))) return err;
|
|
|
|
}
|
2018-09-06 02:18:42 +00:00
|
|
|
if (janet_buffer_push_u8(e->buffer, ']')) goto overflow;
|
2018-08-26 15:28:51 +00:00
|
|
|
}
|
|
|
|
break;
|
2018-09-06 02:18:42 +00:00
|
|
|
case JANET_TABLE:
|
|
|
|
case JANET_STRUCT:
|
2018-08-26 15:28:51 +00:00
|
|
|
{
|
|
|
|
const char *err;
|
2018-09-06 02:18:42 +00:00
|
|
|
const JanetKV *kvs;
|
2018-08-26 15:28:51 +00:00
|
|
|
int32_t count, capacity;
|
2018-09-06 02:18:42 +00:00
|
|
|
janet_dictionary_view(x, &kvs, &count, &capacity);
|
|
|
|
if (janet_buffer_push_u8(e->buffer, '{')) goto overflow;
|
2018-08-26 15:28:51 +00:00
|
|
|
e->indent++;
|
|
|
|
for (int32_t i = 0; i < capacity; i++) {
|
2018-09-06 02:18:42 +00:00
|
|
|
if (janet_checktype(kvs[i].key, JANET_NIL))
|
2018-08-26 15:28:51 +00:00
|
|
|
continue;
|
2018-09-06 02:18:42 +00:00
|
|
|
if (!janet_checktype(kvs[i].key, JANET_STRING))
|
2018-08-26 15:28:51 +00:00
|
|
|
return "only strings keys are allowed in objects";
|
|
|
|
if ((err = encode_newline(e))) return err;
|
|
|
|
if ((err = encode_one(e, kvs[i].key, depth + 1)))
|
|
|
|
return err;
|
|
|
|
const char *sep = e->tablen ? ": " : ":";
|
2018-09-06 02:18:42 +00:00
|
|
|
if (janet_buffer_push_cstring(e->buffer, sep))
|
2018-08-26 15:28:51 +00:00
|
|
|
goto overflow;
|
|
|
|
if ((err = encode_one(e, kvs[i].value, depth + 1)))
|
|
|
|
return err;
|
2018-09-06 02:18:42 +00:00
|
|
|
if (janet_buffer_push_u8(e->buffer, ','))
|
2018-08-26 15:28:51 +00:00
|
|
|
goto overflow;
|
|
|
|
}
|
|
|
|
e->indent--;
|
|
|
|
if (e->buffer->data[e->buffer->count - 1] == ',') {
|
|
|
|
e->buffer->count--;
|
|
|
|
if ((err = encode_newline(e))) return err;
|
|
|
|
}
|
2018-09-06 02:18:42 +00:00
|
|
|
if (janet_buffer_push_u8(e->buffer, '}')) goto overflow;
|
2018-08-26 15:28:51 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
/* Errors */
|
|
|
|
overflow:
|
|
|
|
return "buffer overflow";
|
|
|
|
badtype:
|
|
|
|
return "type not supported";
|
|
|
|
invalidutf8:
|
|
|
|
return "string contains invalid utf-8";
|
|
|
|
}
|
|
|
|
|
2018-09-06 02:18:42 +00:00
|
|
|
static int json_encode(JanetArgs args) {
|
|
|
|
JANET_MINARITY(args, 1);
|
|
|
|
JANET_MAXARITY(args, 3);
|
2018-08-26 15:28:51 +00:00
|
|
|
Encoder e;
|
|
|
|
e.indent = 0;
|
2018-09-06 02:18:42 +00:00
|
|
|
e.buffer = janet_buffer(10);
|
2018-08-26 15:28:51 +00:00
|
|
|
e.tab = NULL;
|
|
|
|
e.newline = NULL;
|
|
|
|
e.tablen = 0;
|
|
|
|
e.newlinelen = 0;
|
|
|
|
if (args.n >= 2) {
|
2018-09-06 02:18:42 +00:00
|
|
|
JANET_ARG_BYTES(e.tab, e.tablen, args, 1);
|
2018-08-26 15:28:51 +00:00
|
|
|
if (args.n >= 3) {
|
2018-09-06 02:18:42 +00:00
|
|
|
JANET_ARG_BYTES(e.newline, e.newlinelen, args, 2);
|
2018-08-26 15:28:51 +00:00
|
|
|
} else {
|
2018-09-03 16:51:57 +00:00
|
|
|
e.newline = (const uint8_t *)"\r\n";
|
2018-09-09 16:13:32 +00:00
|
|
|
e.newlinelen = 2;
|
2018-08-26 15:28:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
const char *err = encode_one(&e, args.v[0], 0);
|
2018-09-06 02:18:42 +00:00
|
|
|
if (err) JANET_THROW(args, err);
|
|
|
|
JANET_RETURN_BUFFER(args, e.buffer);
|
2018-08-26 15:28:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************/
|
|
|
|
/* Module Entry */
|
|
|
|
/****************/
|
|
|
|
|
2018-09-06 02:18:42 +00:00
|
|
|
static const JanetReg cfuns[] = {
|
2018-11-15 22:44:30 +00:00
|
|
|
{"encode", json_encode,
|
|
|
|
"(json.encode x)\n\n"
|
|
|
|
"Encodes a janet value in JSON (utf-8)."
|
|
|
|
},
|
|
|
|
{"decode", json_decode,
|
|
|
|
"(json.decode json-source)\n\n"
|
|
|
|
"Returns a janet object after parsing JSON."
|
|
|
|
},
|
2018-08-26 15:28:51 +00:00
|
|
|
{NULL, NULL}
|
|
|
|
};
|
|
|
|
|
2018-09-06 02:18:42 +00:00
|
|
|
JANET_MODULE_ENTRY(JanetArgs args) {
|
|
|
|
JanetTable *env = janet_env(args);
|
|
|
|
janet_cfuns(env, "json", cfuns);
|
2018-08-26 15:28:51 +00:00
|
|
|
return 0;
|
|
|
|
}
|