aboutsummaryrefslogtreecommitdiffstats
path: root/src/Data/JLD/Model/ListObject.hs
blob: 6277d2443a735d5e4b90a69bbeaffa41eb37f6e0 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
module Data.JLD.Model.ListObject (isListObject, isListObject', isNotListObject, toListObject) where

import Data.JLD.Prelude

import Data.JLD.Model.Keyword (Keyword (..))

import Data.Aeson (Object, Value (..))
import Data.Aeson.KeyMap qualified as KM (member, singleton, size)

isListObject :: Value -> Bool
isListObject (Object o) = isListObject' o
isListObject _ = False

isListObject' :: Object -> Bool
isListObject' o =
    KM.member (show KeywordList) o
        && ( KM.size o == 1
                || (KM.size o == 2 && KM.member (show KeywordIndex) o)
           )

isNotListObject :: Value -> Bool
isNotListObject = isListObject .> not

toListObject :: Value -> Value
toListObject value@(Array _) = Object <| KM.singleton (show KeywordList) value
toListObject value = Object <| KM.singleton (show KeywordList) (Array <| pure value)