Skip to content

Commit

Permalink
update to yyjson v0.9.0
Browse files Browse the repository at this point in the history
  • Loading branch information
coolbutuseless committed Apr 8, 2024
1 parent 4a64d13 commit fbd212a
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 27 deletions.
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

* Fix some compilation warnings.
* Update documentation.
* Update to yyjson v0.9.0

# yyjsonr 0.1.18.9006 2024-04-02

Expand Down
2 changes: 1 addition & 1 deletion README.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ if (FALSE) {

In most cases it is around 2x to 10x faster than `{jsonlite}` at both reading and writing JSON.

It is a wrapper for the [`yyjson`](https://github.com/ibireme/yyjson) C library (v0.8.0).
It is a wrapper for the [`yyjson`](https://github.com/ibireme/yyjson) C library (v0.9.0 April 8, 2024).
`yysjon` is MIT licensed - see `LICENSE-yyjson.txt` in this package for more details.

### What's in the box
Expand Down
36 changes: 31 additions & 5 deletions src/yyjson.c
Original file line number Diff line number Diff line change
Expand Up @@ -1407,6 +1407,7 @@ bool yyjson_mut_doc_set_val_pool_size(yyjson_mut_doc *doc, size_t count) {
void yyjson_mut_doc_free(yyjson_mut_doc *doc) {
if (doc) {
yyjson_alc alc = doc->alc;
memset(&doc->alc, 0, sizeof(alc));
unsafe_yyjson_str_pool_release(&doc->str_pool, &alc);
unsafe_yyjson_val_pool_release(&doc->val_pool, &alc);
alc.free(alc.ctx, doc);
Expand Down Expand Up @@ -8309,21 +8310,23 @@ static_inline u8 *yyjson_write_single(yyjson_val *val,
bool cpy = (enc_table == enc_table_cpy);
bool esc = has_write_flag(ESCAPE_UNICODE) != 0;
bool inv = has_write_flag(ALLOW_INVALID_UNICODE) != 0;
bool newline = has_write_flag(NEWLINE_AT_END) != 0;
const usize end_len = 2; /* '\n' and '\0' */

switch (unsafe_yyjson_get_type(val)) {
case YYJSON_TYPE_RAW:
str_len = unsafe_yyjson_get_len(val);
str_ptr = (const u8 *)unsafe_yyjson_get_str(val);
check_str_len(str_len);
incr_len(str_len + 1);
incr_len(str_len + end_len);
cur = write_raw(cur, str_ptr, str_len);
break;

case YYJSON_TYPE_STR:
str_len = unsafe_yyjson_get_len(val);
str_ptr = (const u8 *)unsafe_yyjson_get_str(val);
check_str_len(str_len);
incr_len(str_len * 6 + 4);
incr_len(str_len * 6 + 2 + end_len);
if (likely(cpy) && unsafe_yyjson_get_subtype(val)) {
cur = write_string_noesc(cur, str_ptr, str_len);
} else {
Expand All @@ -8333,7 +8336,7 @@ static_inline u8 *yyjson_write_single(yyjson_val *val,
break;

case YYJSON_TYPE_NUM:
incr_len(32);
incr_len(32 + end_len);
cur = write_number(cur, val, flg);
if (unlikely(!cur)) goto fail_num;
break;
Expand All @@ -8349,13 +8352,13 @@ static_inline u8 *yyjson_write_single(yyjson_val *val,
break;

case YYJSON_TYPE_ARR:
incr_len(4);
incr_len(2 + end_len);
byte_copy_2(cur, "[]");
cur += 2;
break;

case YYJSON_TYPE_OBJ:
incr_len(4);
incr_len(2 + end_len);
byte_copy_2(cur, "{}");
cur += 2;
break;
Expand All @@ -8364,6 +8367,7 @@ static_inline u8 *yyjson_write_single(yyjson_val *val,
goto fail_type;
}

if (newline) *cur++ = '\n';
*cur = '\0';
*dat_len = (usize)(cur - hdr);
memset(err, 0, sizeof(yyjson_write_err));
Expand Down Expand Up @@ -8436,6 +8440,7 @@ static_inline u8 *yyjson_write_minify(const yyjson_val *root,
bool cpy = (enc_table == enc_table_cpy);
bool esc = has_write_flag(ESCAPE_UNICODE) != 0;
bool inv = has_write_flag(ALLOW_INVALID_UNICODE) != 0;
bool newline = has_write_flag(NEWLINE_AT_END) != 0;

alc_len = root->uni.ofs / sizeof(yyjson_val);
alc_len = alc_len * YYJSON_WRITER_ESTIMATED_MINIFY_RATIO + 64;
Expand Down Expand Up @@ -8542,6 +8547,11 @@ static_inline u8 *yyjson_write_minify(const yyjson_val *root,
}

doc_end:
if (newline) {
incr_len(2);
*(cur - 1) = '\n';
cur++;
}
*--cur = '\0';
*dat_len = (usize)(cur - hdr);
memset(err, 0, sizeof(yyjson_write_err));
Expand Down Expand Up @@ -8615,6 +8625,7 @@ static_inline u8 *yyjson_write_pretty(const yyjson_val *root,
bool esc = has_write_flag(ESCAPE_UNICODE) != 0;
bool inv = has_write_flag(ALLOW_INVALID_UNICODE) != 0;
usize spaces = has_write_flag(PRETTY_TWO_SPACES) ? 2 : 4;
bool newline = has_write_flag(NEWLINE_AT_END) != 0;

alc_len = root->uni.ofs / sizeof(yyjson_val);
alc_len = alc_len * YYJSON_WRITER_ESTIMATED_PRETTY_RATIO + 64;
Expand Down Expand Up @@ -8745,6 +8756,10 @@ static_inline u8 *yyjson_write_pretty(const yyjson_val *root,
}

doc_end:
if (newline) {
incr_len(2);
*cur++ = '\n';
}
*cur = '\0';
*dat_len = (usize)(cur - hdr);
memset(err, 0, sizeof(yyjson_write_err));
Expand Down Expand Up @@ -8977,6 +8992,7 @@ static_inline u8 *yyjson_mut_write_minify(const yyjson_mut_val *root,
bool cpy = (enc_table == enc_table_cpy);
bool esc = has_write_flag(ESCAPE_UNICODE) != 0;
bool inv = has_write_flag(ALLOW_INVALID_UNICODE) != 0;
bool newline = has_write_flag(NEWLINE_AT_END) != 0;

alc_len = estimated_val_num * YYJSON_WRITER_ESTIMATED_MINIFY_RATIO + 64;
alc_len = size_align_up(alc_len, sizeof(yyjson_mut_write_ctx));
Expand Down Expand Up @@ -9087,6 +9103,11 @@ static_inline u8 *yyjson_mut_write_minify(const yyjson_mut_val *root,
}

doc_end:
if (newline) {
incr_len(2);
*(cur - 1) = '\n';
cur++;
}
*--cur = '\0';
*dat_len = (usize)(cur - hdr);
err->code = YYJSON_WRITE_SUCCESS;
Expand Down Expand Up @@ -9162,6 +9183,7 @@ static_inline u8 *yyjson_mut_write_pretty(const yyjson_mut_val *root,
bool esc = has_write_flag(ESCAPE_UNICODE) != 0;
bool inv = has_write_flag(ALLOW_INVALID_UNICODE) != 0;
usize spaces = has_write_flag(PRETTY_TWO_SPACES) ? 2 : 4;
bool newline = has_write_flag(NEWLINE_AT_END) != 0;

alc_len = estimated_val_num * YYJSON_WRITER_ESTIMATED_PRETTY_RATIO + 64;
alc_len = size_align_up(alc_len, sizeof(yyjson_mut_write_ctx));
Expand Down Expand Up @@ -9296,6 +9318,10 @@ static_inline u8 *yyjson_mut_write_pretty(const yyjson_mut_val *root,
}

doc_end:
if (newline) {
incr_len(2);
*cur++ = '\n';
}
*cur = '\0';
*dat_len = (usize)(cur - hdr);
err->code = YYJSON_WRITE_SUCCESS;
Expand Down
56 changes: 35 additions & 21 deletions src/yyjson.h
Original file line number Diff line number Diff line change
Expand Up @@ -527,16 +527,16 @@ extern "C" {
#define YYJSON_VERSION_MAJOR 0

/** The minor version of yyjson. */
#define YYJSON_VERSION_MINOR 8
#define YYJSON_VERSION_MINOR 9

/** The patch version of yyjson. */
#define YYJSON_VERSION_PATCH 0

/** The version of yyjson in hex: `(major << 16) | (minor << 8) | (patch)`. */
#define YYJSON_VERSION_HEX 0x000800
#define YYJSON_VERSION_HEX 0x000900

/** The version string of yyjson. */
#define YYJSON_VERSION_STRING "0.8.0"
#define YYJSON_VERSION_STRING "0.9.0"

/** The version of yyjson in hex, same as `YYJSON_VERSION_HEX`. */
yyjson_api uint32_t yyjson_version(void);
Expand Down Expand Up @@ -738,7 +738,7 @@ typedef uint32_t yyjson_read_flag;
- Report error if double number is infinity.
- Report error if string contains invalid UTF-8 character or BOM.
- Report error on trailing commas, comments, inf and nan literals. */
static const yyjson_read_flag YYJSON_READ_NOFLAG = 0 << 0;
static const yyjson_read_flag YYJSON_READ_NOFLAG = 0;

/** Read the input data in-situ.
This option allows the reader to modify and use input data to store string
Expand Down Expand Up @@ -1067,7 +1067,7 @@ typedef uint32_t yyjson_write_flag;
- Report error on inf or nan number.
- Report error on invalid UTF-8 string.
- Do not escape unicode or slash. */
static const yyjson_write_flag YYJSON_WRITE_NOFLAG = 0 << 0;
static const yyjson_write_flag YYJSON_WRITE_NOFLAG = 0;

/** Write JSON pretty with 4 space indent. */
static const yyjson_write_flag YYJSON_WRITE_PRETTY = 1 << 0;
Expand Down Expand Up @@ -1096,6 +1096,10 @@ static const yyjson_write_flag YYJSON_WRITE_ALLOW_INVALID_UNICODE = 1 << 5;
This flag will override `YYJSON_WRITE_PRETTY` flag. */
static const yyjson_write_flag YYJSON_WRITE_PRETTY_TWO_SPACES = 1 << 6;

/** Adds a newline character `\n` at the end of the JSON.
This can be helpful for text editors or NDJSON. */
static const yyjson_write_flag YYJSON_WRITE_NEWLINE_AT_END = 1 << 7;



/** Result code for JSON writer */
Expand Down Expand Up @@ -3695,7 +3699,7 @@ yyjson_api_inline bool yyjson_mut_obj_add_strcpy(yyjson_mut_doc *doc,
The `len` should be the length of the `val`, in bytes.
This function allows duplicated key in one object.
@warning The key/value strings are not copied, you should keep these strings
@warning The key strings are not copied, you should keep these strings
unmodified for the lifetime of this JSON document. */
yyjson_api_inline bool yyjson_mut_obj_add_strncpy(yyjson_mut_doc *doc,
yyjson_mut_val *obj,
Expand Down Expand Up @@ -4829,6 +4833,7 @@ yyjson_api_inline size_t yyjson_doc_get_val_count(yyjson_doc *doc) {
yyjson_api_inline void yyjson_doc_free(yyjson_doc *doc) {
if (doc) {
yyjson_alc alc = doc->alc;
memset(&doc->alc, 0, sizeof(alc));
if (doc->str_pool) alc.free(alc.ctx, doc->str_pool);
alc.free(alc.ctx, doc);
}
Expand Down Expand Up @@ -5668,6 +5673,7 @@ yyjson_api_inline yyjson_mut_val *yyjson_mut_bool(yyjson_mut_doc *doc,
if (yyjson_likely(doc)) {
yyjson_mut_val *val = unsafe_yyjson_mut_val(doc, 1);
if (yyjson_likely(val)) {
_val = !!_val;
val->tag = YYJSON_TYPE_BOOL | (uint8_t)((uint8_t)_val << 3);
return val;
}
Expand Down Expand Up @@ -5920,7 +5926,8 @@ yyjson_api_inline yyjson_mut_val *yyjson_mut_arr(yyjson_mut_doc *doc) {
yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_with_bool(
yyjson_mut_doc *doc, const bool *vals, size_t count) {
yyjson_mut_arr_with_func({
val->tag = YYJSON_TYPE_BOOL | (uint8_t)((uint8_t)vals[i] << 3);
bool _val = !!vals[i];
val->tag = YYJSON_TYPE_BOOL | (uint8_t)((uint8_t)_val << 3);
});
}

Expand Down Expand Up @@ -6872,6 +6879,7 @@ yyjson_api_inline bool yyjson_mut_obj_add_bool(yyjson_mut_doc *doc,
const char *_key,
bool _val) {
yyjson_mut_obj_add_func({
_val = !!_val;
val->tag = YYJSON_TYPE_BOOL | (uint8_t)((uint8_t)(_val) << 3);
});
}
Expand Down Expand Up @@ -7730,33 +7738,39 @@ yyjson_api_inline bool yyjson_ptr_get_bool(
}

/**
Set provided `value` if the JSON Pointer (RFC 6901) exists and is type uint.
Returns true if value at `ptr` exists and is the correct type, otherwise false.
Set provided `value` if the JSON Pointer (RFC 6901) exists and is an integer
that fits in `uint64_t`. Returns true if successful, otherwise false.
*/
yyjson_api_inline bool yyjson_ptr_get_uint(
yyjson_val *root, const char *ptr, uint64_t *value) {
yyjson_val *val = yyjson_ptr_get(root, ptr);
if (value && yyjson_is_uint(val)) {
*value = unsafe_yyjson_get_uint(val);
return true;
} else {
return false;
if (value && val) {
uint64_t ret = val->uni.u64;
if (unsafe_yyjson_is_uint(val) ||
(unsafe_yyjson_is_sint(val) && !(ret >> 63))) {
*value = ret;
return true;
}
}
return false;
}

/**
Set provided `value` if the JSON Pointer (RFC 6901) exists and is type sint.
Returns true if value at `ptr` exists and is the correct type, otherwise false.
Set provided `value` if the JSON Pointer (RFC 6901) exists and is an integer
that fits in `int64_t`. Returns true if successful, otherwise false.
*/
yyjson_api_inline bool yyjson_ptr_get_sint(
yyjson_val *root, const char *ptr, int64_t *value) {
yyjson_val *val = yyjson_ptr_get(root, ptr);
if (value && yyjson_is_sint(val)) {
*value = unsafe_yyjson_get_sint(val);
return true;
} else {
return false;
if (value && val) {
int64_t ret = val->uni.i64;
if (unsafe_yyjson_is_sint(val) ||
(unsafe_yyjson_is_uint(val) && ret >= 0)) {
*value = ret;
return true;
}
}
return false;
}

/**
Expand Down

0 comments on commit fbd212a

Please sign in to comment.