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

Ability to emit objects that have default value of serialize call. #112

Open
seanbotha123 opened this issue Sep 19, 2019 · 4 comments
Open
Assignees
Labels
enhancement New feature or request

Comments

@seanbotha123
Copy link

Hey guys,

So the reason for this request is as follows. Picture you have a class that has a few properties, you have to serialise and de-serialise when doing server calls, all is perfect, network calls are fine ect etc.

Now the issue comes in when you start having multiple classes with loads of properties, and theses classes reference other classes that have loads of properties. When you are serialising the classes now the json object returned starts getting rather large, which causes the network requests to become rather large also, and user experience starts going down the drain.

What I am requesting is a way to make the serialisation remove properties that are equal to their default values, consider the following class:

@JsonObject
export class Car {
    @JsonProperty('hasWheels', Boolean)
    hasWheels = true;

    @JsonProperty('wheelCount', Number)
    wheelCount = 4;

    @JsonProperty('make', String)
    make = '';

    @JsonProperty('model', String)
    model = '';
}

Now consider the following two objects:

let car1 = {
    hasWheels: true,
    wheelCount: 4,
    make: 'make1',
    model: 'model1'
} as Car;

let car2 = {
    hasWheels: true,
    wheelCount: 3,
    make: 'make2',
    model: 'model1'
} as Car;

When serialising these items currently both of them will have 4 properties in the serialised object, but if we drop the values that are equal to the defaults, then car1 would only have 2 properties in the serialised object,

{
    "make": "make1",
    "model": "model1"
}

and car2 would only have 3 properties.

{
    "wheelCount": 3,
    "make": "make2",
    "model": "model1"
}

While in this scenario this is not a lot, there are other applications that can benefit a lot from something like this.

I do understand that this would not be valid for lots of applications, and feel that it is more of a niche case, but this would help a lot in the overall experience in the project that I a busy with, as I am using json2typescript for serialisation and de-serialisation, so there should also not be any data loss.

@andreas-aeschlimann andreas-aeschlimann self-assigned this Sep 23, 2019
@andreas-aeschlimann
Copy link
Member

This is a very interesting idea, thank you.

Will definitely consider this in an update. Any suggestion how we would achieve that? Probably with an additional setting on the jsonConvert instance?

@andreas-aeschlimann andreas-aeschlimann added the enhancement New feature or request label Sep 23, 2019
@seanbotha123
Copy link
Author

Well I'm guessing there are 3 ways to go; either add a new serialize method that can do this, or add another parameter to the existing serialize method that can toggle this functionality on or off, or as you suggested as a setting on the instance.

@seanbotha123
Copy link
Author

Hi @andreas-aeschlimann,

Have you given any thought as to which method would be the best to use? I would like to look at this issue and submit a pull, but would like your input on the best way to do this.

@andreas-aeschlimann
Copy link
Member

I suggest that we introduce a new flag for serialization mode. When set, the serialization compares a given instance to a new instance and omits equal values.

There is still discussion going on in a PR how we shall handle different scenarios of serialization with null/undefined, so I am not sure if we should wait for that and include this flag there.

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

2 participants