Skip to content

Commit 810c4c3

Browse files
author
ochafik
committed
json-partial: add comments
1 parent 8886c24 commit 810c4c3

File tree

3 files changed

+29
-2
lines changed

3 files changed

+29
-2
lines changed

common/json-partial.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,11 +113,10 @@ bool common_json_parse(
113113
auto start = it;
114114
json::sax_parse(it, end, &err_loc);
115115

116-
// std::string::const_iterator temptative_end;
117116
if (err_loc.found_error) {
118117
it = start;
119118
auto temptative_end = it + err_loc.position;
120-
// fprintf(stderr, "Error at position %zu (is_end = %s): %s\n", err_loc.position, temptative_end == end ? "true" : "false", err_loc.exception_message.c_str());
119+
// LOG_DBG("Error at position %zu (is_end = %s): %s\n", err_loc.position, temptative_end == end ? "true" : "false", err_loc.exception_message.c_str());
121120

122121
auto input = std::string(it, temptative_end);
123122
try {

common/json-partial.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,35 @@
11
#pragma once
22
#include <json.hpp>
33

4+
// Healing marker (empty if the JSON was fully parsed / wasn't healed).
45
struct common_healing_marker {
6+
// Raw marker.
57
std::string marker;
8+
9+
// Cutting the `common_json.json.dump()` string at the (only) occurrence of this marker should yield the original partial JSON string (modulo spaces / if it had the same dump format).
610
std::string json_dump_marker;
711
};
812

13+
// Represents a parsed JSON object, with its optional healing marker (a JSON dump fragment that can be used to find the position of healing in the JSON dump string)
914
struct common_json {
1015
nlohmann::ordered_json json;
16+
1117
common_healing_marker healing_marker;
1218
};
1319

20+
// Parse the JSON string, healing (closing) any partial JSON if `healing_marker` is not empty.
21+
//
22+
// Healing completes partial JSON strings by adding a (possibly modified) healing marker, then whatever is needed to close the JSON.
23+
// This allows to parse the resulting healed JSON string, yet be able to cut it again if needed at the healing marker.
24+
// (this is used when parsing JSON outputs from the models, then crafting partial JSONs for the partial tool calls in OAI format).
25+
//
26+
// For instance, parsing `{` with a healing marker `foo` will produce a healed JSON `{"foo":1}`, w/ json_dump_marker = `"foo"` (which can be used to break the JSON again).
1427
bool common_json_parse(
1528
const std::string & input,
1629
const std::string & healing_marker,
1730
common_json & out);
1831

32+
// Parse the JSON string (see overload above), but advancing an iterator to the end of the input when the (potentially partial) parsing succeeds.
1933
bool common_json_parse(
2034
std::string::const_iterator & it,
2135
const std::string::const_iterator & end,

tests/test-json-partial.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,20 @@ static void test_json_healing() {
9393
R"({"a":"$foo"})",
9494
R"("$foo)"
9595
);
96+
test(
97+
{
98+
R"({)",
99+
},
100+
R"({"$foo":1})",
101+
R"("$foo)"
102+
);
103+
test(
104+
{
105+
R"([)",
106+
},
107+
R"(["$foo"])",
108+
R"("$foo)"
109+
);
96110
// Healing right after a full literal
97111
test(
98112
{

0 commit comments

Comments
 (0)