Skip to content

Commit

Permalink
Merge pull request #268 from tfausak/aeson-2.1
Browse files Browse the repository at this point in the history
Allow aeson 2.1
  • Loading branch information
tfausak authored Jun 18, 2022
2 parents 8453c05 + 5b5071e commit 20ed2d4
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 19 deletions.
2 changes: 1 addition & 1 deletion rattletrap.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ library

autogen-modules: Paths_rattletrap
build-depends:
, aeson ^>= 2.0.3
, aeson ^>= 2.0.3 || ^>= 2.1.0
, aeson-pretty ^>= 0.8.9
, array ^>= 0.5.4
, bytestring ^>= 0.11.3.1
Expand Down
6 changes: 4 additions & 2 deletions src/lib/Rattletrap/Schema.hs
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,12 @@ named n j = Schema { name = Text.pack n, json = j }
ref :: Schema -> Json.Value
ref s = Json.object [Json.pair "$ref" $ Text.pack "#/definitions/" <> name s]

object :: [((String, Json.Value), Bool)] -> Json.Value
object :: [((Json.Key, Json.Value), Bool)] -> Json.Value
object xs = Json.object
[ Json.pair "type" "object"
, Json.pair "properties" . Json.object $ fmap (uncurry Json.pair . fst) xs
, Json.pair "properties" . Json.object $ fmap
((\(k, v) -> Json.pair (Json.keyToString k) v) . fst)
xs
, Json.pair "required" . fmap (fst . fst) $ filter snd xs
]

Expand Down
22 changes: 6 additions & 16 deletions src/lib/Rattletrap/Utility/Json.hs
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
{- hlint ignore "Avoid restricted flags" -}
{-# OPTIONS_GHC -Wno-orphans #-}

{- hlint ignore "Avoid restricted extensions" -}
{-# LANGUAGE FlexibleInstances #-}

module Rattletrap.Utility.Json
( module Rattletrap.Utility.Json
, Aeson.FromJSON(parseJSON)
, Key.Key
, Aeson.ToJSON(toJSON)
, Aeson.Value
, Aeson.encode
Expand All @@ -22,28 +18,22 @@ import qualified Data.Aeson.Types as Aeson
import qualified Data.ByteString as ByteString
import qualified Data.ByteString.Lazy as LazyByteString

toKey :: String -> Key.Key
toKey = Key.fromString

fromKey :: Key.Key -> String
fromKey = Key.toString

instance Aeson.KeyValue (String, Aeson.Value) where
k .= v = (fromKey k, Aeson.toJSON v)
keyToString :: Key.Key -> String
keyToString = Key.toString

required
:: Aeson.FromJSON value => Aeson.Object -> String -> Aeson.Parser value
required object key = object Aeson..: toKey key
required object key = object Aeson..: Key.fromString key

optional
:: Aeson.FromJSON value
=> Aeson.Object
-> String
-> Aeson.Parser (Maybe value)
optional object key = object Aeson..:? toKey key
optional object key = object Aeson..:? Key.fromString key

pair :: (Aeson.ToJSON value, Aeson.KeyValue pair) => String -> value -> pair
pair key value = toKey key Aeson..= value
pair key value = Key.fromString key Aeson..= value

decode :: Aeson.FromJSON a => ByteString.ByteString -> Either String a
decode = Aeson.eitherDecodeStrict'
Expand Down

0 comments on commit 20ed2d4

Please sign in to comment.