Skip to content

Commit da300d5

Browse files
committedMar 6, 2025
fix(marshaller): Improve JsonRPCStructureMarshaller template based on PR discussion
1 parent 33a015b commit da300d5

File tree

1 file changed

+82
-72
lines changed

1 file changed

+82
-72
lines changed
 

‎generator/ServiceClientGeneratorLib/Generators/Marshallers/JsonRPCStructureMarshaller.tt

+82-72
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,52 @@ namespace <#= this.Config.Namespace #>.Model.Internal.MarshallTransformat
158158
}
159159
else if(structure.IsMap)
160160
{
161+
ProcessMap(level, variableName, structure);
162+
}
163+
else if(structure.IsDocument)
164+
{
165+
#>
166+
<#=new string(' ', level * 4)#> Amazon.Runtime.Documents.Internal.Transform.DocumentMarshaller.Instance.Write(context.Writer, <#=variableName#>);
167+
<#+
168+
169+
}
170+
else if(structure.IsStructure)
171+
{
172+
#>
173+
<#=new string(' ', level * 4)#> context.Writer.WriteStartObject();
174+
175+
<#=new string(' ', level * 4)#> var marshaller = <#=structure.Name#>Marshaller.Instance;
176+
<#=new string(' ', level * 4)#> marshaller.Marshall(<#=variableName#>, context);
177+
178+
<#=new string(' ', level * 4)#> context.Writer.WriteEndObject();
179+
<#+
180+
}
181+
else if(structure.IsMemoryStream)
182+
{
183+
#>
184+
<#=new string(' ', level * 4)#> context.Writer.WriteStringValue(StringUtils.FromMemoryStream(<#=variableName#>));
185+
<#+
186+
}
187+
else
188+
{
189+
// Only use timestamp format based marshalling if format is not Unix epoch, which is the default for json/rest-json
190+
if(structure.IsTimeStamp && structure.GetTimestampFormat(MarshallLocation.Body) != TimestampFormat.UnixTimestamp)
191+
{
192+
#>
193+
<#=new string(' ', level * 4)#> context.Writer.WriteStringValue(<#=structure.PrimitiveMarshaller(MarshallLocation.Body)#>(<#=variableName#>));
194+
<#+
195+
}
196+
else
197+
{
198+
DetermineNormalJsonWriteMethod(structure, variableName, level);
199+
}
200+
201+
}
202+
}
203+
204+
protected void ProcessMap(int level, string variableName, Shape structure)
205+
{
206+
string flatVariableName = variableName.Replace(".", "");
161207
#>
162208
<#=new string(' ', level * 4)#> context.Writer.WriteStartObject();
163209
<#=new string(' ', level * 4)#> foreach (var <#=flatVariableName#>Kvp in <#=variableName#>)
@@ -177,7 +223,7 @@ namespace <#= this.Config.Namespace #>.Model.Internal.MarshallTransformat
177223
if (isNullableMap)
178224
{
179225
#>
180-
<#=new string(' ', level * 4)#> if(<#=flatVariableName#>Value == null)
226+
<#=new string(' ', level * 4)#> if (<#=flatVariableName#>Value == null)
181227
<#=new string(' ', level * 4)#> {
182228
<#=new string(' ', level * 4)#> context.Writer.WriteNullValue();
183229
<#=new string(' ', level * 4)#> }
@@ -216,6 +262,39 @@ namespace <#= this.Config.Namespace #>.Model.Internal.MarshallTransformat
216262
<#+
217263
}
218264
}
265+
else if(structure.ValueShape.IsTimeStamp)
266+
{
267+
if(isNullableMap)
268+
{
269+
if (structure.ValueShape.data[Shape.TimestampFormatKey] != null && !string.Equals(structure.ValueShape.data["timestampFormat"].ToString(), "unixTimestamp"))
270+
{
271+
#>
272+
<#=new string(' ', level * 4)#> context.Writer.WriteStringValue(<#=flatVariableName#>Value.Value);
273+
<#+
274+
}
275+
else
276+
{
277+
#>
278+
<#=new string(' ', level * 4)#> context.Writer.WriteNumberValue(Convert.ToInt64(StringUtils.FromDateTimeToUnixTimestamp(<#=flatVariableName#>Value.Value)));
279+
<#+
280+
}
281+
}
282+
else
283+
{
284+
if (structure.ValueShape.data[Shape.TimestampFormatKey] != null && !string.Equals(structure.ValueShape.data["timestampFormat"].ToString(), "unixTimestamp"))
285+
{
286+
#>
287+
<#=new string(' ', level * 4)#> context.Writer.WriteStringValue(<#=flatVariableName#>Value);
288+
<#+
289+
}
290+
else
291+
{
292+
#>
293+
<#=new string(' ', level * 4)#> context.Writer.WriteNumberValue(Convert.ToInt64(StringUtils.FromDateTimeToUnixTimestamp(<#=flatVariableName#>Value)));
294+
<#+
295+
}
296+
}
297+
}
219298
else
220299
{
221300
if(isNullableMap)
@@ -239,66 +318,8 @@ namespace <#= this.Config.Namespace #>.Model.Internal.MarshallTransformat
239318
<#=new string(' ', level * 4)#> }
240319
<#=new string(' ', level * 4)#> context.Writer.WriteEndObject();
241320
<#+
242-
}
243-
else if(structure.IsDocument)
244-
{
245-
#>
246-
<#=new string(' ', level * 4)#> Amazon.Runtime.Documents.Internal.Transform.DocumentMarshaller.Instance.Write(context.Writer, <#=variableName#>);
247-
<#+
248-
249-
}
250-
else if(structure.IsStructure)
251-
{
252-
#>
253-
<#=new string(' ', level * 4)#> context.Writer.WriteStartObject();
254-
255-
<#=new string(' ', level * 4)#> var marshaller = <#=structure.Name#>Marshaller.Instance;
256-
<#=new string(' ', level * 4)#> marshaller.Marshall(<#=variableName#>, context);
257-
258-
<#=new string(' ', level * 4)#> context.Writer.WriteEndObject();
259-
<#+
260-
}
261-
else if(structure.IsMemoryStream)
262-
{
263-
#>
264-
<#=new string(' ', level * 4)#> context.Writer.WriteStringValue(StringUtils.FromMemoryStream(<#=variableName#>));
265-
<#+
266-
}
267-
else
268-
{
269-
// Only use timestamp format based marshalling if format is not Unix epoch, which is the default for json/rest-json
270-
if(structure.IsTimeStamp && structure.GetTimestampFormat(MarshallLocation.Body) != TimestampFormat.UnixTimestamp)
271-
{
272-
#>
273-
<#=new string(' ', level * 4)#> context.Writer.WriteStringValue(<#=structure.PrimitiveMarshaller(MarshallLocation.Body)#>(<#=variableName#>));
274-
<#+
275-
}
276-
else
277-
{
278-
DetermineNormalJsonWriteMethod(structure, variableName, level);
279-
}
280-
281-
}
282-
}
283-
#>
284-
<#+
285-
protected void DetermineCustomMarshallerJsonWriteMethod(Member member, string memberProperty, int level)
286-
{
287-
if (String.Equals(member.CustomMarshallerTransformation,"Amazon.Util.AWSSDKUtils.ConvertToUnixEpochMilliseconds", StringComparison.OrdinalIgnoreCase))
288-
{
289-
#>
290-
<#=new string(' ', level * 4)#> context.Writer.WriteNumberValue(<#=member.CustomMarshallerTransformation#>(<#=memberProperty#>));
291-
<#+
292-
}
293-
else
294-
{
295-
#>
296-
<#=new string(' ', level * 4)#> context.Writer.WriteStringValue(<#=member.CustomMarshallerTransformation#>(<#=memberProperty#>));
297-
<#+
298-
}
299321
}
300-
#>
301-
<#+
322+
302323
/// https://smithy.io/2.0/aws/protocols/aws-restjson1-protocol.html#json-shape-serialization
303324
/// timestamps in json protocols use unixtimestamp if none is specified (by default)
304325
protected void DetermineNormalJsonWriteMethod(Shape shape, string memberProperty, int level)
@@ -329,21 +350,10 @@ namespace <#= this.Config.Namespace #>.Model.Internal.MarshallTransformat
329350
}
330351
else if (shape.IsInt || shape.IsLong || shape.IsFloat || shape.IsDouble)
331352
{
332-
// Handle integer enums with special care to support both nullable and non-nullable types
333-
if (memberProperty.EndsWith(".Value") && shape.IsInt)
334-
{
335-
// Already has .Value appended, use as is for nullable types
353+
// Simple numeric value
336354
#>
337355
<#=new string(' ', level * 4)#> context.Writer.WriteNumberValue(<#=memberProperty#>);
338356
<#+
339-
}
340-
else
341-
{
342-
// For non-nullable types, don't try to access .Value
343-
#>
344-
<#=new string(' ', level * 4)#> context.Writer.WriteNumberValue(<#=memberProperty#>);
345-
<#+
346-
}
347357
}
348358
else if (shape.IsBoolean)
349359
{

0 commit comments

Comments
 (0)
Failed to load comments.