diff --git a/serde_json/rust/json.rs b/serde_json/rust/json.rs index 4cd16b7..e3c55e1 100644 --- a/serde_json/rust/json.rs +++ b/serde_json/rust/json.rs @@ -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 { let object = match self.value.as_object() { Some(o) => o, diff --git a/serde_json/serde_json_bridge.cc b/serde_json/serde_json_bridge.cc index 4ac1862..7af8d02 100644 --- a/serde_json/serde_json_bridge.cc +++ b/serde_json/serde_json_bridge.cc @@ -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> SerdeJson::GetKeys() const { rs_std::Result diff --git a/serde_json/serde_json_bridge.h b/serde_json/serde_json_bridge.h index 1a8ca39..3fdd177 100644 --- a/serde_json/serde_json_bridge.h +++ b/serde_json/serde_json_bridge.h @@ -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;