diff options
Diffstat (limited to 'src/Language/Fiddle/Internal/Scopes.hs')
-rw-r--r-- | src/Language/Fiddle/Internal/Scopes.hs | 19 |
1 files changed, 19 insertions, 0 deletions
diff --git a/src/Language/Fiddle/Internal/Scopes.hs b/src/Language/Fiddle/Internal/Scopes.hs index eea4c6f..ac6f7d1 100644 --- a/src/Language/Fiddle/Internal/Scopes.hs +++ b/src/Language/Fiddle/Internal/Scopes.hs @@ -3,6 +3,8 @@ module Language.Fiddle.Internal.Scopes where import Control.Monad (forM) +import Data.Aeson +import Data.Aeson.Key import Data.List (inits, intercalate) import Data.List.NonEmpty (NonEmpty (..), prependList) import Data.Map (Map) @@ -64,6 +66,9 @@ upsertScope (s :| (a : as)) v (Scope ss sv) = (replaced, subscope') = upsertScope (a :| as) v subscope in (replaced, Scope (Map.insert s subscope' ss) sv) +insertScope :: (Ord k) => NonEmpty k -> t -> Scope k t -> Scope k t +insertScope p s = snd . upsertScope p s + -- insertScope :: (Ord k) => NonEmpty k -> t -> Scope k t -> Scope k t -- insertScope a b = snd . upsertScope a b @@ -108,3 +113,17 @@ lookupScopeWithPath (ScopePath current others) key scope = case lookupScope (prependList prefix key) scope of Just s -> [(prependList prefix key, s)] Nothing -> [] + +instance (ToJSONKey k, ToJSON v, Ord k) => ToJSON (Scope k v) where + toJSON (Scope subScopes scopeValues) = + object + [ fromString "subScopes" .= toJSON subScopes, + fromString "scopeValues" .= toJSON scopeValues + ] + +instance (FromJSONKey k, FromJSON v, Ord k) => FromJSON (Scope k v) where + parseJSON (Object v) = + Scope + <$> v .: fromString "subScopes" + <*> v .: fromString "scopeValues" + parseJSON _ = fail "Expected an object for Scope" |