Haskell JSON Rest API — how to generically serialize meta information? - json

I'm creating a JSON REST API (lots of caps there), and already have Data.Aeson.Generic working nicely. In the following, serializedString would be {"x":10, "y":10}
import qualified Data.Aeson.Generic as A
import Data.Data (Data, Typeable)
data Unit = Unit { x :: Int, y :: Int } deriving (Show, Eq, Data, Typeable)
example = do
let serializedByteString = A.encode (Unit 10 10)
I would like to have my api respond like this for successes:
{unit:{x:10, y:10}}
And this for failures
{error:"Didn't work!"}
I was thinking of making some kind of Response data type, with Response and Error constructors. It's easy to serialize Error, but response could have all kinds of different objects, and rather than send back {data:{...}} I'd like to do {unit:{...}}.
Is there a way to define my Response value constructor so that it works with anything deriving Data? Is there a way to know what the name of the value constructor is when I go to serialize my object? show knows it somehow. Thanks!

Doesn't work for the Error constructor yet (I think), but this works for unit
{-# LANGUAGE OverloadedStrings, DeriveDataTypeable #-}
module Types where
import Data.Data (Data, Typeable, typeOf)
import Data.Aeson (ToJSON(..), (.=), object)
import qualified Data.Aeson.Generic as AG
import qualified Data.Text as T
import Data.Char (toLower)
data Unit = Unit { x :: Int, y :: Int } deriving (Show, Data, Typeable)
data Message a = Message { obj :: a } |
Error String
deriving (Show, Data, Typeable)
instance (Data a, Typeable a) => ToJSON (Message a) where
toJSON m = object [T.pack typeName .= AG.toJSON (obj m)]
where o = obj m
typeName = map toLower $ show $ typeOf o
You have to use Data.Aeson.encode, rather than Data.Aeson.Generic.encode on the top-level message. The key concept I was missing was typeOf from Data.Data.Typeable. It gives you a string representing the data constructor. It will be interesting to try to go the other direction using FromJSON
Edit: to be clear, you can then call Data.Aeson.encode $ Message $ Unit 10 10 and get {unit:{x:10, y:10}}

Related

FromJSON / ToJSON when some of the fields should be parsed manually

I have a data:
data MyData = MyData { a :: String, b :: Integer, c :: Bool }
deriving (Generic)
instance FromJSON MyData
instance ToJSON MyData
In fact, I have many more fields in MyData.
I want to parse 1 or 2 fields manually because in MyData they're called slightly different than in the real JSON object, while still being able to have FromJSON and ToJSON or something like that. Is it possible? Or should I in this case parse all the fields manually and not use FromJSON / ToJSON?
You'll want to take a look at the template Haskell deriving abilities of aeson. There is an option there which helps you rename fields. For example, say I want to rename the color field to colour in the declaration below:
data MyData = MyData { address :: String
, streetNumber :: Integer
, isApartment :: Bool
, color :: String
}
Then, instead of deriving Generic, I add the following
{-# LANGUAGE TemplateHaskell #-}
import Data.Aeson.TH
data MyData = MyData { address :: String
, streetNumber :: Integer
, isApartment :: Bool
, color :: String
}
$(deriveJSON defaultOptions{
constructorTagModifier = \f -> if f == "color" then "colour" else f
} ''MyData)
Then my ToJSON and FromJSON instances have appropriately named fields.

Python's json.dumps (or) loads in haskell-aeson?

In Aeson library meant for object serializing/deserializing, I see the functions, FromJSON & ToJSON declared as instances. The code is,
data Coord = Coord { x :: Double, y :: Double }
deriving (Show)
instance ToJSON Coord where
toJSON (Coord xV yV) = object [ "x" .= xV,
"y" .= yV ]
My questions are,
Why does the author create ToJSON/FromJSON instances with just one method? Can't toJSON/parseJSON be written as a function on its own?
In Python, one just does json.loads/json.dumps to handle any kind of object/json-string. Why does the haskell user need to write all these extra code for every object that he seralizes?
For composite objects with multiple hierarchies like
{"a":
{"b":
{
"c":1
}
}
}
, do we need to create multiple data and instance at each level?
Why does the author create ToJSON/FromJSON instances with just one method? Can't toJSON/parseJSON be written as a function on its own?
You misunderstand a lot of things, so let me clear this up a little. ToJSON and FromJSON aren't functions. These are typeclasses. Typeclasses are a way to write polymorphic code in Haskell.
Here I would explain a very simplified and incomplete definition of json serialization. First of all we declare a typeclass:
class ToJSON a where
toJSON :: a -> String
This statement basically says: "If a is an instance of typeclass ToJSON, then we can use function toJSON to serialize a into a JSON string".
When a typeclass is defined, one can implement instances of it for a variety of simple types:
instance ToJSON String where
toJSON s = s
instance ToJSON Int where
toJSON n = show n
instance ToJSON Double where
toJSON n = show n
After you defined these simple implementation, you can apply toJSON to values of either String, Int or Double and it would get dispatched to a right implementation:
toJSON "hello" -----> "hello"
toJSON (5 :: Int) -----> "5"
toJSON (5.5 :: Double) -----> "5.5"
To go further we need a way to encode JSON collections. Let's start with lists. We want to express that if there is a value a that can be serialized into JSON, then a list of such values can also be serialized into JSON.
-- ,-- value 'a' can be serialized into JSON
-- ,--------,
instance (ToJSON a) => ToJSON [a] where
-- ``````````-- A list of such values can also be serialized
-- | Here is how serialization can be performed
toJSON as = "[" ++ (intercalate ", " $ map toJSON as) ++ "]"
We serialize each value in the list, separate them with ", " and enclose in brackets. Note that recursive call to toJSON gets dispatched to the correct implementation.
Now we can use toJSON on lists:
toJSON [1,2,3,4] -----> "[1, 2, 3, 4]"
You can go further and try to implement the whole JSON syntax. Your next step here might be maps. I'll leave it as an exercise.
My point was to explain that when you write instance ToJSON Coord ... you simply provide a way to serialize Coord into JSON. And this gives you an ability to serialize lists of Coords, maps with Coords and many other things. This wouldn't be possible without typeclasses.
In Python, one just does json.loads/json.dumps to handle any kind of object/json-string. Why does the haskell user need to write all these extra code for every object that he seralizes?
An important point is that Python's json.loads wouldn't deserialize json into your object. It would deserialize it into a built in structure that might be equivalent to your object. You can do the same thing in Haskell by using template haskell which would declare ToJSON/FromJSON instances for you. Alternatively you can just dump the JSON into a key value Map and operate on it.
However, writing that extra code (or automatically generating it) gives you a lot of benefits which can be summarized with words "type safety".
For composite objects with multiple hierarchies like ..., do we need to create multiple data and instance at each level?
No you don't. In case of a structure that you linked the instances that would transform a number into such a structure or vice-versa would look approximately like this:
-- | Just a wrapper for the number which must be stored in a nested structure
newtype NestedStructure = NestedStructure Int
instance ToJSON NestedStructure where
toJSON (NestedStructure n) =
object ["a" .= object ["b" .= object ["c" .= n]]]
instance FromJSON NestedStructure where
fromJSON (Object o) = NestedStructure <$> ((o .: "a") >>= (.: "b")
>>= (.: "c"))
fromJSON _ = mzero
In Python, one just does json.loads/json.dumps to handle any kind of
object/json-string. Why does the haskell user need to write all these
extra code for every object that he seralizes?
One way to avoid this is to use the deriving mechanism in combination with GHC.Generics.
The "deriving" mechanism automatically generates typeclass instances for you, avoiding boilerplate. For example:
{-# LANGUAGE DeriveGeneric #-}
import GHC.Generics
data VDPServer = VDPServer
{ vdpHost :: String
, vdpPort :: Int
, vdpLogin :: String
, vdpPassword :: String
, vdpDatabase :: String
}
deriving Generic
instance FromJSON VDPServer
instance ToJSON VDPServer
This is descrived in the Type Conversion section of the documentation.
You can customize how the instances are generated using the Options type:
aesonOptions :: Options
aesonOptions = defaultOptions
{ sumEncoding = ObjectWithSingleField
, fieldLabelModifier = tail
, omitNothingFields = True
}
instance FromJSON VDPServer where
parseJSON = genericParseJSON aesonOptions
instance ToJSON VDPServer where
toJSON = genericToJSON aesonOptions
Sometimes, when dealing with complex preexisting JSON schemas, this approach doesn't work so well and one has to fall back to manually defining the parser. But for simpler cases it avoids you a lot of boilerplate.
do we need to create multiple data and instance at each level?
All of the record fields must have their own FromJSON/ToJSON instances. Many common types (tuples, lists, maps, strings...) already have such instances, see the instance list for FromJSON in the documentation. But if not, you'll have to define them (maybe using the Generic trick again).
In Python, one just does json.loads/json.dumps to handle any kind of
object/json-string. Why does the haskell user need to write all these
extra code for every object that he seralizes?
The Haskell equivalent of deserializing a JSON file into a composite of maps, lists, and privitive types is to read a Value object. This way one doesn't have to define a new record.

Is this legitimate use of "undefined" or are there better approaches to this?

I've been playing around with MySQL-Simple lately. Trying to come up with some comprehensive examples to that library. I wanted to separate IDs from the actual record I'm trying to store.
To implement the error case for QueryResults for this Entity datatype I need the amount of fields this datatype actually consumes. Therefore I introduced an Arity Typeclass (which is pretty easy to get wrong, especially when the data changes). I'm also using undefined here to call the correct instance of the arity method.
I've seen something like this in several examples and yet I feel uneasy about the use of undefined here. Are there better ways to do that?
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE BangPatterns #-}
module Main where
import Database.MySQL.Simple
import Database.MySQL.Simple.QueryResults
import Database.MySQL.Simple.QueryParams
import Database.MySQL.Simple.Result
import Database.MySQL.Simple.Param
connectInfo :: ConnectInfo
connectInfo = defaultConnectInfo { connectUser = "dbuser" }
data Person = Person { personName :: String
, personAge :: Int
} deriving Show
class Arity a where
arity :: a -> Int
instance Arity Person where
arity _ = 2
instance QueryResults Person where
convertResults [fname, fage] [vname, vage] =
Person name age
where
!name = convert fname vname
!age = convert fage vage
convertResults fs vs = convertError fs vs 2
instance QueryParams Person where
renderParams (Person name age) = [render name, render age]
data Entity a = Entity Int a
instance (Arity a, QueryResults a) => QueryResults (Entity a) where
convertResults (fid:fs) (vid:vs) =
Entity id value
where
!id = convert fid vid
!value = convertResults fs vs
convertResults fs vs =
convertError fs vs (1 + arity (undefined :: Person))
Kind regards, raichoo
If arity should only depend on the choice of a, i.e. its type, you should use a Proxy which is basically () with a phantom type tag:
class Arity a where
arity :: proxy a -> Int
This way, instances of Arity are forced to not try to look at the passed value, and clients don't need to pass a bottom:
instance Arity Person where
arity _ = 2
convertResults fs vs =
convertError fs vs (1 + arity (Proxy :: Proxy Person))
I think it's fine. If you want a non-undefined alternative, you can use a proxy phantom type:
data Proxy a = Proxy
class Arity a where
arity :: Proxy a -> Int
instance Arity Person where
arity _ = 2
convertResults fs vs =
convertError fs vs (1 + arity (Proxy :: Proxy Person))

Aeson and Lens with DeriveGeneric and makeLenses - names don't line up

Let's say I have a type Person
import GHC.Generics
import Data.Text
import Data.Aeson
import Control.Lens
data Person = Person {
_firstName :: Text,
_lastName :: Text,
_age :: Int
} deriving (Show, Generic)
And I want to automatically derive Lenses and JSON typeclasses for it
makeLenses ''Person
instance FromJSON Person
instance ToJSON Person
This works correctly, however DeriveGeneric sees my field names as having an underscore and expects my JSON to be formatted accordingly.
{ "_firstName": "James" ... etc} -- The underscore doesn't belong here.
Obviously I could remove the underscore from the data definition itself, but then makeLenses wouldn't be able to derive the required getters and setters.
Ideally what I want to be able to do is something like this
let person = decode blob
let name = person ^. firstName
i.e. I want to be able to derive lenses and JSON instances with all field names lining up correctly with the values in the JSON-REST Api I'm consuming, without having to write much boilerplate.
This seems like such a straight forward thing that I feel I'm missing something obvious?
Both lens and aeson have functions to allow customizable handling of field and constructor names. Since aeson's default is not what you want, and wouldn't work anyway if you want the lens names to be the same as the JSON field names, let's change the aeson configuration:
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE TemplateHaskell #-}
import GHC.Generics
import Data.Text hiding (drop)
import Data.Aeson
import Data.Aeson.TH
import Data.Aeson.Types
import Control.Lens
data Person = Person {
_firstName :: Text,
_lastName :: Text,
_age :: Int
} deriving (Show, Generic)
makeLenses ''Person
deriveJSON defaultOptions{fieldLabelModifier = drop 1} ''Person
{- alternative Generic version
instance FromJSON Person where
parseJSON = genericParseJSON defaultOptions{fieldLabelModifier = drop 1}
instance ToJSON Person where
toJSON = genericToJSON defaultOptions{fieldLabelModifier = drop 1}
-}
For lens, the corresponding configurable function would be makeLensesWith.

How can I use multiple 'Data' instance when one of them is 'deduced' from a type function?

I'm trying to make the following code work (well, compiling first!):
module Orexio.Radix where
import Data.Data
import Data.Map (Map)
import qualified Data.Map as Map
import Data.Typeable
import Text.JSON.Generic
class Resource a where
type Representation a :: *
identifier :: Resource a => Identifier a
class Endpoint a where
call :: Resource a => a -> Representation a
data Identifier a = Identifier [String] deriving (Show)
data Binding a = Binding (JSValue -> Either String JSValue)
bind :: (Data a, Resource a, Endpoint a, Data (Representation a)) => Binding a
bind = Binding (\x -> binding $ query x)
where binding query = fmap (\x -> toJSON $ call x) (resultToEither query)
query jsvalue = fromJSON jsvalue
{-- DEMO --}
data HelloWorld = HelloWorld {
name :: String
} deriving (Show, Typeable, Data)
instance Resource HelloWorld where
type Representation HelloWorld = String
identifier = Identifier ["helloworld"]
instance Endpoint HelloWorld where
call r = "Hello " ++ name r
So I had to enable FlexibleContexts to be able to do Data (Representation a), but still it's not working...
I have this error:
src/Orexio/Radix.hs:21:33:
Could not deduce (Data a0) arising from a use of `query'
from the context (Data a,
Resource a,
Endpoint a,
Data (Representation a))
bound by the type signature for
bind :: (Data a, Resource a, Endpoint a,
Data (Representation a)) =>
Binding a
at src/Orexio/Radix.hs:20:9-78
The type variable `a0' is ambiguous
Possible fix: add a type signature that fixes these type variable(s)
Note: there are several potential instances:
instance Data HelloWorld -- Defined at src/Orexio/Radix.hs:29:29
instance Data () -- Defined in `Data.Data'
instance (Data a, Data b) => Data (a, b) -- Defined in `Data.Data'
...plus 42 others
In the second argument of `($)', namely `query x'
In the expression: binding $ query x
In the first argument of `Binding', namely
`(\ x -> binding $ query x)'
Honestly I'm kinda of lost here, I must be missing something but what?
Here is the other extensions I have activated: DeriveDataTypeable, ExistentialQuantification, NoMonomorphismRestriction, TypeFamilies
Thanks in advance!
It looks like Binding a is supposed to be a function that transforms a value of type a to a value of type Representation a or an error message. Since the input and output are JSON-encoded, however, they both have type JSValue; their types don't mention a at all!
data Binding a = Binding (JSValue -> Either String JSValue)
There's no information to indicate what type those JSValues represent.
In the definition of bind, the compiler knows that the return type is Binding a, but there's no link between that type and the types of JSValues. In particular, the compiler cannot deduce that fromJSON should return an a and that toJSON should take a Representation a. To fix it, add explicit type signatures to binding and query.
One detail that sometimes confuses people is how to tell GHC about type variable scopes. This needs the ScopedTypeVariables extension. Add forall a. to the type signature of bind, and add forall. to the other type signatures in the body of bind so that the variable a is properly scoped.
The essence of the type error is this line: "The type variable `a0' is ambiguous".
(Disclaimer: I'm trying to avoid jargon in this answer.)
To understand what's going on here, I suggest floating the binding and query bindings to the top-level. They successfully typecheck if you then comment out the bind binding. GHC infers the following types.
*Orexio.Radix> :i query
query :: Data a => JSValue -> Result a
*Orexio.Radix> :i binding
binding ::
(Data (Representation a), Endpoint a, Resource a) =>
Result a -> Either String JSValue
Your error is essentially caused by the expression \x -> binding (query x) in the definition of bind. Notice that the type variable a appears only in the domain of binding and the range of query. Thus, when you compose them, two things happen.
The type variable is not determined; its "value" remains unknown.
The type variable is not reachable from the type of the
composition. For our informal purposes, that type is JSValue -> Either
String JSValue.
GHC raises the error because the type variable was not determined during the composition (ie 1) and it can never be determined in the future (a consequence of 2).
The general shape of this problem in Haskell is more commonly known as the "show-read problem"; search for "ambiguous" on Chapter 6 of Real World Haskell. (Some might also call it "too much polymorphism".)
As you and Sjoerd have determined (and the RWH chapter explains), you can fix this type error by ascribing a type to the result of query before applying binding. Without knowing your semantics, I assume that you intend this "hidden" type variable a to be the same as the argument to the Binding type constructor. So the following would work.
bind :: forall b.
(Data b, Resource b, Endpoint b, Data (Representation b)) => Binding b
bind = Binding (\x -> binding $ (query x :: Result b))
This ascription eliminates the type variable a by replacing it entirely with Result b. Note that unlike a it is acceptable for b to remain undetermined, since it's reachable in the top-level type; uses of bind may each determine b.
That first solution requires giving bind an explicit signature — which can sometimes be quite onerous. In this case, due to the monomorphism restriction, you probably already need that type signature. However, if bind took an argument (but still exhibited this ambiguous type variable error), you could still rely on type inference by using a solution like the following.
dummy_ResultType :: Binding a -> Result a
dummy_ResultType = error "dummy_ResultType"
bind () = result where
result = Binding (\x -> binding $ (query x `asTypeOf` dummy))
dummy = dummy_ResultType result
(If the use of error worries you, cf the Proxy type.)
Or trade some idiomaticness for directness:
withArgTypeOf :: f x -> g x -> f x
withArgTypeOf x _ = x
bind () = result where
result = Binding (\x -> binding (query x `withArgTypeOf` result))
Now inference works.
*Orexio.Radix> :i bind
bind ::
(Data (Representation a), Data a, Endpoint a, Resource a) =>
() -> Binding a
Rest assured that GHC quickly determines after typechecking that the definition is not actually recursive.
HTH.