-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Split the src files, added custom converters and adjusted tests
- Loading branch information
1 parent
96ba86d
commit 0527c25
Showing
28 changed files
with
1,805 additions
and
810 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 +1,4 @@ | ||
export * from "./src/json2typescript/json-convert"; | ||
export { JsonConvert } from "./src/json2typescript/json-convert"; | ||
export { JsonCustomConvert } from "./src/json2typescript/json-custom-convert"; | ||
export { ValueCheckingMode, OperationMode } from "./src/json2typescript/json-convert-enums"; | ||
export { JsonObject, JsonProperty, JsonConverter } from "./src/json2typescript/json-convert-decorators"; |
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 |
---|---|---|
@@ -0,0 +1,63 @@ | ||
"use strict"; | ||
exports.__esModule = true; | ||
var json_convert_options_1 = require("./json-convert-options"); | ||
/** | ||
* Decorator of a class that is a custom converter. | ||
* | ||
* @param target the class | ||
*/ | ||
function JsonConverter(target) { | ||
target[json_convert_options_1.Settings.MAPPER_PROPERTY] = ""; | ||
} | ||
exports.JsonConverter = JsonConverter; | ||
/** | ||
* Decorator of a class that comes from a JSON object. | ||
* | ||
* @param target the class | ||
*/ | ||
function JsonObject(target) { | ||
target[json_convert_options_1.Settings.MAPPING_PROPERTY] = []; | ||
} | ||
exports.JsonObject = JsonObject; | ||
/** | ||
* Decorator of a class property that comes from a JSON object. | ||
* | ||
* The second param can be either a type or a class of a custom converter. | ||
* | ||
* Use the following notation for the type: | ||
* - Primitive type: String|Number|Boolean | ||
* - Custom type: YourClassName | ||
* - Array type: [String|Number|Boolean|YourClassName] | ||
* | ||
* If you decide to use a custom converter, make sure this class implements the interface JsonCustomConvert from this package. | ||
* | ||
* @param jsonProperty the key in the expected JSON object | ||
* @param conversionOption optional param (default: undefined), should be either the expected type (String|Boolean|Number|etc) or a custom converter class implementing JsonCustomConvert | ||
* @param isOptional optional param (default: false), if true, the json property does not have to be present in the object | ||
* | ||
* @returns {(target:any, key:string)=>void} | ||
*/ | ||
function JsonProperty(jsonProperty, conversionOption, isOptional) { | ||
return function (target, classProperty) { | ||
if (typeof (target[json_convert_options_1.Settings.MAPPING_PROPERTY]) === "undefined") { | ||
target[json_convert_options_1.Settings.MAPPING_PROPERTY] = []; | ||
} | ||
if (typeof (isOptional) === "undefined") { | ||
isOptional = false; | ||
} | ||
var jsonPropertyMappingOptions = new json_convert_options_1.MappingOptions(); | ||
jsonPropertyMappingOptions.classProperty = classProperty; | ||
jsonPropertyMappingOptions.jsonProperty = jsonProperty; | ||
jsonPropertyMappingOptions.isOptional = isOptional ? isOptional : false; | ||
// Check if conversionOption is a type or a custom converter. | ||
if (typeof (conversionOption) !== "undefined" && typeof (conversionOption[json_convert_options_1.Settings.MAPPER_PROPERTY]) !== "undefined") { | ||
jsonPropertyMappingOptions.customConverter = new conversionOption(); | ||
} | ||
else { | ||
jsonPropertyMappingOptions.expectedType = conversionOption; | ||
} | ||
// Save the mapping info | ||
target[json_convert_options_1.Settings.MAPPING_PROPERTY][classProperty] = jsonPropertyMappingOptions; | ||
}; | ||
} | ||
exports.JsonProperty = JsonProperty; |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,68 @@ | ||
import { MappingOptions, Settings } from "./json-convert-options"; | ||
|
||
/** | ||
* Decorator of a class that is a custom converter. | ||
* | ||
* @param target the class | ||
*/ | ||
export function JsonConverter(target: any) { | ||
target[Settings.MAPPER_PROPERTY] = ""; | ||
} | ||
|
||
/** | ||
* Decorator of a class that comes from a JSON object. | ||
* | ||
* @param target the class | ||
*/ | ||
export function JsonObject(target: any) { | ||
target[Settings.MAPPING_PROPERTY] = []; | ||
} | ||
|
||
/** | ||
* Decorator of a class property that comes from a JSON object. | ||
* | ||
* The second param can be either a type or a class of a custom converter. | ||
* | ||
* Use the following notation for the type: | ||
* - Primitive type: String|Number|Boolean | ||
* - Custom type: YourClassName | ||
* - Array type: [String|Number|Boolean|YourClassName] | ||
* | ||
* If you decide to use a custom converter, make sure this class implements the interface JsonCustomConvert from this package. | ||
* | ||
* @param jsonProperty the key in the expected JSON object | ||
* @param conversionOption optional param (default: undefined), should be either the expected type (String|Boolean|Number|etc) or a custom converter class implementing JsonCustomConvert | ||
* @param isOptional optional param (default: false), if true, the json property does not have to be present in the object | ||
* | ||
* @returns {(target:any, key:string)=>void} | ||
*/ | ||
export function JsonProperty(jsonProperty: string, conversionOption?: any, isOptional?: boolean): any { | ||
|
||
return function (target: any, classProperty: string): void { | ||
|
||
if (typeof(target[Settings.MAPPING_PROPERTY]) === "undefined") { | ||
target[Settings.MAPPING_PROPERTY] = []; | ||
} | ||
|
||
if (typeof(isOptional) === "undefined") { | ||
isOptional = false; | ||
} | ||
|
||
let jsonPropertyMappingOptions = new MappingOptions(); | ||
jsonPropertyMappingOptions.classProperty = classProperty; | ||
jsonPropertyMappingOptions.jsonProperty = jsonProperty; | ||
jsonPropertyMappingOptions.isOptional = isOptional ? isOptional : false; | ||
|
||
// Check if conversionOption is a type or a custom converter. | ||
if (typeof(conversionOption) !== "undefined" && typeof(conversionOption[Settings.MAPPER_PROPERTY]) !== "undefined") { | ||
jsonPropertyMappingOptions.customConverter = new conversionOption(); | ||
} else { | ||
jsonPropertyMappingOptions.expectedType = conversionOption; | ||
} | ||
|
||
// Save the mapping info | ||
target[Settings.MAPPING_PROPERTY][classProperty] = jsonPropertyMappingOptions; | ||
|
||
} | ||
|
||
} |
Oops, something went wrong.