typescript-json-serializer icon indicating copy to clipboard operation
typescript-json-serializer copied to clipboard

[FEAT]: The way/method to identify serializer or serialize group

Open panoti opened this issue 2 years ago • 4 comments

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?

panoti avatar Jul 29 '22 16:07 panoti

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"}

GillianPerard avatar Jul 30 '22 07:07 GillianPerard

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.

panoti avatar Jul 30 '22 14:07 panoti

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.

stale[bot] avatar Sep 21 '22 08:09 stale[bot]

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.

stale[bot] avatar Oct 22 '22 20:10 stale[bot]

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.

ghost avatar Nov 08 '22 18:11 ghost

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.

stale[bot] avatar Dec 21 '22 01:12 stale[bot]