Skip to content
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
6 changes: 6 additions & 0 deletions serde_json/rust/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,12 @@ impl SerdeJson {
self.serialize_with_formatter(sort_keys, SpacedFormatter)
}

/// Converts a [SerdeJson] to a pretty-printed, multi-line string with
/// two-space indentation.
pub fn to_string_pretty(&self, sort_keys: bool) -> RawString {
self.serialize_with_formatter(sort_keys, serde_json::ser::PrettyFormatter::new())
}

pub fn get_keys(&self) -> Result<VecRawString, RawString> {
let object = match self.value.as_object() {
Some(o) => o,
Expand Down
4 changes: 4 additions & 0 deletions serde_json/serde_json_bridge.cc
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,10 @@ std::string SerdeJson::ToStringSpaced(bool sort_keys) const {
return FromRustRawString(json_obj_.to_string_spaced(sort_keys));
}

std::string SerdeJson::ToStringPretty(bool sort_keys) const {
return FromRustRawString(json_obj_.to_string_pretty(sort_keys));
}

absl::StatusOr<std::vector<std::string>> SerdeJson::GetKeys() const {
rs_std::Result<rust::json::VecRawString,
rust::raw_string::RawString>
Expand Down
3 changes: 3 additions & 0 deletions serde_json/serde_json_bridge.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,9 @@ class SerdeJson final {
// Convert this object to string with spaces after colons and commas.
// Example: {"key": "value", "num": 42}
std::string ToStringSpaced(bool sort_keys = true) const;
// Convert this object to a pretty-printed, multi-line string with two-space
// indentation.
std::string ToStringPretty(bool sort_keys = true) const;
absl::StatusOr<::google::protobuf::Struct> ToProtoStruct() const;
absl::StatusOr<::google::protobuf::Value> ToProtoValue() const;

Expand Down
Loading