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

Avoid creating unnecessary intermediate strings #3439

Merged
merged 7 commits into from
Aug 24, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -93,32 +93,35 @@ namespace <#=this.Config.Namespace #>.Model.Internal.MarshallTransformations
if (this.Operation.RequestHasBodyMembers || shouldMarshallPayload)
{
#>
using (StringWriter stringWriter = new StringWriter(CultureInfo.InvariantCulture))
{
JsonWriter writer = new JsonWriter(stringWriter);
writer.Validate = false;
using (MemoryStream memoryStream = new MemoryStream())
{
using (StreamWriter streamWriter = new InvariantCultureStreamWriter(memoryStream))
{
JsonWriter writer = new JsonWriter(streamWriter);
writer.Validate = false;
<#
if (shouldMarshallPayload)
{
#>
var context = new JsonMarshallerContext(request, writer);
var context = new JsonMarshallerContext(request, writer);
<#
ProcessStructure(0, "publicRequest." + payload.PropertyName, payload.Shape);
ProcessStructure(1, "publicRequest." + payload.PropertyName, payload.Shape);
}
else
{
#>
writer.WriteObjectStart();
var context = new JsonMarshallerContext(request, writer);
writer.WriteObjectStart();
var context = new JsonMarshallerContext(request, writer);
<#
ProcessMembers(1, "publicRequest", this.Operation.RequestBodyMembers);
ProcessMembers(2, "publicRequest", this.Operation.RequestBodyMembers);
#>
writer.WriteObjectEnd();
writer.WriteObjectEnd();
<#
}
#>
string snippet = stringWriter.ToString();
request.Content = System.Text.Encoding.UTF8.GetBytes(snippet);
}

request.Content = memoryStream.ToArray();
<#
GenerateRequestChecksumHandling(this.Operation, "snippet");
#>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*******************************************************************************
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* Licensed under the Apache License, Version 2.0 (the "License"). You may not use
* this file except in compliance with the License. A copy of the License is located at
*
* http://aws.amazon.com/apache2.0
*
* or in the "license" file accompanying this file.
* This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
* CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
* *****************************************************************************
* __ _ _ ___
* ( )( \/\/ )/ __)
* /__\ \ / \__ \
* (_)(_) \/\/ (___/
*
* AWS SDK for .NET
* API Version: 2006-03-01
*
*/

using System;
using System.Globalization;
using System.IO;

namespace Amazon.Runtime.Internal.Util
{
/// <summary>
/// A <see cref="StreamWriter"/> that uses the invariant culture for formatting.
/// </summary>
public class InvariantCultureStreamWriter : StreamWriter
{
/// <summary>
/// Initializes a new instance of the <see cref="InvariantCultureStreamWriter"/> class with the specified stream.
/// </summary>
/// <param name="stream">The stream to write to.</param>
public InvariantCultureStreamWriter(Stream stream)
: base(stream)
{
}

/// <inheritdoc/>
public override IFormatProvider FormatProvider
{
get
{
return CultureInfo.InvariantCulture;
}
}
}
}