1+ import { determineTimestampFormat } from "@smithy/core/protocols" ;
12import { NormalizedSchema , SCHEMA } from "@smithy/core/schema" ;
23import { dateToUtcString , generateIdempotencyToken , LazyJsonString } from "@smithy/core/serde" ;
34import { Schema , ShapeSerializer } from "@smithy/types" ;
5+ import { toBase64 } from "@smithy/util-base64" ;
46
57import { SerdeContextConfig } from "../ConfigurableSerdeContext" ;
68import { JsonSettings } from "./JsonCodec" ;
@@ -22,12 +24,22 @@ export class JsonShapeSerializer extends SerdeContextConfig implements ShapeSeri
2224 this . buffer = this . _write ( this . rootSchema , value ) ;
2325 }
2426
27+ public writeDiscriminatedDocument ( schema : Schema , value : unknown ) : void {
28+ this . write ( schema , value ) ;
29+ if ( typeof this . buffer === "object" ) {
30+ this . buffer . __type = NormalizedSchema . of ( schema ) . getName ( true ) ;
31+ }
32+ }
33+
2534 public flush ( ) : string {
26- if ( this . rootSchema ?. isStructSchema ( ) || this . rootSchema ?. isDocumentSchema ( ) ) {
35+ const { rootSchema } = this ;
36+ this . rootSchema = undefined ;
37+
38+ if ( rootSchema ?. isStructSchema ( ) || rootSchema ?. isDocumentSchema ( ) ) {
2739 const replacer = new JsonReplacer ( ) ;
2840 return replacer . replaceInJson ( JSON . stringify ( this . buffer , replacer . createReplacer ( ) , 0 ) ) ;
2941 }
30- return this . buffer ;
42+ return JSON . stringify ( this . buffer ) ;
3143 }
3244
3345 private _write ( schema : Schema , value : unknown , container ?: NormalizedSchema ) : any {
@@ -73,23 +85,21 @@ export class JsonShapeSerializer extends SerdeContextConfig implements ShapeSeri
7385 return void 0 ;
7486 }
7587
76- if ( ns . isBlobSchema ( ) && ( value instanceof Uint8Array || typeof value === "string" ) ) {
88+ if (
89+ ( ns . isBlobSchema ( ) && ( value instanceof Uint8Array || typeof value === "string" ) ) ||
90+ ( ns . isDocumentSchema ( ) && value instanceof Uint8Array )
91+ ) {
7792 if ( ns === this . rootSchema ) {
7893 return value ;
7994 }
8095 if ( ! this . serdeContext ?. base64Encoder ) {
81- throw new Error ( "Missing base64Encoder in serdeContext" ) ;
96+ return toBase64 ( value ) ;
8297 }
8398 return this . serdeContext ?. base64Encoder ( value ) ;
8499 }
85100
86- if ( ns . isTimestampSchema ( ) && value instanceof Date ) {
87- const options = this . settings . timestampFormat ;
88- const format = options . useTrait
89- ? ns . getSchema ( ) === SCHEMA . TIMESTAMP_DEFAULT
90- ? options . default
91- : ns . getSchema ( ) ?? options . default
92- : options . default ;
101+ if ( ( ns . isTimestampSchema ( ) || ns . isDocumentSchema ( ) ) && value instanceof Date ) {
102+ const format = determineTimestampFormat ( ns , this . settings ) ;
93103 switch ( format ) {
94104 case SCHEMA . TIMESTAMP_DATE_TIME :
95105 return value . toISOString ( ) . replace ( ".000Z" , "Z" ) ;
@@ -124,6 +134,10 @@ export class JsonShapeSerializer extends SerdeContextConfig implements ShapeSeri
124134 }
125135 }
126136
137+ if ( ns . isDocumentSchema ( ) && isObject ) {
138+ return structuredClone ( value ) ;
139+ }
140+
127141 return value ;
128142 }
129143}
0 commit comments