typescript-json-serializer
typescript-json-serializer copied to clipboard
[FEAT]: The way/method to identify serializer or serialize group
Description
I have a class UserModel
:
class UserModel {
id: string;
addresses: string[];
}
Then, this class will be serialized to UserDto
and plain model
class UserDto {
id: string;
address: string;
}
The problem here is the property addresses
is serialized to address
in DTO and addresses
in plain model. ~~I add property serializeType
into UserModel
for current workaround, and use it to determine DTO or plain model when serialize~~ <= bad ideal
Proposed solution
I suggest we can transfer serializer name to hooks beforeDeserialize
and afterSerialize
. Do you have any better ideal for this problem?
Hi, than you for using my lib.
This way is not good for you or I missed something?
import {
JsonObject,
JsonSerializer,
JsonProperty,
} from 'typescript-json-serializer';
const json = {
id: '3',
address: 'test,test2,test3',
};
@JsonObject()
class UserModel {
@JsonProperty()
id: string;
@JsonProperty({
name: 'address',
beforeDeserialize: (property) => property?.split(','),
beforeSerialize: (property) => property.join(','),
})
addresses: string[];
}
const serializer = new JsonSerializer();
const d = serializer.deserialize(json, UserModel);
const s = serializer.serialize(d);
console.log(d); //UserModel {id: "3", addresses: Array[3]}
console.log(s); //{id: "3", address: "test,test2,test3"}
Hi, than you for using my lib.
This way is not good for you or I missed something?
Yeah, above just a normal case. As you can see serializer.serialize(d)
only return an object with address: "test,test1,test2"
. But I wanna return addresses: ["test", "test1", "test2"]
as well. I have forked your project, and add name
into serializer class. Then, I rewrite my model
@JsonProperty({
name: ['addresses', 'address'],
beforeDeserialize: (val) => {
if (val['addres']) { // is dto
return [val['addres']];
} else if (val['addreses']) { // is model
return val['addreses'];
}
},
afterSerialize: (val, obj, serializer) => {
if (serializer.options.name === 'model') {
return { addreses: val };
}
return { address: Array.isArray(val) && val.length ? val.join(',') : '' };
},
})
addreses: string[];
I create 2 serializer with different names, one for DTO serialization and one for model serialization. I wanna this feature can be available in public package not my fork, so I discuss with you about contribution for this feat.
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.
Based on the title (but not content). I'd like to see a way to set specific serializations/deserializations based on groups. Something like
@JsonProperty({group: ['create','update']})
(or any other name) like class-transformer
so i could serialize an object with only properties from a specific group. Or even just the ability to exclude certain properties directly when calling serialize()
Alternatively, set a Dto for them to match, and all properties not in the dto are ignored.
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.