#@ template language="C#" inherits="BaseRequestMarshaller" #>
<#@ assembly name="System.Core" #>
<#@ import namespace="System.Linq" #>
<#@ import namespace="System.Text" #>
<#@ import namespace="System.Collections.Generic" #>
<#
AddLicenseHeader();
AddCommonUsingStatements();
#>
using ThirdParty.Json.LitJson;
namespace <#= this.Config.Namespace #>.Model.Internal.MarshallTransformations
{
///
/// <#=this.Structure.Name #> Marshaller
///
public class <#=this.Structure.Name #>Marshaller : IRequestMarshaller<<#=this.Structure.Name #>, JsonMarshallerContext>
{
///
/// Unmarshaller the response from the service to the response class.
///
///
///
///
public void Marshall(<#=this.Structure.Name #> requestObject, JsonMarshallerContext context)
{
<#
ProcessMembers(0, "requestObject", this.Structure.Members);
#>
}
///
/// Singleton Marshaller.
///
public readonly static <#=this.Structure.Name #>Marshaller Instance = new <#=this.Structure.Name #>Marshaller();
}
}
<#+
protected void ProcessMembers(int level, string variableName, IEnumerable members)
{
foreach(var member in members)
{
#>
<#=new string(' ', level * 4)#> if(<#=variableName#>.IsSet<#=member.PropertyName#>())
<#=new string(' ', level * 4)#> {
<#=new string(' ', level * 4)#> context.Writer.WritePropertyName("<#=member.MarshallName#>");
<#+
string memberProperty = variableName + "." + member.PropertyName + (member.UseNullable ? ".Value" : string.Empty);
if(member.IsStructure || member.IsList || member.IsMap)
{
this.ProcessStructure(level, variableName + "." + member.PropertyName, member.Shape);
}
else if(!string.IsNullOrEmpty(member.CustomMarshallerTransformation))
{
#>
<#=new string(' ', level * 4)#> context.Writer.Write(<#=member.CustomMarshallerTransformation#>(<#=memberProperty#>));
<#+
}
else if(member.IsMemoryStream)
{
#>
<#=new string(' ', level * 4)#> context.Writer.Write(StringUtils.FromMemoryStream(<#=variableName + "." + member.PropertyName#>));
<#+
}
else
{
// Only use timestamp format based marshalling if format is not Unix epoch, which is the default for json/rest-json
if(member.IsDateTime && member.TimestampFormat != TimestampFormat.UnixTimestamp)
{
#>
<#=new string(' ', level * 4)#> context.Writer.Write(<#=member.PrimitiveMarshaller#>(<#=variableName + "." + member.PropertyName#>));
<#+
}
else
{
#>
<#=new string(' ', level * 4)#> context.Writer.Write(<#=memberProperty#>);
<#+
}
}
#>
<#=new string(' ', level * 4)#> }
<#+
if(member.IsIdempotent)
{
#>
<#=new string(' ', level * 4)#> else if(!(<#=variableName#>.IsSet<#=member.PropertyName#>()))
<#=new string(' ', level * 4)#> {
<#=new string(' ', level * 4)#> context.Writer.WritePropertyName("<#=member.MarshallName#>");
<#=new string(' ', level * 4)#> context.Writer.Write(Guid.NewGuid().ToString());
<#=new string(' ', level * 4)#> }
<#+
}
}
}
protected void ProcessRequestPayloadMember(int level, string variableName, Shape structure)
{
#>
<#=new string(' ', level * 4)#> var marshaller = <#=structure.Name#>Marshaller.Instance;
<#=new string(' ', level * 4)#> marshaller.Marshall(<#=variableName#>, context);
<#+
}
protected void ProcessStructure(int level, string variableName, Shape structure)
{
string flatVariableName = variableName.Replace(".", "");
if(structure.IsList)
{
#>
<#=new string(' ', level * 4)#> context.Writer.WriteArrayStart();
<#=new string(' ', level * 4)#> foreach(var <#=flatVariableName#>ListValue in <#=variableName#>)
<#=new string(' ', level * 4)#> {
<#+
ProcessStructure(level + 1, flatVariableName + "ListValue", structure.ListShape);
#>
<#=new string(' ', level * 4)#> }
<#=new string(' ', level * 4)#> context.Writer.WriteArrayEnd();
<#+
}
else if(structure.IsMap)
{
#>
<#=new string(' ', level * 4)#> context.Writer.WriteObjectStart();
<#=new string(' ', level * 4)#> foreach (var <#=flatVariableName#>Kvp in <#=variableName#>)
<#=new string(' ', level * 4)#> {
<#=new string(' ', level * 4)#> context.Writer.WritePropertyName(<#=flatVariableName#>Kvp.Key);
<#=new string(' ', level * 4)#> var <#=flatVariableName#>Value = <#=flatVariableName#>Kvp.Value;
<#+
ProcessStructure(level + 1, flatVariableName + "Value", structure.ValueShape);
#>
<#=new string(' ', level * 4)#> }
<#=new string(' ', level * 4)#> context.Writer.WriteObjectEnd();
<#+
}
else if(structure.IsDocument)
{
#>
<#=new string(' ', level * 4)#> Amazon.Runtime.Documents.Internal.Transform.DocumentMarshaller.Instance.Write(context.Writer, <#=variableName#>);
<#+
}
else if(structure.IsStructure)
{
#>
<#=new string(' ', level * 4)#> context.Writer.WriteObjectStart();
<#=new string(' ', level * 4)#> var marshaller = <#=structure.Name#>Marshaller.Instance;
<#=new string(' ', level * 4)#> marshaller.Marshall(<#=variableName#>, context);
<#=new string(' ', level * 4)#> context.Writer.WriteObjectEnd();
<#+
}
else if(structure.IsMemoryStream)
{
#>
<#=new string(' ', level * 4)#> context.Writer.Write(StringUtils.FromMemoryStream(<#=variableName#>));
<#+
}
else
{
// Only use timestamp format based marshalling if format is not Unix epoch, which is the default for json/rest-json
if(structure.IsDateTime && structure.GetTimestampFormat(MarshallLocation.Body) != TimestampFormat.UnixTimestamp)
{
#>
<#=new string(' ', level * 4)#> context.Writer.Write(<#=structure.PrimitiveMarshaller(MarshallLocation.Body)#>(<#=variableName#>));
<#+
}
else
{
#>
<#=new string(' ', level * 4)#> context.Writer.Write(<#=variableName#>);
<#+
}
}
}
#>