反序列化JSON导致编译时错误

时间:2016-12-09 20:23:15

标签: json class object typescript deserialization

我想从标准JSON对象创建一个模拟User对象。

我正在尝试在this SO answer上实施 #Option 4

但是,当我从JSON对象创建我的模拟用户时,我得到了这个编译时错误:

  

类型'{id:number;}中缺少属性'deserialize'用户名:   串; profilePhoto:string; coverPhoto:string; country:string;   reputa ...'。

deserialize是一个函数,而不是属性,因此我不想将它与我的JSON对象一起传递。

这是我的班级:

interface iSerializable<T> {
    deserialize(input: Object): T;
}

export class User implements iSerializable<User>{
    public id?: number;
    public userName?: string;
    public country?: string;
    public profilePhoto?: string;
    public coverPhoto?: string;
    public reputation?: number;
    public description?: string;
    public favourites?: Result[];
    public followers?: User[];
    public following?: User[];
    public reviews?: Review[];
    public catchPhrase?: string;
    public itemsAdded?: string;
    public topActivities?: string;
    public recentActivity?: any[];
    public lastSeen?: Date;
    private _lastSeenString?: string;
    get lastSeenString(): string {
        return Time.timeSince(this.lastSeen);
    } set lastSeenString(value: string) {
        this._lastSeenString = value;
    }
    public badges?: string[];
    public memberSince?: Date;

    public deserialize(input: User) {
        this.lastSeen = input.lastSeen;
        this.badges = input.badges;
        this.catchPhrase = input.catchPhrase;
        this.country = input.country;
        this.coverPhoto = input.coverPhoto;
        this.description = input.description;
        this.favourites = input.favourites;
        this.followers = input.followers;
        this.following = input.following;
        this.id = input.id;
        this.itemsAdded = input.itemsAdded;
        this.memberSince = input.memberSince;
        this.profilePhoto = input.profilePhoto;
        this.recentActivity = input.recentActivity;
        this.reputation = input.reputation;
        this.reviews = input.reviews;
        this.topActivities = input.topActivities;
        this.userName = input.userName;
        return this;
    }
}

这是我尝试从JSON创建该类的实例:

export var SERENA: User = new User().deserialize({
   id: 1,
   userName: "irishNimue",
   profilePhoto: "profile-photo-images/serena.jpg",
    coverPhoto: "cover-photo-images/ben-cover-photo.jpg",
   country: "New Zealand",
    reputation: 501,
    description: "Interests include comics, wine, good food, surfing and exercising",
    favourites: RESULTS,
    followers: [BEN],
    following: [BEN],
    reviews: [
  {
    id: 11,
    date: new Date("October 20, 2016 11:13:00"),
    description: 'Suspendisse accumsan vehicula consequat. Nam ultrices egestas enim sit amet sollicitudin. Aenean volutpat enim nec sem dignissim, non consectetur mauris pharetra. Sed a maximus ante. Suspendisse potenti. Nunc ornare erat urna, egestas gravida nisi pretium vitae. Sed id leo nunc.',
    rating: 2.1,
    likes: 5,
    user: SERENA,
    comments: [{
      id: 1,
      description: "great review!",
      user: BEN
    }]
  },
  {
    id: 12,
    date: new Date("October 6, 2016 11:13:00"),
    description: 'Suspendisse accumsan vehicula consequat. Nam ultrices egestas enim sit amet sollicitudin. Aenean volutpat enim nec sem dignissim, non consectetur mauris pharetra. Sed a maximus ante. Suspendisse potenti. Nunc ornare erat urna, egestas gravida nisi pretium vitae. Sed id leo nunc.',
    rating: 6.9,
    likes: 5,
    user: SERENA,
    comments: [{
      id: 1,
      description: "great review!",
      user: BEN
    }]
  },
  {
    id: 13,
    date: new Date("October 3, 2016 11:13:00"),
    description: 'Maecenas dui dui, gravida et sagittis vitae, ornare ut urna. Proin id nulla ut ante lacinia tincidunt. Nullam eget risus blandit ex dapibus feugiat.',
    rating: 7.1,
    likes: 5,
    user: SERENA,
    comments: [{
      id: 1,
      description: "great review!",
      user: BEN
    }]
  },
  {
    id: 14,
    date: new Date("October 1, 2016 11:13:00"),
    description: 'Vivamus fringilla leo eu cursus consequat. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Aliquam a odio turpis.',
    rating: 8.1,
    likes: 5,
    user: SERENA,
    comments: [{
      id: 1,
      description: "great review!",
      user: BEN
    }]
  }],
    catchPhrase: "It's like a tiny doormat",
    itemsAdded: null,
    topActivities: null,
    recentActivity: null,
    lastSeen: new Date("October 13, 2016 11:13:00"),
    badges: ["active User", "helper"],
    memberSince: new Date("October 13, 2014"),
  lastSeenString: ""
});

如何摆脱错误并成功从JSON创建User类的实例?

0 个答案:

没有答案