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

Feature request: serialize by class #4

Closed
Messier82 opened this issue Feb 28, 2017 · 17 comments
Closed

Feature request: serialize by class #4

Messier82 opened this issue Feb 28, 2017 · 17 comments
Assignees
Labels
enhancement New feature or request

Comments

@Messier82
Copy link

Hi! I really liked this library and just wondered, can we, developers, have slightly improved serialize function, so it can accept a class same way, as deserialize method receives it? Thank you!

@andreas-aeschlimann
Copy link
Member

Serializing an object is basically just calling JSON.stringify(instance). I have implemented that as follows:

let instance = {
     id: 1,
     name: "Andreas"
}; // test object instance
let jsonString: string = JsonConvert.serializeObject(instance);

Or did you mean something else?

@andreas-aeschlimann andreas-aeschlimann self-assigned this Feb 28, 2017
@andreas-aeschlimann
Copy link
Member

Closing due to inactivity. Will reopen if really an issue.

@Messier82
Copy link
Author

I meant to serialize and rename all parameters, how they're defined by JsonProperty in the class itself

@andreas-aeschlimann
Copy link
Member

I don't follow, can you please make an example and explain what you mean? Maybe you mean something similar like #6?

@Messier82
Copy link
Author

No. For example: we have some class, described to use JsonConvert. It have 3 params: _id, _name, _age and JsonProperty is set up to deserialize them from id, name and age(without underscores) attributes. It's working like a charm. But I want to SERIALIZE them and in output have properties, that I described by JsonProperties(id, name, age), not original ones(_id, _name, _age)

@andreas-aeschlimann
Copy link
Member

Basically you have this scenario:

JSON:
_id, _name, _age

TypeScript:
id, name, age

The deserialization maps _id to id, _name to name and _age to age.

Am I correct you would like the serialize() function to map id to _id, name to _name and age to _age again, as it was in the JSON?

@Messier82
Copy link
Author

Exactly!

@andreas-aeschlimann
Copy link
Member

Ok, I will keep this issue open and put it on my to do list for a future version.

@andreas-aeschlimann andreas-aeschlimann added the enhancement New feature or request label Apr 24, 2017
@Messier82
Copy link
Author

Thank you!

@RAHILKHERA
Copy link

Waiting for this enhancement.

@andreas-aeschlimann
Copy link
Member

@RAHILKHERA I will work on this after finishing my current project in around 2 weeks!

@RAHILKHERA
Copy link

@andreas-aeschlimann Thanks a lot. Please comment here whenever it is ready.

@HeWhoHasNoName
Copy link

@RAHILKHERA looking forward to this feature as I was curious about it not being implemented myself... as generally in the java world (being a java dev) this is what I was expecting. Thanks and look forward to see the update.

@crownofapollo
Copy link

Adding my request for this feature as well. Coming from the C# world, this was my expected behavior.

@bmercier-manakin
Copy link

+1

@raviada
Copy link

raviada commented Jul 13, 2017

+1
@Messier82, how did you solve this in the meanwhile? I have a fairly large and nested JSON which works great for deserializing into TypeScript objects. But my problem is now I have to save it to saver, and the APIs expect the json keys to have underscores instead of camecase.

please let me know if you used any alternatives to solve it temporarily until this feature is available. This is almost like Jackson libraries in Java world.

@andreas-aeschlimann
Copy link
Member

I'll close this as this is implemented now. Will publish the NPM package when the other features are included as well.

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

No branches or pull requests

7 participants