{- |
Module:      MCP.Aeson
Copyright:   (c) DPella AB 2025
License:     MPL-2.0
Maintainer:  <matti@dpella.io>, <lobo@dpella.io>
-}
module MCP.Aeson where

import Data.Aeson
import Data.List (stripPrefix)

-- | Custom Aeson parsing options for MCP types.
mcpParseOpts :: Options
mcpParseOpts :: Options
mcpParseOpts =
    Options
defaultOptions
        { omitNothingFields = True
        , fieldLabelModifier = flm
        }
  where
    -- Custom field label modifier to handle "_meta" field
    flm :: String -> String
    flm :: String -> String
flm String
"_meta" = String
"_meta"
    -- Handle duplicate record fields
    flm String
fl
        | Just String
with_sel <- String -> String -> Maybe String
forall a. Eq a => [a] -> [a] -> Maybe [a]
stripPrefix String
"$sel:" String
fl =
            (Char -> Bool) -> String -> String
forall a. (a -> Bool) -> [a] -> [a]
takeWhile (Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
/= Char
':') String
with_sel
    flm String
fl = String
fl