2

I have the following Json:

{
  "Setting": {
    "IsYear": true
  },
  "Durations": [
    {
      "Value": "100000000",
      "Duration": 1
    },
    {
      "Value": "100000001",
      "Duration": 2
    },
    {
      "Value": "100000002",
      "Duration": 3
    },
    {
      "Value": "100000003",
      "Duration": 5
    },
    {
      "Value": "100000004",
      "Duration": 0
    },
    {
      "Value": "100000005",
      "Duration": 8
    },
    {
      "Value": "100000006",
      "Duration": 10
    }
  ]
}

The class I have so far:

class Durations {
    IsYear = false;
    Durations = [];
}

And the deserialization:

let obj: MyObj = JSON.parse(jsonContent);

I'm actually struggling with deserializing and accessing the values in the resulting object.

Could please one show me how to properly deserialize the above json into the corresponding class with TypeScript and how to access the values?

Any help is highly appreciated.

2
  • 1
    Do you really want to deserialize to a class? Why not instead define a type: interface Durations{ IsYear: boolean; Duration: number[]; }, and then const durations: Durations = JSON.parse(...)? Commented Nov 19, 2020 at 9:13
  • Didn't thouth about this way of solution, good point. The point is that I need this object to be passed to a method which executes some calculations based on the values Commented Nov 19, 2020 at 9:55

1 Answer 1

1

A solution would be to implement the serialize and deserialize method.

My exemple is manual.

const serializedObj: string = JSON.stringify({
  "Setting": {
    "IsYear": true
  },
  "Durations": [
    {
      "Value": "100000000",
      "Duration": 1
    },
    {
      "Value": "100000001",
      "Duration": 2
    },
    {
      "Value": "100000002",
      "Duration": 3
    },
    {
      "Value": "100000003",
      "Duration": 5
    },
    {
      "Value": "100000004",
      "Duration": 0
    },
    {
      "Value": "100000005",
      "Duration": 8
    },
    {
      "Value": "100000006",
      "Duration": 10
    }
  ]
});

interface MyObjType {
  Setting: {
    IsYear: boolean;
  };

  Durations: {
    Value: string;
    Duration: number;
  }[];
}

class MyObj {
  protected IsYear: MyObjType['Setting']['IsYear'] = false;
  protected Durations: MyObjType['Durations'] = [];

  public serialize(): string {
    return JSON.stringify({
      Setting: {
        IsYear: this.IsYear,
      },

      Durations: this.Durations,
    } as MyObjType);
  }

  public deserialize(serializedObj: string): void {
    const obj: MyObjType = JSON.parse(serializedObj);

    this.IsYear = obj.Setting.IsYear;
    this.Durations = obj.Durations;
  }
}

// --- Test

const obj = new MyObj();

obj.deserialize(serializedObj);

console.log(obj.serialize());

Code snippet

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.