GillianPerard / typescript-json-serializer

A typescript library to deserialize json into typescript classes and serialize classes into json.
MIT License
213 stars 29 forks source link

About nested class deserializing #82

Closed JStudio closed 4 years ago

JStudio commented 4 years ago

Hi @GillianPerard , I'm Ash in Korea.

I'm interested in your json-serializer, it is a great product I have ever seen it in Typescipt.

And I wonder if I use it correctly, it can't deserialize the case of nested class.

import { Serializable, JsonProperty } from 'typescript-json-serializer';
import { SR_Status } from './SR_Status';
import { ContentEntry } from './ContentEntry';

@Serializable()
export class GetContentEntries_2C_1 {

    public readonly ID: string  = "GetContentEntries.2C.1";

    @JsonProperty("id")
    public id: string;

    @JsonProperty("st")
    public status: SR_Status;

    @JsonProperty({ name : "es", type: ContentEntry })
    public entries: Array<ContentEntry>;

    public constructor() {
        this.id = this.ID;
        this.status = new SR_Status();
        this.entries = new Array<ContentEntry>();
    }
}

//deserialize
const m = deserialize<GetContentEntries_2C_1>(msg,GetContentEntries_2C_1);  

//result
id: string; 
entries : object
status : object

When I try to deserialize this class,

"this.entries" and "status" remained just an object.

So I need to deserialize with each type again.

Could you let me know, it is the intended way or what am I missing something?

Thanks for your cool product.

GillianPerard commented 4 years ago

Hi! I really appreciate you use and like my lib 😊

Let me check what happened, could you just provide me the msg object as well please, to be sure to reproduce exactly the case.

JStudio commented 4 years ago

@GillianPerard Thanks for your reply! Here is msg object.

{"id":"CASINO.GetContentEntries.2C.1","st":{"c":0,"m":""},"es":[{"ty":"SLOTS","no":1001001,"tit":"Wrath of Zeus","st":"OFF","ps":{"scene":"wrath_of_zeus","rev":"1","version":"1.3.1","leaderboard":"ON"},"sin":1485874800000,"pop":648,"tpc":0,"twa":0,"mwa":0,"ftm":0,"ltm":0},{"ty":"SLOTS","no":1011001,"tit":"Wizard of the Moon","st":"OFF","ps":{"scene":"wizard_of_the_moon","rev":"1","version":"1.4.4","leaderboard":"ON"},"sin":1561820400000,"pop":322,"tpc":0,"twa":0,"mwa":0,"ftm":0,"ltm":0},{"ty":"SLOTS","no":1002001,"tit":"Forest of Phantasm","st":"ON","ps":{"scene":"forest_of_phantasm","rev":"1","version":"1.3.1","leaderboard":"ON"},"sin":1488294000000,"pop":5250,"tpc":0,"twa":0,"mwa":0,"ftm":0,"ltm":0},{"ty":"SLOTS","no":1003001,"tit":"Cleopatra and Caesar","st":"ON","ps":{"scene":"cleopatra_and_caesar","rev":"1","version":"1.3.1","leaderboard":"ON"},"sin":1490972400000,"pop":11148,"tpc":0,"twa":0,"mwa":0,"ftm":0,"ltm":0},{"ty":"SLOTS","no":1004001,"tit":"The Katana and the Sakura","st":"ON","ps":{"scene":"the_katana_and_the_sakura","rev":"1","version":"1.3.1","leaderboard":"ON"},"sin":1493564400000,"pop":3656,"tpc":0,"twa":0,"mwa":0,"ftm":0,"ltm":0},{"ty":"SLOTS","no":1005001,"tit":"Poisonous Flower","st":"ON","ps":{"scene":"poisonous_flower","rev":"1","version":"1.3.1","leaderboard":"ON"},"sin":1496242800000,"pop":2986,"tpc":0,"twa":0,"mwa":0,"ftm":0,"ltm":0},{"ty":"SLOTS","no":1006001,"tit":"HOT TRIPLE SEVEN","st":"ON","ps":{"scene":"hot_triple_seven","rev":"1","version":"1.3.1","leaderboard":"ON"},"sin":1498834800000,"pop":2774,"tpc":0,"twa":0,"mwa":0,"ftm":0,"ltm":0},{"ty":"SLOTS","no":1007001,"tit":"Buffalo of Twillight","st":"ON","ps":{"scene":"twilight_buffalo","rev":"1","version":"1.3.1","leaderboard":"ON"},"sin":1501513200000,"pop":3463,"tpc":0,"twa":0,"mwa":0,"ftm":0,"ltm":0},{"ty":"SLOTS","no":1008001,"tit":"Secret Flower","st":"OFF","ps":{"scene":"secret_flower","rev":"1","version":"1.3.1","leaderboard":"ON"},"sin":1513004400000,"pop":22,"tpc":0,"twa":0,"mwa":0,"ftm":0,"ltm":0},{"ty":"SLOTS","no":1009001,"tit":"White Storm","st":"OFF","ps":{"scene":"white_storm","rev":"1","version":"1.3.1","leaderboard":"ON"},"sin":1525014000000,"pop":179,"tpc":0,"twa":0,"mwa":0,"ftm":0,"ltm":0},{"ty":"SLOTS","no":1010001,"tit":"Devil's Touch","st":"ON","ps":{"scene":"devils_touch","rev":"1","version":"1.3.1","leaderboard":"ON"},"sin":1527001200000,"pop":3182,"tpc":0,"twa":0,"mwa":0,"ftm":0,"ltm":0},{"ty":"SLOTS","no":1012001,"tit":"NONE","st":"OFF","ps":{"scene":"-","rev":"1"},"sin":1561820400000,"pop":0,"tpc":0,"twa":0,"mwa":0,"ftm":0,"ltm":0},{"ty":"VIDEO_POKER","no":10000,"tit":"Jacks or Better","st":"ON","ps":{"scene":"video_poker_layout","rev":"1","version":"1.1.5"},"sin":1501513200000,"pop":152,"tpc":0,"twa":0,"mwa":0,"ftm":0,"ltm":0},{"ty":"VIDEO_POKER","no":10001,"tit":"All American","st":"ON","ps":{"scene":"video_poker_layout","rev":"1","version":"1.1.5"},"sin":1530370800000,"pop":171,"tpc":0,"twa":0,"mwa":0,"ftm":0,"ltm":0},{"ty":"VIDEO_POKER","no":10002,"tit":"Tens or Better","st":"ON","ps":{"scene":"video_poker_layout","rev":"1","version":"1.1.5"},"sin":1530370800000,"pop":7,"tpc":0,"twa":0,"mwa":0,"ftm":0,"ltm":0},{"ty":"VIDEO_POKER","no":10100,"tit":"Bonus Poker","st":"ON","ps":{"scene":"video_poker_layout","rev":"1","version":"1.1.5"},"sin":1501599600000,"pop":42,"tpc":0,"twa":0,"mwa":0,"ftm":0,"ltm":0},{"ty":"VIDEO_POKER","no":10101,"tit":"Double Bonus Poker","st":"ON","ps":{"scene":"video_poker_layout","rev":"1","version":"1.1.5"},"sin":1501686000000,"pop":0,"tpc":0,"twa":0,"mwa":0,"ftm":0,"ltm":0},{"ty":"VIDEO_POKER","no":10102,"tit":"Double Double Bonus Poker","st":"ON","ps":{"scene":"video_poker_layout","rev":"1","version":"1.1.5"},"sin":1530370800000,"pop":5,"tpc":0,"twa":0,"mwa":0,"ftm":0,"ltm":0},{"ty":"VIDEO_POKER","no":10200,"tit":"Deuces Wild","st":"ON","ps":{"scene":"video_poker_layout","rev":"1","version":"1.1.5"},"sin":1501772400000,"pop":3655,"tpc":0,"twa":0,"mwa":0,"ftm":0,"ltm":0},{"ty":"VIDEO_POKER","no":10300,"tit":"Joker's Wild","st":"ON","ps":{"scene":"video_poker_layout","rev":"1","version":"1.1.5"},"sin":1530370800000,"pop":280,"tpc":0,"twa":0,"mwa":0,"ftm":0,"ltm":0},{"ty":"MINI_GAME","no":100,"tit":"WHEEL","st":"OFF","ps":{},"sin":1530370800000,"pop":0,"tpc":0,"twa":0,"mwa":0,"ftm":0,"ltm":0},{"ty":"MINI_GAME","no":200,"tit":"MATCH3","st":"ON","ps":{"scene":"match_game","rev":"1"},"sin":1504278000000,"pop":0,"tpc":0,"twa":0,"mwa":0,"ftm":0,"ltm":0},{"ty":"MINI_GAME","no":300,"tit":"LOTTO","st":"ON","ps":{"scene":"lotto_game","rev":"1"},"sin":1504364400000,"pop":0,"tpc":0,"twa":0,"mwa":0,"ftm":0,"ltm":0}]}

GillianPerard commented 4 years ago

You did not provide me your subclasses like ContentEntry or SR_Status but I think you miss to set these classes Serializable and use the JsonProperty on each of their properties 😊

@Serializable()
class SR_Status {
    @JsonProperty() c: number;
    @JsonProperty() m: string;
}

@Serializable()
export class PS {
    @JsonProperty() scene: string;
    @JsonProperty() rev: string;
    @JsonProperty() version: string;
    @JsonProperty() leaderboard: string;
}

@Serializable()
export class ContentEntry {
    @JsonProperty() ty: string;
    @JsonProperty() no: number;
    @JsonProperty() tit: string;
    @JsonProperty() st: string;
    @JsonProperty() ps: PS;
    @JsonProperty() sin: number;
    @JsonProperty() pop: number;
    @JsonProperty() tpc: number;
    @JsonProperty() twa: number;
    @JsonProperty() mwa: number;
    @JsonProperty() ftm: number;
    @JsonProperty() ltm: number;
}

@Serializable()
export class GetContentEntries_2C_1 {
    public readonly ID: string = 'GetContentEntries.2C.1';

    @JsonProperty('id')
    public id: string;

    @JsonProperty('st')
    public status: SR_Status;

    @JsonProperty({ name: 'es', type: ContentEntry })
    public entries: Array<ContentEntry>;

    public constructor() {
        this.id = this.ID;
        this.status = new SR_Status();
        this.entries = new Array<ContentEntry>();
    }
}

const m = deserialize<GetContentEntries_2C_1>(msg, GetContentEntries_2C_1); 

This is just an example, you have to verify the classes 😊

JStudio commented 4 years ago

Thanks for your answer!

It works well, I missed the @Serializable() on sub class.

You saved my day! you're a genius

GillianPerard commented 4 years ago

I'm happy it works and again thanks a lot for your compliments and especially to use my lib 😊.