-
-
Notifications
You must be signed in to change notification settings - Fork 281
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add deserialization for primitive type.
- Loading branch information
Showing
6 changed files
with
107 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
export * from "./primitive"; | ||
export * from "./array"; | ||
export * from "./map"; | ||
export * from "./set"; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import {IConverter} from "../interfaces/Converter"; | ||
import {Converter} from "../decorators/converter"; | ||
import {BadRequest} from "ts-httpexceptions"; | ||
|
||
@Converter(String, Number, Boolean) | ||
export class PrimitiveConverter implements IConverter { | ||
|
||
deserialize(data: string, target: any): String | Number | Boolean { | ||
|
||
switch (target) { | ||
case String: | ||
return "" + data; | ||
|
||
case Number: | ||
const n = +data; | ||
|
||
if (isNaN(n)) { | ||
throw new BadRequest("Cast error. Expression value is not a number."); | ||
} | ||
|
||
return n; | ||
|
||
case Boolean: | ||
|
||
if (data === "true") return true; | ||
if (data === "false") return false; | ||
|
||
return !!data; | ||
|
||
} | ||
|
||
} | ||
|
||
serialize(object: String | Number | Boolean): any { | ||
return object; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters