Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

- Find case insensitive items #42

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
32 changes: 25 additions & 7 deletions index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 22 additions & 3 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ export function JsonProperty<T>(metadata?: IDecoratorMetaData<T>|string): (targe
decoratorMetaData = metadata as IDecoratorMetaData<T>;
}
else {
throw new Error('index.ts: meta data in Json property is undefined. meta data: ' + metadata)
decoratorMetaData = new DecoratorMetaData<T>();
}

return Reflect.metadata(JSON_META_DATA_KEY, decoratorMetaData);
Expand Down Expand Up @@ -117,12 +117,29 @@ function hasAnyNullOrUndefined(...args: any[]) {
return args.some((arg: any) => arg === null || arg === undefined);
}

/**
* Ensure obj has key, otherwise try to find case-insensitve version of key
*
* @param obj
* @param key
*/
function getCaseInsensitiveMatch(obj: any, key: string): any {
if ((obj as Object).hasOwnProperty(key))
return key;
else {
let newKey = Object.keys(obj).find(i => {
return key.toUpperCase() === i.toUpperCase();
});

return newKey || key;
}
}

function mapFromJson<T>(decoratorMetadata: IDecoratorMetaData<any>, instance: T, json: IGenericObject, key: any): any {
/**
* if decorator name is not found, use target property key as decorator name. It means mapping it directly
*/
let decoratorName = decoratorMetadata.name || key;
let decoratorName = getCaseInsensitiveMatch(json, decoratorMetadata.name || key);
let innerJson: any = json ? json[decoratorName] : undefined;
let clazz = getClazz(instance, key);
if (isArrayOrArrayClass(clazz)) {
Expand Down Expand Up @@ -184,7 +201,9 @@ export function deserialize<T extends IGenericObject>(Clazz: {new(): T}, json: I
* pass value to instance
*/
if (decoratorMetaData && decoratorMetaData.customConverter) {
instance[key] = decoratorMetaData.customConverter.fromJson(json[decoratorMetaData.name || key]);
let jsonKey = getCaseInsensitiveMatch(json, decoratorMetaData.name || key);

instance[key] = decoratorMetaData.customConverter.fromJson(json[jsonKey]);
} else {
instance[key] = decoratorMetaData ? mapFromJson(decoratorMetaData, instance, json, key) : json[key];
}
Expand Down
1 change: 1 addition & 0 deletions libs/utils.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions spec/common/dateconverter.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { ICustomConverter } from '../../index';
declare const dateConverter: ICustomConverter;
export default dateConverter;
2 changes: 1 addition & 1 deletion spec/common/dateconverter.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions spec/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export {};
49 changes: 25 additions & 24 deletions spec/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions spec/serialize.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export {};
Loading