Skip to content

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

@seanbotha123

Description

@seanbotha123

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.

Metadata

Metadata

Labels

enhancementNew feature or request

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions