Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Empty strings in version 0.1.15, is converted to null #58

Open
bjsk opened this issue Jun 7, 2017 · 3 comments
Open

Empty strings in version 0.1.15, is converted to null #58

bjsk opened this issue Jun 7, 2017 · 3 comments

Comments

@bjsk
Copy link

bjsk commented Jun 7, 2017

Hello,

I have been using cerialize for a while, and I am generally very satisfied. It does have some funky behaviors that you will have to learn to use it in more advanced cases, but it is not that problematic.

However, after the latest update (0.1.15) an empty JSON string ("") is deserialized into null, which for me is a big problem. I will now have to go through my objects manually to change this back to empty string. Meaning that the value gained from using the library seems to disappear.

My solution for now is to roll back to the version 0.1.14, which also means rolling back my typescript version.

I hope that you can help me, and maybe release a new version that fixes this, or at least makes it as an option or similar.

  • Bjarke
@weichx
Copy link
Owner

weichx commented Jun 7, 2017

Glad you like the library, would love to know what you think is funky. (some things I know about and are a language limitation 👎 )

As far as this issue is concerned, I'm not sure I understand your use case. Are you saying that deserializing an emptystring to json should results in {} and doesn't? can you provide a code sample?

@bjsk
Copy link
Author

bjsk commented Jun 8, 2017

Thanks for your response.

No an empty string '' should still be an empty string '' when it is deserialized. Actually none of the versions give what I would consider as the correct result. Here is an example that illustrates the issue:

The Class:

export class Test {
    @deserializeAs(String) public type: string;
    @deserializeAs(String) public uuid: string;
    @deserializeAs(String) public name: string;
    constructor() {
        this.type = "";
        this.uuid = "";
        this.name = "";
    }
}

The code:

let json_data : any = {"name": "Hello world", "uuid": "", "type": "test"};
const test: Test = Deserialize(json_data, Test);
console.log(JSON.stringify(test));

let json_data2 : any = {"name": "Hello world", "uuid": null, "type": "test"};
const test2: Test = Deserialize(json_data2, Test);
console.log(JSON.stringify(test2));

(The JSON.stringify is just to have a result that is easy to copy/paste here)

Version 0.1.14 Output:

{"type":"test","uuid":"","name":"Hello world"}
{"type":"test","uuid":"null","name":"Hello world"}

Version 0.1.15 Output:

{"type":"test","uuid":null,"name":"Hello world"}
{"type":"test","uuid":null,"name":"Hello world"}

Expected Output:

{"type":"test","uuid":"","name":"Hello world"}
{"type":"test","uuid":null,"name":"Hello world"}

The only way that I can trigger the "expected result" (in both versions) is to change the uuid attribute in the Test class to be like this:
@deserialize public uuid: string;

But using the simple deserialize removes the check for type, which is not ideal.

@weichx
Copy link
Owner

weichx commented Jun 9, 2017

Ah ok, I see what you mean. I'll take a look at this though I'm traveling for the next few days so may not find the time until next week.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants