LabEG / Serializable

Small library for deserialization and serialization for javascript and typescript
MIT License
62 stars 10 forks source link

Enhancement #7

Closed Zig1375 closed 1 year ago

Zig1375 commented 3 years ago

Actually JSON is not an object, but a string and it would be great if the fromJSON method can receive a string was well.

const json: string = '{"firstName": "John Doe"}';
const user = User.fromJSON( json );

because at this moment the code looks like this:

const json: string = '{"firstName": "John Doe"}';
const user = User.fromJSON( JSON.parse(json) );
LabEG commented 2 years ago

Nice feature. I'll add it soon.

LabEG commented 1 year ago

Sorry for long response. Lost this issue. I add method fromString and toString. Now you can deserilize direct from string. toString ovverride default toString object method an make serializtion to json string.

const user = User.fromString('{"firstName": "John Doe"}');

fetch("url", {
    method: "POST",
    body: user.toString()
});