Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement handling JSON_PRETTY_PRINT in json_encode() function #999

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions runtime/json-functions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -275,12 +275,25 @@ bool JsonEncoder::encode(double d) noexcept {
if (vk::any_of_equal(std::fpclassify(d), FP_INFINITE, FP_NAN)) {
php_warning("%s: strange double %lf in function json_encode", json_path_.to_string().c_str(), d);
if (options_ & JSON_PARTIAL_OUTPUT_ON_ERROR) {
static_SB.append("0", 1);
if (options_ & JSON_PRESERVE_ZERO_FRACTION) {
static_SB.append("0.0", 3);
} else {
static_SB.append("0", 1);
}
} else {
return false;
}
} else {
static_SB << (simple_encode_ ? f$number_format(d, 6, string{"."}, string{}) : string{d});
if (simple_encode_) {
static_SB << f$number_format(d, 6, string{"."}, string{});
} else {
static_SB << string{d};
if (options_ & JSON_PRESERVE_ZERO_FRACTION) {
if (double dummy = 0.0; std::modf(d, &dummy) == 0.0) {
static_SB.append(".0", 2);
}
}
}
}
return true;
}
Expand Down
6 changes: 3 additions & 3 deletions runtime/json-functions.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@

constexpr int64_t JSON_UNESCAPED_UNICODE = 1;
constexpr int64_t JSON_FORCE_OBJECT = 16;
constexpr int64_t JSON_PRETTY_PRINT = 128; // TODO: add actual support to untyped
constexpr int64_t JSON_PRETTY_PRINT = 128;
constexpr int64_t JSON_PARTIAL_OUTPUT_ON_ERROR = 512;
constexpr int64_t JSON_PRESERVE_ZERO_FRACTION = 1024;

constexpr int64_t JSON_AVAILABLE_OPTIONS = JSON_UNESCAPED_UNICODE | JSON_FORCE_OBJECT | JSON_PARTIAL_OUTPUT_ON_ERROR;
constexpr int64_t JSON_AVAILABLE_FLAGS_TYPED = JSON_PRETTY_PRINT | JSON_PRESERVE_ZERO_FRACTION;
constexpr int64_t JSON_AVAILABLE_OPTIONS = JSON_UNESCAPED_UNICODE | JSON_FORCE_OBJECT | JSON_PARTIAL_OUTPUT_ON_ERROR | JSON_PRESERVE_ZERO_FRACTION;
constexpr int64_t JSON_AVAILABLE_FLAGS_TYPED = JSON_PRETTY_PRINT;

struct JsonPath {
constexpr static int MAX_DEPTH = 8;
Expand Down
2 changes: 1 addition & 1 deletion runtime/json-writer.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class JsonWriter : vk::not_copyable {

array<NestedLevel> stack_;
string error_;
std::size_t double_precision_{0};
std::size_t double_precision_{0}; // unused
const bool pretty_print_{false};
const bool preserve_zero_fraction_{false};
bool has_root_{false};
Expand Down