Skip to content

Commit

Permalink
feat: print formatted Value
Browse files Browse the repository at this point in the history
  • Loading branch information
SHIINASAMA committed Dec 24, 2024
1 parent 6faad29 commit f6df7f8
Show file tree
Hide file tree
Showing 8 changed files with 221 additions and 97 deletions.
13 changes: 12 additions & 1 deletion sese/config/Json.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,10 @@ bool Json::streamifyObject(
auto &[object, count] = stack.top();
auto &object_ref = object->getDict();
stack.pop();
if (object_ref.empty()) {
CONST_WRITE(out, "{}");
return true;
}
if (count == object_ref.size()) {
CONST_WRITE(out, "}");
return true;
Expand Down Expand Up @@ -320,6 +324,7 @@ bool Json::streamifyObject(
}
if (count == object_ref.size()) {
CONST_WRITE(out, "}");
return true;
}
}
return true;
Expand All @@ -333,6 +338,10 @@ bool Json::streamifyArray(
auto &[array, count] = stack.top();
auto &array_ref = array->getList();
stack.pop();
if (array_ref.empty()) {
CONST_WRITE(out, "[]");
return true;
}
if (count == array_ref.size()) {
CONST_WRITE(out, "]");
return true;
Expand All @@ -355,12 +364,14 @@ bool Json::streamifyArray(
if (value->isList()) {
stack.emplace(array, count);
stack.emplace(value.get(), 0);
return true;
}
if (!streamifyBasic(out, value)) {
return false;
}
if (count == array_ref.size()) {
CONST_WRITE(out, "]");
return true;
}
}
return true;
Expand All @@ -377,7 +388,7 @@ bool Json::streamifyBasic(io::OutputStream *out, const std::shared_ptr<Value> &v
CONST_WRITE(out, "false");
}
} else if (value->isDouble() || value->isInt() || value->isString()) {
auto v = value->toString();
auto v = value->toStringBasic();
STRING_WRITE(out, v);
} else {
return false;
Expand Down
2 changes: 1 addition & 1 deletion sese/config/Yaml.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -471,7 +471,7 @@ bool Yaml::streamifyBasic(OutputStream *output, const std::shared_ptr<Value> &va
decltype(auto) data = value->getBool() ? "true" : "false";
CONST_WRITE(output, data);
} else if (value->isInt() || value->isDouble()) {
decltype(auto) data = value->toString();
decltype(auto) data = value->toStringBasic();
STRING_WRITE(output, data);
} else {
// value->isString()
Expand Down
4 changes: 3 additions & 1 deletion sese/test/Data/data.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
"boolean": true,
"object": {
"object_value1": "value1",
"object_value2": "value2"
"object_value2": "value2",
"null_array": [],
"null_object": {}
},
"mixin_array": [
1,
Expand Down
12 changes: 11 additions & 1 deletion sese/test/TestJson.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,20 @@ TEST(TestJson, Value) {
input->close();
ASSERT_FALSE(object.isNull());

auto content = object.toString(4);
auto content = object.toString();
puts(content.c_str());
}

TEST(TestJson, Streamify) {
auto input = sese::io::FileStream::create(PROJECT_PATH "/sese/test/Data/data.json", sese::io::FileStream::T_READ);
auto object = sese::Json::parse(input.get());
input->close();
ASSERT_FALSE(object.isNull());

sese::io::ConsoleOutputStream console_output_stream;
sese::Json::streamify(&console_output_stream, object);
}

/// Key-value pair splitting error
TEST(TestJson, Error_0) {
const char STR[] = "{\n"
Expand Down
5 changes: 3 additions & 2 deletions sese/test/TestValue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -234,13 +234,14 @@ TEST(TestValue, ToString) {
.append(false)
.append(INT64_C(114514))
.append("Blob", 4)
.append(Value::Dict()))
.append(Value::Dict())
.append(Value::List()))
.set("int", INT64_C(1919810))
.set("double", 3.14)
.set("dict", Value::Dict()
.set("string", "World")
.set("int", INT64_C(123456)))
);
// clang-format on
SESE_INFO("{2}", value);
SESE_INFO("\n{}", value);
}
10 changes: 5 additions & 5 deletions sese/test/TestYaml.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ constexpr char STR1[]{R"(
root:
mapping:
str 1: "Hello:-|str1"
str\'2: 'Hello str2'
str'2: 'Hello str2'
str\"3: "Hello\\\"str3"
str|4: 'Hello:-|\"str4'
str5: Hello str5
bool1: true
bool2: yes
"bool1": true
'bool2': yes
bool3: or
int1: 114514
float1: 3.14
Expand Down Expand Up @@ -83,7 +83,7 @@ TEST(TestYaml, Deserialize_0) {
auto &mapping_obj_dict = mapping_obj->getDict();

{
auto str2_obj = mapping_obj_dict.find("str\\'2");
auto str2_obj = mapping_obj_dict.find("str'2");
ASSERT_TRUE(str2_obj->isString());
EXPECT_EQ(str2_obj->getString(), "Hello str2");

Expand Down Expand Up @@ -259,7 +259,7 @@ TEST(TestYaml, Value) {
auto input = sese::io::InputBufferWrapper(STR1, sizeof(STR1) - 1);
const auto VALUE = sese::Yaml::parse(&input);
ASSERT_FALSE(VALUE.isNull());
const auto CONTENT = VALUE.toString(5);
const auto CONTENT = VALUE.toString();
puts(CONTENT.c_str());
}

Expand Down
Loading

0 comments on commit f6df7f8

Please sign in to comment.