Add example/jsonfindptrs -query=STR flag

This commit is contained in:
Nigel Tao committed 2020-08-12 23:18:37 +10:00
1 parent ab6f895007
commit 8aac6767bb
5 files changed
+638 -82

No files matched your search

+303 -33
View File
@@ -8604,16 +8604,28 @@ class DecodeJsonCallbacks {
IOBuffer& buffer);
};
extern const char DecodeJson_BadJsonPointer[];
extern const char DecodeJson_NoMatch[];
// DecodeJson calls callbacks based on the JSON-formatted data in input.
//
// On success, the returned error_message is empty and cursor_position counts
// the number of bytes consumed. On failure, error_message is non-empty and
// cursor_position is the location of the error. That error may be a content
// error (invalid JSON) or an input error (e.g. network failure).
//
// json_pointer is a query in the JSON Pointer (RFC 6901) syntax. The callbacks
// run for the input's sub-node that matches the query. DecodeJson_NoMatch is
// returned if no matching sub-node was found. The empty query matches the
// input's root node, consistent with JSON Pointer semantics.
//
// The JSON Pointer implementation is greedy: duplicate keys are not rejected
// but only the first match for each '/'-separated fragment is followed.
DecodeJsonResult DecodeJson(
DecodeJsonCallbacks&& callbacks,
sync_io::Input&& input,
wuffs_base__slice_u32 quirks = wuffs_base__empty_slice_u32());
wuffs_base__slice_u32 quirks = wuffs_base__empty_slice_u32(),
std::string json_pointer = std::string());
} // namespace wuffs_aux
@@ -29355,7 +29367,7 @@ DecodeJsonResult::DecodeJsonResult(std::string&& error_message0,
std::string //
DecodeJsonCallbacks::AppendByteString(std::string&& val) {
return "wuffs_aux::JsonDecoder: unexpected JSON byte string";
return "wuffs_aux::DecodeJson: unexpected JSON byte string";
}
void //
@@ -29363,6 +29375,10 @@ DecodeJsonCallbacks::Done(DecodeJsonResult& result,
sync_io::Input& input,
IOBuffer& buffer) {}
const char DecodeJson_BadJsonPointer[] =
"wuffs_aux::DecodeJson: bad JSON Pointer";
const char DecodeJson_NoMatch[] = "wuffs_aux::DecodeJson: no match";
#define WUFFS_AUX__DECODE_JSON__GET_THE_NEXT_TOKEN \
while (tok_buf.meta.ri >= tok_buf.meta.wi) { \
if (tok_status.repr == nullptr) { \
@@ -29374,17 +29390,17 @@ DecodeJsonCallbacks::Done(DecodeJsonResult& result,
goto done; \
} else if (cursor_index != io_buf->meta.ri) { \
ret_error_message = \
"wuffs_aux::JsonDecoder: internal error: bad cursor_index"; \
"wuffs_aux::DecodeJson: internal error: bad cursor_index"; \
goto done; \
} else if (io_buf->meta.closed) { \
ret_error_message = \
"wuffs_aux::JsonDecoder: internal error: io_buf is closed"; \
"wuffs_aux::DecodeJson: internal error: io_buf is closed"; \
goto done; \
} \
io_buf->compact(); \
if (io_buf->meta.wi >= io_buf->data.len) { \
ret_error_message = \
"wuffs_aux::JsonDecoder: internal error: io_buf is full"; \
"wuffs_aux::DecodeJson: internal error: io_buf is full"; \
goto done; \
} \
cursor_index = io_buf->meta.ri; \
@@ -29395,7 +29411,7 @@ DecodeJsonCallbacks::Done(DecodeJsonResult& result,
} \
if (WUFFS_JSON__DECODER_WORKBUF_LEN_MAX_INCL_WORST_CASE != 0) { \
ret_error_message = \
"wuffs_aux::JsonDecoder: internal error: bad WORKBUF_LEN"; \
"wuffs_aux::DecodeJson: internal error: bad WORKBUF_LEN"; \
goto done; \
} \
wuffs_base__slice_u8 work_buf = wuffs_base__empty_slice_u8(); \
@@ -29406,16 +29422,268 @@ DecodeJsonCallbacks::Done(DecodeJsonResult& result,
if ((io_buf->meta.ri < cursor_index) || \
((io_buf->meta.ri - cursor_index) < token_len)) { \
ret_error_message = \
"wuffs_aux::JsonDecoder: internal error: bad token indexes"; \
"wuffs_aux::DecodeJson: internal error: bad token indexes"; \
goto done; \
} \
uint8_t* token_ptr = io_buf->data.ptr + cursor_index; \
cursor_index += token_len
namespace {
// DecodeJson_SplitJsonPointer returns ("bar", 8) for ("/foo/bar/baz/qux", 5).
// It returns a 0 size_t when s has invalid JSON Pointer syntax.
std::pair<std::string, size_t> //
DecodeJson_SplitJsonPointer(std::string& s, size_t i) {
std::string fragment;
while (i < s.size()) {
char c = s[i];
if (c == '/') {
break;
} else if (c != '~') {
fragment.push_back(c);
i++;
continue;
}
i++;
if (i >= s.size()) {
return std::make_pair(std::string(), 0);
}
c = s[i];
if (c == '0') {
fragment.push_back('~');
i++;
continue;
} else if (c == '1') {
fragment.push_back('/');
i++;
continue;
}
return std::make_pair(std::string(), 0);
}
return std::make_pair(std::move(fragment), i);
}
std::string //
DecodeJson_DecodeBackslashX(std::string& str,
uint8_t* token_ptr,
size_t token_len) {
wuffs_base__slice_u8 encoded =
wuffs_base__make_slice_u8(token_ptr, token_len);
while (encoded.len > 0) {
uint8_t decoded[64];
constexpr bool src_closed = true;
wuffs_base__transform__output o = wuffs_base__base_16__decode4(
wuffs_base__make_slice_u8(&decoded[0], sizeof decoded), encoded,
src_closed, WUFFS_BASE__BASE_16__DEFAULT_OPTIONS);
if (o.status.is_error()) {
return o.status.message();
} else if ((o.num_dst > (sizeof decoded)) || (o.num_src > encoded.len)) {
return "wuffs_aux::DecodeJson: internal error: inconsistent base16 "
"decoding";
}
str.append( // Convert from (uint8_t*).
static_cast<const char*>(static_cast<void*>(&decoded[0])), o.num_dst);
encoded.ptr += o.num_src;
encoded.len -= o.num_src;
}
return "";
}
std::string //
DecodeJson_WalkJsonPointerFragment(wuffs_base__token_buffer& tok_buf,
wuffs_base__status& tok_status,
wuffs_json__decoder::unique_ptr& dec,
wuffs_base__io_buffer* io_buf,
std::string& io_error_message,
size_t& cursor_index,
sync_io::Input& input,
std::string& json_pointer_fragment) {
std::string ret_error_message;
while (true) {
WUFFS_AUX__DECODE_JSON__GET_THE_NEXT_TOKEN;
int64_t vbc = token.value_base_category();
uint64_t vbd = token.value_base_detail();
if (vbc == WUFFS_BASE__TOKEN__VBC__FILLER) {
continue;
} else if ((vbc != WUFFS_BASE__TOKEN__VBC__STRUCTURE) ||
!(vbd & WUFFS_BASE__TOKEN__VBD__STRUCTURE__PUSH)) {
return DecodeJson_NoMatch;
} else if (vbd & WUFFS_BASE__TOKEN__VBD__STRUCTURE__TO_LIST) {
goto do_list;
}
goto do_dict;
}
do_dict:
// Alternate between these two things:
// 1. Decode the next dict key (a string). If it matches the fragment, we're
// done (success). If we've reached the dict's end (VBD__STRUCTURE__POP)
// so that there was no next dict key, we're done (failure).
// 2. Otherwise, skip the next dict value.
while (true) {
for (std::string str; true;) {
WUFFS_AUX__DECODE_JSON__GET_THE_NEXT_TOKEN;
int64_t vbc = token.value_base_category();
uint64_t vbd = token.value_base_detail();
switch (vbc) {
case WUFFS_BASE__TOKEN__VBC__FILLER:
continue;
case WUFFS_BASE__TOKEN__VBC__STRUCTURE:
if (vbd & WUFFS_BASE__TOKEN__VBD__STRUCTURE__PUSH) {
goto fail;
}
return DecodeJson_NoMatch;
case WUFFS_BASE__TOKEN__VBC__STRING: {
if (vbd & WUFFS_BASE__TOKEN__VBD__STRING__CONVERT_0_DST_1_SRC_DROP) {
// No-op.
} else if (vbd &
WUFFS_BASE__TOKEN__VBD__STRING__CONVERT_1_DST_1_SRC_COPY) {
const char* ptr = // Convert from (uint8_t*).
static_cast<const char*>(static_cast<void*>(token_ptr));
str.append(ptr, token_len);
} else if (
vbd &
WUFFS_BASE__TOKEN__VBD__STRING__CONVERT_1_DST_4_SRC_BACKSLASH_X) {
ret_error_message =
DecodeJson_DecodeBackslashX(str, token_ptr, token_len);
if (!ret_error_message.empty()) {
goto done;
}
} else {
goto fail;
}
break;
}
case WUFFS_BASE__TOKEN__VBC__UNICODE_CODE_POINT: {
uint8_t u[WUFFS_BASE__UTF_8__BYTE_LENGTH__MAX_INCL];
size_t n = wuffs_base__utf_8__encode(
wuffs_base__make_slice_u8(
&u[0], WUFFS_BASE__UTF_8__BYTE_LENGTH__MAX_INCL),
static_cast<uint32_t>(vbd));
const char* ptr = // Convert from (uint8_t*).
static_cast<const char*>(static_cast<void*>(&u[0]));
str.append(ptr, n);
break;
}
default:
goto fail;
}
if (token.continued()) {
continue;
}
if (str == json_pointer_fragment) {
return "";
}
goto skip_the_next_dict_value;
}
skip_the_next_dict_value:
for (uint32_t skip_depth = 0; true;) {
WUFFS_AUX__DECODE_JSON__GET_THE_NEXT_TOKEN;
int64_t vbc = token.value_base_category();
uint64_t vbd = token.value_base_detail();
if (token.continued() || (vbc == WUFFS_BASE__TOKEN__VBC__FILLER)) {
continue;
} else if (vbc == WUFFS_BASE__TOKEN__VBC__STRUCTURE) {
if (vbd & WUFFS_BASE__TOKEN__VBD__STRUCTURE__PUSH) {
skip_depth++;
continue;
}
skip_depth--;
}
if (skip_depth == 0) {
break;
}
} // skip_the_next_dict_value
} // do_dict
do_list:
do {
wuffs_base__result_u64 result_u64 = wuffs_base__parse_number_u64(
wuffs_base__make_slice_u8(
static_cast<uint8_t*>(static_cast<void*>(
const_cast<char*>(json_pointer_fragment.data()))),
json_pointer_fragment.size()),
WUFFS_BASE__PARSE_NUMBER_XXX__DEFAULT_OPTIONS);
if (!result_u64.status.is_ok()) {
return DecodeJson_NoMatch;
}
uint64_t remaining = result_u64.value;
if (remaining == 0) {
goto check_that_a_value_follows;
}
for (uint32_t skip_depth = 0; true;) {
WUFFS_AUX__DECODE_JSON__GET_THE_NEXT_TOKEN;
int64_t vbc = token.value_base_category();
uint64_t vbd = token.value_base_detail();
if (token.continued() || (vbc == WUFFS_BASE__TOKEN__VBC__FILLER)) {
continue;
} else if (vbc == WUFFS_BASE__TOKEN__VBC__STRUCTURE) {
if (vbd & WUFFS_BASE__TOKEN__VBD__STRUCTURE__PUSH) {
skip_depth++;
continue;
}
if (skip_depth == 0) {
return DecodeJson_NoMatch;
}
skip_depth--;
}
if (skip_depth > 0) {
continue;
}
remaining--;
if (remaining == 0) {
goto check_that_a_value_follows;
}
}
} while (false); // do_list
check_that_a_value_follows:
while (true) {
WUFFS_AUX__DECODE_JSON__GET_THE_NEXT_TOKEN;
int64_t vbc = token.value_base_category();
uint64_t vbd = token.value_base_detail();
if (vbc == WUFFS_BASE__TOKEN__VBC__FILLER) {
continue;
}
// Undo the last part of WUFFS_AUX__DECODE_JSON__GET_THE_NEXT_TOKEN, so that
// we're only peeking at the next token.
tok_buf.meta.ri--;
cursor_index -= token_len;
if ((vbc == WUFFS_BASE__TOKEN__VBC__STRUCTURE) &&
(vbd & WUFFS_BASE__TOKEN__VBD__STRUCTURE__POP)) {
return DecodeJson_NoMatch;
}
return "";
} // check_that_a_value_follows
fail:
return "wuffs_aux::DecodeJson: internal error: unexpected token";
done:
return ret_error_message;
}
} // namespace
DecodeJsonResult //
DecodeJson(DecodeJsonCallbacks&& callbacks,
sync_io::Input&& input,
wuffs_base__slice_u32 quirks) {
wuffs_base__slice_u32 quirks,
std::string json_pointer) {
// Prepare the wuffs_base__io_buffer and the resultant error_message.
wuffs_base__io_buffer* io_buf = input.BringsItsOwnIOBuffer();
wuffs_base__io_buffer fallback_io_buf = wuffs_base__empty_io_buffer();
@@ -29433,7 +29701,7 @@ DecodeJson(DecodeJsonCallbacks&& callbacks,
// Prepare the low-level JSON decoder.
wuffs_json__decoder::unique_ptr dec = wuffs_json__decoder::alloc();
if (!dec) {
ret_error_message = "wuffs_aux::JsonDecoder: out of memory";
ret_error_message = "wuffs_aux::DecodeJson: out of memory";
goto done;
}
for (size_t i = 0; i < quirks.len; i++) {
@@ -29451,6 +29719,27 @@ DecodeJson(DecodeJsonCallbacks&& callbacks,
uint32_t depth = 0;
std::string str;
// Walk the (optional) JSON Pointer.
for (size_t i = 0; i < json_pointer.size();) {
if (json_pointer[i] != '/') {
ret_error_message = DecodeJson_BadJsonPointer;
goto done;
}
std::pair<std::string, size_t> split =
DecodeJson_SplitJsonPointer(json_pointer, i + 1);
i = std::move(split.second);
if (i == 0) {
ret_error_message = DecodeJson_BadJsonPointer;
goto done;
}
ret_error_message = DecodeJson_WalkJsonPointerFragment(
tok_buf, tok_status, dec, io_buf, io_error_message, cursor_index,
input, split.first);
if (!ret_error_message.empty()) {
goto done;
}
}
// Loop, doing these two things:
// 1. Get the next token.
// 2. Process that token.
@@ -29488,29 +29777,10 @@ DecodeJson(DecodeJsonCallbacks&& callbacks,
} else if (
vbd &
WUFFS_BASE__TOKEN__VBD__STRING__CONVERT_1_DST_4_SRC_BACKSLASH_X) {
wuffs_base__slice_u8 encoded =
wuffs_base__make_slice_u8(token_ptr, token_len);
while (encoded.len > 0) {
uint8_t decoded[64];
constexpr bool src_closed = true;
wuffs_base__transform__output o = wuffs_base__base_16__decode4(
wuffs_base__make_slice_u8(&decoded[0], sizeof decoded),
encoded, src_closed, WUFFS_BASE__BASE_16__DEFAULT_OPTIONS);
if (o.status.is_error()) {
ret_error_message = o.status.message();
goto done;
} else if ((o.num_dst > (sizeof decoded)) ||
(o.num_src > encoded.len)) {
ret_error_message =
"wuffs_aux::JsonDecoder: internal error: inconsistent "
"base16 decoding";
goto done;
}
str.append( // Convert from (uint8_t*).
static_cast<const char*>(static_cast<void*>(&decoded[0])),
o.num_dst);
encoded.ptr += o.num_src;
encoded.len -= o.num_src;
ret_error_message =
DecodeJson_DecodeBackslashX(str, token_ptr, token_len);
if (!ret_error_message.empty()) {
goto done;
}
} else {
goto fail;
@@ -29597,7 +29867,7 @@ DecodeJson(DecodeJsonCallbacks&& callbacks,
fail:
ret_error_message =
"wuffs_aux::JsonDecoder: internal error: unexpected token";
"wuffs_aux::DecodeJson: internal error: unexpected token";
goto done;
parsed_a_value: