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

JsonMember's reviver and replacer #84

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
8 changes: 8 additions & 0 deletions js/typed-json.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ interface JsonObjectOptions<T> {
serializer?: (object: T) => any;
/** A custom deserializer function transforming a JSON object to an instace. */
initializer?: (json: any) => T;
/** A function that transforms the JSON after serializing. */
replacer?: (key: string, value: any) => any;
/** A function that transforms the JSON before deserializing. */
reviver?: (key: any, value: any) => any;
}
/**
* Specifies that the type is serializable to and deserializable from a JSON string.
Expand Down Expand Up @@ -76,6 +80,10 @@ interface JsonMemberOptions<TFunction extends Function> {
emitDefaultValue?: boolean;
/** When set, type-hint is mandatory when deserializing. Set for properties with interface or abstract types/element-types. */
refersAbstractType?: boolean;
/** Will be invoked, with the deserialized value in order to let the member, revive it. */
reviver?: (deserializedValue: any) => any;
/** Will be invoked, with the serialized value in order to let the member, replace it. */
replacer?: (serializedValue: any) => any;
}
/**
* Specifies that the property is part of the object when serializing.
Expand Down
17 changes: 12 additions & 5 deletions js/typed-json.js

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

2 changes: 1 addition & 1 deletion js/typed-json.js.map

Large diffs are not rendered by default.

29 changes: 29 additions & 0 deletions src/tests/test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import {isEqual} from "./object-compare";
import {JsonObject, JsonMember, TypedJSON} from "../typed-json";

@JsonObject
class Person {
@JsonMember({type : String})
firstName: string;

@JsonMember({type : String})
lastName: string;

@JsonMember({type : Number, reviver : value=> value + 5, replacer : value=> value * 10 })
level : number


public getFullname() {
return this.firstName + " " + this.lastName;
}
}


var person = TypedJSON.parse('{ "firstName": "John", "lastName": "Doe", level : 20 }', Person);
var jsPerson = TypedJSON.stringify(person)

console.log("Pesron Level: ",person.level)
console.log("jsPerson", jsPerson)



29 changes: 28 additions & 1 deletion src/typed-json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,11 @@ class JsonMemberMetadata<T> {
public order: number;

public forceEnableTypeHinting: boolean;

/** Will be invoked, with the deserialized value in order to let the member, revive it. */
reviver? : (deserializedValue : any)=>any
/** Will be invoked, with the serialized value in order to let the member, replace it. */
replacer? : (serializedValue : any)=>any
}

class JsonObjectMetadata<T> {
Expand Down Expand Up @@ -567,6 +572,12 @@ interface JsonObjectOptions<T> {

/** A custom deserializer function transforming a JSON object to an instace. */
initializer?: (json: any) => T;

/** A function that transforms the JSON after serializing. */
replacer?: (key: string, value: any) => any;

/** A function that transforms the JSON before deserializing. */
reviver?: (key: any, value: any) => any;
}

/**
Expand Down Expand Up @@ -687,6 +698,11 @@ interface JsonMemberOptions<TFunction extends Function> {

/** When set, type-hint is mandatory when deserializing. Set for properties with interface or abstract types/element-types. */
refersAbstractType?: boolean;

/** Will be invoked, with the deserialized value in order to let the member, revive it. */
reviver? : (deserializedValue : any)=>any
/** Will be invoked, with the serialized value in order to let the member, replace it. */
replacer? : (serializedValue : any)=>any
}

function jsonMemberTypeInit<T>(metadata: JsonMemberMetadata<T>, propertyName: string, warnArray = false) {
Expand Down Expand Up @@ -961,7 +977,7 @@ abstract class Serializer {
Object.keys(objectMetadata.dataMembers).forEach(propertyKey => {
var propertyMetadata = objectMetadata.dataMembers[propertyKey];

json[propertyMetadata.name] = this.writeToJsonObject(object[propertyKey], {
let temp = this.writeToJsonObject(object[propertyKey], {
elements: propertyMetadata.elements,
emitDefault: propertyMetadata.emitDefaultValue,
enableTypeHints: settings.enableTypeHints,
Expand All @@ -970,6 +986,12 @@ abstract class Serializer {
requireTypeHints: settings.requireTypeHints,
typeHintPropertyKey: settings.typeHintPropertyKey
});

if(propertyMetadata.replacer) {
temp = propertyMetadata.replacer(temp)
}

json[propertyMetadata.name] = temp
});
} else {
// Serialize all own properties.
Expand Down Expand Up @@ -1187,6 +1209,11 @@ abstract class Deserializer {
typeHintPropertyKey: settings.typeHintPropertyKey
});

if (propertyMetadata.reviver) {

temp = propertyMetadata.reviver(temp)
}

// Do not make undefined/null property assignments.
if (Helpers.valueIsDefined(temp)) {
object[propertyKey] = temp;
Expand Down