Skip to content

Commit

Permalink
Fix consult client JSON stringifying. (#1658)
Browse files Browse the repository at this point in the history
  • Loading branch information
Barenboim authored Nov 20, 2024
1 parent 736c318 commit c92f95a
Showing 1 changed file with 57 additions and 50 deletions.
107 changes: 57 additions & 50 deletions src/client/WFConsulClient.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
Authors: Wang Zhenpeng ([email protected])
*/

#include <stdio.h>
#include <string.h>
#include <string>
#include <vector>
Expand Down Expand Up @@ -1116,6 +1117,60 @@ static bool parse_discover_result(const json_value_t *root,
return true;
}

static void print_json_string(const char *str, std::string& json_str)
{
json_str += "\"";
while (*str)
{
switch (*str)
{
case '\r':
json_str += "\\r";
break;
case '\n':
json_str += "\\n";
break;
case '\f':
json_str += "\\f";
break;
case '\b':
json_str += "\\b";
break;
case '\"':
json_str += "\\\"";
break;
case '\t':
json_str += "\\t";
break;
case '\\':
json_str += "\\\\";
break;
default:
if ((unsigned char)*str < 0x20)
{
char buf[8];
sprintf(buf, "\\u00%02x", *str);
json_str += buf;
}
else
json_str += *str;
break;
}
str++;
}
json_str += "\"";
}

static void print_json_number(double number, std::string& json_str)
{
long long integer = number;

if (integer == number)
json_str += std::to_string(integer);
else
json_str += std::to_string(number);
}

static void print_json_object(const json_object_t *obj, int depth,
std::string& json_str)
{
Expand All @@ -1134,9 +1189,8 @@ static void print_json_object(const json_object_t *obj, int depth,
for (i = 0; i < depth + 1; i++)
json_str += " ";

json_str += "\"";
json_str += name;
json_str += "\": ";
print_json_string(name, json_str);
json_str += ": ";
print_json_value(val, depth + 1, json_str);
}

Expand Down Expand Up @@ -1174,53 +1228,6 @@ static void print_json_array(const json_array_t *arr, int depth,
json_str += "]";
}

static void print_json_string(const char *str, std::string& json_str)
{
json_str += "\"";
while (*str)
{
switch (*str)
{
case '\r':
json_str += "\\r";
break;
case '\n':
json_str += "\\n";
break;
case '\f':
json_str += "\\f";
break;
case '\b':
json_str += "\\b";
break;
case '\"':
json_str += "\\\"";
break;
case '\t':
json_str += "\\t";
break;
case '\\':
json_str += "\\\\";
break;
default:
json_str += *str;
break;
}
str++;
}
json_str += "\"";
}

static void print_json_number(double number, std::string& json_str)
{
long long integer = number;

if (integer == number)
json_str += std::to_string(integer);
else
json_str += std::to_string(number);
}

static void print_json_value(const json_value_t *val, int depth,
std::string& json_str)
{
Expand Down

0 comments on commit c92f95a

Please sign in to comment.