3

There is a type:

data HandlerH f = StreamHandler { level     :: HKD T.Level f
                                -- ^ Default is @NOTSET@.
                                , filterer  :: HKD T.Filterer f
                                -- ^ Default is [].
                                , formatter :: HKD String f
                                -- ^ It represents key of 'ConfigH''s
                                -- formatters.
                                , stream    :: HKD String f
                                -- ^ Only support 'stderr' and 'stdout',
                                -- default is 'stderr'.
                                }

                | FileHandler { level     :: HKD T.Level f
                              -- ^ Default is @NOTSET@.
                              , filterer  :: HKD T.Filterer f
                              -- ^ Default is [].
                              , formatter :: HKD String f
                              -- ^ It represents key of 'ConfigH''s
                              -- formatters.
                              , file      :: FilePath
                              , encoding  :: HKD String f
                              -- ^ See 'System.IO.mkTextEncoding',
                              -- default is utf8.
                              }
                | RotatingFileHandler { level       :: HKD T.Level f
                                      -- ^ Default is @NOTSET@.
                                      , filterer    :: HKD T.Filterer f
                                      -- ^ Default is [].
                                      , formatter   :: HKD String f
                                      -- ^ It represents key of 'ConfigH''s
                                      -- formatters.
                                      , file        :: FilePath
                                      , encoding    :: HKD String f
                                      -- ^ See 'System.IO.mkTextEncoding',
                                      -- default is utf8.
                                      , maxBytes    :: HKD Int f
                                      -- ^ Default is 100 MB.
                                      , backupCount :: HKD Int f
                                      -- ^ Default is 10.
                                      }
                | TimeRotatingFileHandler { level       :: HKD T.Level f
                                          -- ^ Default is @NOTSET@.
                                          , filterer    :: HKD T.Filterer f
                                          -- ^ Default is [].
                                          , formatter   :: HKD String f
                                          -- ^ It represents key of 'ConfigH''s
                                          -- formatters.
                                          , file        :: FilePath
                                          , encoding    :: HKD String f
                                          , timezone    :: Maybe String
                                          -- ^ If not set, same as 'Manager''s
                                          -- timezone.
                                          , rotateTime  :: HKD T.RotateTime f
                                          -- ^ Indicates when to rotate file,
                                          -- e.g. @D3@ means every 3 days,
                                          -- @W4@ means at 0 clock of 'Thursday',
                                          -- try reading and showing
                                          -- 'RotateTime'.
                                          , backupCount :: HKD Int f
                                          }
                 -- ^ @since 0.7.0
                 deriving Generic

and I want to write a ToJSON instance for it in a simple and short way based on default ToJSON implementation and options, for example, its FromJSON uses them:

instance FromJSON (HandlerH Maybe) where
  parseJSON = genericParseJSON option
    where
      sumEncoding = defaultTaggedObject { tagFieldName = "type" }
      option = defaultOptions { sumEncoding = sumEncoding }

so something similar for ToJSON but I want to omit the field "filterer", so it will not appear in the JSON representation at all. Is it possible?

Options look like:

Options
{ fieldLabelModifier      = id
, constructorTagModifier  = id
, allNullaryToStringTag   = True
, omitNothingFields       = False
, sumEncoding             = defaultTaggedObject
, unwrapUnaryRecords      = False
, tagSingleConstructors   = False
, rejectUnknownFields     = False
}

but I don't see useful constructor for my use case... I expected something similar to omitNothingFields but as a [String] with names of fields to be omitted.

RandomB
  • 3,367
  • 19
  • 30
  • 1
    Standard proviso on "is it possible" questions: you should take a lack of answers as a "no". (If it's possible, somebody's likely to write an answer. But if it's not possible, it's very hard to be confident of that fact -- one can only ever be sure of "my imagination wasn't good enough to think of a way" -- so it feels dangerous to write an answer.) – Daniel Wagner Apr 28 '22 at 04:40

0 Answers0