Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { NormalizedSchema, SCHEMA } from "@smithy/core/schema";
import { determineTimestampFormat } from "@smithy/core/protocols";
import { NormalizedSchema, SCHEMA, TypeRegistry } from "@smithy/core/schema";
import {
LazyJsonString,
NumericValue,
Expand Down Expand Up @@ -84,13 +85,8 @@ export class JsonShapeDeserializer extends SerdeContextConfig implements ShapeDe
}
}

if (ns.isTimestampSchema()) {
const options = this.settings.timestampFormat;
const format = options.useTrait
? ns.getSchema() === SCHEMA.TIMESTAMP_DEFAULT
? options.default
: ns.getSchema() ?? options.default
: options.default;
if (ns.isTimestampSchema() && value != null) {
const format = determineTimestampFormat(ns, this.settings);
switch (format) {
case SCHEMA.TIMESTAMP_DATE_TIME:
return parseRfc3339DateTimeWithOffset(value);
Expand All @@ -112,6 +108,10 @@ export class JsonShapeDeserializer extends SerdeContextConfig implements ShapeDe
if (value instanceof NumericValue) {
return value;
}
const untyped = value as any;
if (untyped.type === "bigDecimal" && "string" in untyped) {
return new NumericValue(untyped.string, untyped.type);
}
return new NumericValue(String(value), "bigDecimal");
}

Expand All @@ -126,6 +126,22 @@ export class JsonShapeDeserializer extends SerdeContextConfig implements ShapeDe
}
}

if (ns.isDocumentSchema()) {
if (isObject) {
const out = Array.isArray(value) ? [] : ({} as any);
for (const [k, v] of Object.entries(value)) {
if (v instanceof NumericValue) {
out[k] = v;
} else {
out[k] = this._read(ns, v);
}
}
return out;
} else {
return structuredClone(value);
}
}

// covers string, numeric, boolean, document, bigDecimal
return value;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { determineTimestampFormat } from "@smithy/core/protocols";
import { NormalizedSchema, SCHEMA } from "@smithy/core/schema";
import { dateToUtcString, generateIdempotencyToken, LazyJsonString } from "@smithy/core/serde";
import { dateToUtcString, generateIdempotencyToken, LazyJsonString, NumericValue } from "@smithy/core/serde";
import { Schema, ShapeSerializer } from "@smithy/types";
import { toBase64 } from "@smithy/util-base64";

import { SerdeContextConfig } from "../ConfigurableSerdeContext";
import { JsonSettings } from "./JsonCodec";
Expand All @@ -22,11 +24,25 @@ export class JsonShapeSerializer extends SerdeContextConfig implements ShapeSeri
this.buffer = this._write(this.rootSchema, value);
}

/**
* @internal
*/
public writeDiscriminatedDocument(schema: Schema, value: unknown): void {
this.write(schema, value);
if (typeof this.buffer === "object") {
this.buffer.__type = NormalizedSchema.of(schema).getName(true);
}
}

public flush(): string {
if (this.rootSchema?.isStructSchema() || this.rootSchema?.isDocumentSchema()) {
const { rootSchema } = this;
this.rootSchema = undefined;

if (rootSchema?.isStructSchema() || rootSchema?.isDocumentSchema()) {
const replacer = new JsonReplacer();
return replacer.replaceInJson(JSON.stringify(this.buffer, replacer.createReplacer(), 0));
}
// non-struct root schema indicates a blob (base64 string) or plain string payload.
return this.buffer;
}

Expand Down Expand Up @@ -73,23 +89,21 @@ export class JsonShapeSerializer extends SerdeContextConfig implements ShapeSeri
return void 0;
}

if (ns.isBlobSchema() && (value instanceof Uint8Array || typeof value === "string")) {
if (
(ns.isBlobSchema() && (value instanceof Uint8Array || typeof value === "string")) ||
(ns.isDocumentSchema() && value instanceof Uint8Array)
) {
if (ns === this.rootSchema) {
return value;
}
if (!this.serdeContext?.base64Encoder) {
throw new Error("Missing base64Encoder in serdeContext");
return toBase64(value);
}
return this.serdeContext?.base64Encoder(value);
}

if (ns.isTimestampSchema() && value instanceof Date) {
const options = this.settings.timestampFormat;
const format = options.useTrait
? ns.getSchema() === SCHEMA.TIMESTAMP_DEFAULT
? options.default
: ns.getSchema() ?? options.default
: options.default;
if ((ns.isTimestampSchema() || ns.isDocumentSchema()) && value instanceof Date) {
const format = determineTimestampFormat(ns, this.settings);
switch (format) {
case SCHEMA.TIMESTAMP_DATE_TIME:
return value.toISOString().replace(".000Z", "Z");
Expand Down Expand Up @@ -124,6 +138,22 @@ export class JsonShapeSerializer extends SerdeContextConfig implements ShapeSeri
}
}

if (ns.isDocumentSchema()) {
if (isObject) {
const out = Array.isArray(value) ? [] : ({} as any);
for (const [k, v] of Object.entries(value)) {
if (v instanceof NumericValue) {
out[k] = v;
} else {
out[k] = this._write(ns, v);
}
}
return out;
} else {
return structuredClone(value);
}
}

return value;
}
}
Loading
Loading