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] Optionally suppress keys with null values #16

Open
kostmo opened this issue May 22, 2016 · 4 comments
Open

[feature] Optionally suppress keys with null values #16

kostmo opened this issue May 22, 2016 · 4 comments

Comments

@kostmo
Copy link

kostmo commented May 22, 2016

Apparently, such a feature is available in the Data.Aeson.TH module (see omitNothingFields), but that module requires that TemplateHaskell be enabled.

kostmo added a commit to kostmo/aeson-pretty that referenced this issue May 22, 2016
kostmo added a commit to kostmo/aeson-pretty that referenced this issue May 22, 2016
@informatikr
Copy link
Owner

This should be done in the ToJSON instance.

omitNothingFields exists for TemplateHaskell and DeriveGeneric. Or if you write your own ToJSON instance, you can do it there.

@kostmo
Copy link
Author

kostmo commented May 24, 2016

I do now see omitNothingFields in the Data.Aeson.Types module in addition to the Data.Aeson.TH module.

However, I currently have over 50 records (with this number expected to grow) which require serialization to JSON, so the cost of additional per-record boilerplate is high.

With my modification to aeson-pretty to suppress nulls, I am able to get by with only a single, short line per record to declare each as an instance of ToJson:

instance ToJSON MyModule.MyRecord

Unless I am missing something, without having aeson-pretty handle it, the per-record boilerplate increases to this:

instance ToJSON MyModule.MyRecord where
    toJSON = genericToJSON defaultOptions { omitNothingFields = True }

Although this is a 100% increase in lines for me (more in terms of tokens), perhaps this amount of boilerplate doesn't earn me any sympathy.

@chris-martin
Copy link

Wouldn't it make more sense to write a function

removeNullValues :: Value -> Value

and then encode your value with encodePretty . removeNullValues . toJSON instead of encodePretty?

@philderbeast
Copy link

Wouldn't it make more sense to write a function

The stripNulls example from aeson-extras does exactly this, phadej/aeson-extra#57.

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

No branches or pull requests

4 participants