diff --git a/src/aws-cpp-sdk-core/include/aws/core/client/ClientConfiguration.h b/src/aws-cpp-sdk-core/include/aws/core/client/ClientConfiguration.h index 9799edfa69a..77abb91d926 100644 --- a/src/aws-cpp-sdk-core/include/aws/core/client/ClientConfiguration.h +++ b/src/aws-cpp-sdk-core/include/aws/core/client/ClientConfiguration.h @@ -398,6 +398,12 @@ namespace Aws */ bool enableHttpClientTrace = false; + /** + * Serialize XML request payloads without pretty-printing whitespace (indentation and newlines), + * producing a smaller payload. Defaults to false to preserve existing serialization behavior. + */ + bool compactXmlSerialization = false; + /** * profileName in config file that will be used by this object to resolve more configurations. */ diff --git a/src/aws-cpp-sdk-core/include/aws/core/utils/xml/XmlSerializer.h b/src/aws-cpp-sdk-core/include/aws/core/utils/xml/XmlSerializer.h index 8d410479976..0dd2739820d 100644 --- a/src/aws-cpp-sdk-core/include/aws/core/utils/xml/XmlSerializer.h +++ b/src/aws-cpp-sdk-core/include/aws/core/utils/xml/XmlSerializer.h @@ -175,6 +175,11 @@ namespace Aws * Convert entire document to string. Use this if you for example, want to save the document to a file. */ Aws::String ConvertToString() const; + /** + * Convert entire document to string. When compact is true, the output omits pretty-printing + * whitespace (indentation and newlines), producing a smaller payload. + */ + Aws::String ConvertToString(bool compact) const; /** * Returns true if the call to CreateFromXml* was successful, otherwise false. * if this returns false, you can call GetErrorMessage() to see details. diff --git a/src/aws-cpp-sdk-core/source/utils/xml/XmlSerializer.cpp b/src/aws-cpp-sdk-core/source/utils/xml/XmlSerializer.cpp index 3b2441f0dc3..cc30e221be0 100644 --- a/src/aws-cpp-sdk-core/source/utils/xml/XmlSerializer.cpp +++ b/src/aws-cpp-sdk-core/source/utils/xml/XmlSerializer.cpp @@ -274,10 +274,15 @@ Aws::String XmlDocument::GetErrorMessage() const } Aws::String XmlDocument::ConvertToString() const +{ + return ConvertToString(false); +} + +Aws::String XmlDocument::ConvertToString(bool compact) const { if (!m_doc) return ""; - Aws::External::tinyxml2::XMLPrinter printer; + Aws::External::tinyxml2::XMLPrinter printer(nullptr, compact); printer.PushHeader(false, true); m_doc->Accept(&printer); diff --git a/tests/aws-cpp-sdk-core-tests/utils/xml/XmlSerializerTest.cpp b/tests/aws-cpp-sdk-core-tests/utils/xml/XmlSerializerTest.cpp index a8f80783811..1a37d34078e 100644 --- a/tests/aws-cpp-sdk-core-tests/utils/xml/XmlSerializerTest.cpp +++ b/tests/aws-cpp-sdk-core-tests/utils/xml/XmlSerializerTest.cpp @@ -77,6 +77,31 @@ TEST_F(XmlSerializerTest, TestXmlSerialize) ASSERT_TRUE(doc.GetErrorMessage().empty()); } +TEST_F(XmlSerializerTest, TestXmlSerializeCompact) +{ + XmlDocument doc = XmlDocument::CreateWithRootNode("ToDo"); + XmlNode rootElement = doc.GetRootElement(); + XmlNode item1 = rootElement.CreateChildElement("Item"); + item1.SetAttributeValue("priority", "1"); + item1.SetText("Go to the Toy store!"); + XmlNode item2 = rootElement.CreateChildElement("Item"); + item2.SetAttributeValue("priority", "2"); + item2.SetText("Do bills"); + + Aws::String serializedXml = doc.ConvertToString(true); + + Aws::String toCompare = "" + "" + "Go to the <bold>Toy store!</bold>" + "Do bills" + ""; + ASSERT_EQ(toCompare, serializedXml); + ASSERT_TRUE(doc.GetErrorMessage().empty()); + + // The no-arg overload preserves the pretty-printed default. + ASSERT_EQ(doc.ConvertToString(false), doc.ConvertToString()); +} + TEST_F(XmlSerializerTest, TestXmlSerializeNewlines) { XmlDocument doc = XmlDocument::CreateWithRootNode("Newlines");