Skip to content

Commit

Permalink
Refactor JSON serialization to use MemoryStream
Browse files Browse the repository at this point in the history
Replaced StringWriter with MemoryStream and StreamWriter for JSON serialization to improve efficiency. JsonWriter is now initialized with StreamWriter. Adjusted indentation levels in ProcessStructure and ProcessMembers methods. Final request content is now obtained as a byte array from MemoryStream.
  • Loading branch information
paulomorgado committed Aug 13, 2024
1 parent c4e2519 commit d4bf134
Showing 1 changed file with 15 additions and 12 deletions.
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 (SreamWriter sreamWriter = new SreamWriter(memoryStream, null, -1, true))
{
JsonWriter writer = new JsonWriter(sreamWriter);
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

0 comments on commit d4bf134

Please sign in to comment.