Jump to content
pyscripter

Ignoring nil values with ΤJsonSerializer

Recommended Posts

Posted (edited)

When I serialize a an object with object fields that are nilĀ  I get null values in the Json result e.g.

Ā 

Ā 
{
    "capabilities": {
        "positionEncoding": [],
        "textDocumentSync": {
            "openClose": true,
            "change": 2,
            "willSave": false,
            "willSaveWaitUntil": false,
            "save": null
        },
        "notebookDocumentSync": null,
        "completionProvider": null,
}

Ā 

How can I ignore fields that have nil values?


System.JSON.Types defines the following:

Ā  TJsonDefaultValueHandling = (Include, Ignore, Populate, IgnoreAndPopulate);

but it is not used anywhere.

Ā 

I also tried to use aĀ converter, but I could not get it to work.Ā  And in any case converters are used after the property name is written.

Ā 

Any ideas?

Edited by pyscripter

Share this post


Link to post

Does TJSonSerializer work with TObjectList and TObjectDictionary already? As far as I remember this is why I started to (de)serialize objects manually back around Delphi 10.4 but would love to automate things finally :)

Share this post


Link to post
Posted (edited)
29 minutes ago, aehimself said:

Does TJSonSerializer work with TObjectList and TObjectDictionary already? As far as I remember this is why I started to (de)serialize objects manually back around Delphi 10.4 but would love to automate things finally šŸ™‚

It includes a number of converters to handle generic collections, but I think you have to add them manually:

Ā 

image.thumb.png.227259b88e9cefe181f6b54c5856f9b4.png

Edited by pyscripter

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Ɨ