Skip to content

Commit

Permalink
feat: add LazyJsonString class (smithy-lang#899)
Browse files Browse the repository at this point in the history
This commit adds the `LazyJsonString` class that supports holding a
String with JSON contents, deserializing to JSON, and being built
from JSON like content.
  • Loading branch information
kstich authored and srchase committed Jun 16, 2023
1 parent 5b8bdb3 commit b4c2f94
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
1 change: 1 addition & 0 deletions packages/smithy-client/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ export * from "./command";
export * from "./document-type";
export * from "./exception";
export * from "./isa";
export * from "./lazy-json";
21 changes: 21 additions & 0 deletions packages/smithy-client/src/lazy-json.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/**
* Lazy String holder for JSON typed contents.
*/
export class LazyJsonString extends String {
deserializeJSON(): any {
return JSON.parse(super.toString());
}

toJSON(): string {
return super.toString();
}

static fromObject(object: any): LazyJsonString {
if (object instanceof LazyJsonString) {
return object;
} else if (object instanceof String || typeof object === 'string') {
return new LazyJsonString(object);
}
return new LazyJsonString(JSON.stringify(object));
}
}

0 comments on commit b4c2f94

Please sign in to comment.