function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
nishwalnishwal 

Optional Parameters creating problem while serializing a class to json

i have declared a class which have some optional properties. When i doesnt define that property and serialize it using json.serialize method, the result json string contains the entry for that property with null value.

How to avoid adding this property while serializing it?

 

 

Anup JadhavAnup Jadhav

This post might be useful to you. They seem to have fixed this in Summer 12.

 

http://smgoodyear.com/2012/04/15/json-deserializtion-have-salesforce-got-it-wrong/

 

Thanks,

Anup

nishwalnishwal

Hey Anup, this article is talking about json deserialization and i am serialing the class to json string.

 

and what i want is if I does not define any value for any property then it shud not include it in the json string.

nishwalnishwal

No suggestions from anyone? i think i shud post it as Idea.

Anup JadhavAnup Jadhav

If you have a class with some "optional properties", if the serializer is including those properties with null value, why don't you just ignore those properties in the json string?

 

There is no switch or annotation at the moment (in any language as far as I know) to exclude certain properties, when a class is serialized to json.

 

I am not sure if posting it as an idea will help.

 

- Anup

nishwalnishwal

I dont mind to ignore the optional properties with 'null' values in json string but i am passing the json string to third party REST API that does not accept it. So what to do in this case?

 

I think,  the class contains a property as IsOptional kind of corresponding to each property to ignore properties with null/no values, So i thoght posting it as idea might work.