module Mpv.Json where

import qualified Data.Aeson as Aeson
import Data.Aeson (FromJSON, SumEncoding (UntaggedValue), Value, camelTo2, fromJSON)
import Data.Aeson.TH (deriveJSON)
import Data.List (dropWhileEnd)
import qualified Language.Haskell.TH as TH

basicOptions :: Aeson.Options
basicOptions :: Options
basicOptions =
  Options
Aeson.defaultOptions {
    fieldLabelModifier :: String -> String
Aeson.fieldLabelModifier = (Char -> Bool) -> String -> String
forall a. (a -> Bool) -> [a] -> [a]
dropWhile (Char
'_' Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
==) (String -> String) -> (String -> String) -> String -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Char -> Bool) -> String -> String
forall a. (a -> Bool) -> [a] -> [a]
dropWhileEnd (Char
'_' Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
==)
  }

jsonOptions :: Aeson.Options
jsonOptions :: Options
jsonOptions =
  Options
basicOptions {
    unwrapUnaryRecords :: Bool
Aeson.unwrapUnaryRecords = Bool
True
  }

untaggedOptions :: Aeson.Options
untaggedOptions :: Options
untaggedOptions =
  Options
jsonOptions {
    sumEncoding :: SumEncoding
Aeson.sumEncoding = SumEncoding
UntaggedValue
  }

lowerMinusJson :: TH.Name -> TH.Q [TH.Dec]
lowerMinusJson :: Name -> Q [Dec]
lowerMinusJson =
  Options -> Name -> Q [Dec]
deriveJSON Options
jsonOptions {
    unwrapUnaryRecords :: Bool
Aeson.unwrapUnaryRecords = Bool
True,
    constructorTagModifier :: String -> String
Aeson.constructorTagModifier = Char -> String -> String
camelTo2 Char
'-'
  }

aesonToEither :: Aeson.Result a -> Either Text a
aesonToEither :: forall a. Result a -> Either Text a
aesonToEither = \case
  Aeson.Success a
a -> a -> Either Text a
forall a b. b -> Either a b
Right a
a
  Aeson.Error String
s -> Text -> Either Text a
forall a b. a -> Either a b
Left (String -> Text
forall a. ToText a => a -> Text
toText String
s)
{-# inline aesonToEither #-}

jsonDecodeValue ::
  FromJSON a =>
  Value ->
  Either Text a
jsonDecodeValue :: forall a. FromJSON a => Value -> Either Text a
jsonDecodeValue =
  (Text -> Text) -> Either Text a -> Either Text a
forall (p :: * -> * -> *) a b c.
Bifunctor p =>
(a -> b) -> p a c -> p b c
first Text -> Text
forall a. ToText a => a -> Text
toText (Either Text a -> Either Text a)
-> (Value -> Either Text a) -> Value -> Either Text a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Result a -> Either Text a
forall a. Result a -> Either Text a
aesonToEither (Result a -> Either Text a)
-> (Value -> Result a) -> Value -> Either Text a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Value -> Result a
forall a. FromJSON a => Value -> Result a
fromJSON
{-# inline jsonDecodeValue #-}