Add stand-alone deriving for ToJSON in Haskell - json

I wanted to write this data type:
data GameState = GameState {
players :: [Player],
lasers :: [(Float, Float, Float)],
asteroids :: [Asteroid],
score :: Int,
width :: Int,
height :: Int,
keys :: S.Set Key,
lastTimeAsteroidAdded :: Int,
screen :: Int,
mouseclicks :: S.Set (Float, Float)
}
deriving (Show, Generic, ToJSON)
to a JSON file. Therefor I needed to let GameState be derived from ToJSON, but now it says I need to create a stand-alone deriving instance for Key. But how do I do that?
The message that I got:
No instance for (ToJSON Key)
arising from the 'deriving' clause of a data type declaration
Possible fix:
use a standalone 'deriving instance' declaration,
so you can specify the instance context yourself
• When deriving the instance for (ToJSON GameState)

The solution to your problem is rather simple. Since you have no access to Key (it is defined in Gloss) and Key does not derive Generic, you first have to derive an instance of Generic for Key:
deriving instance Generic Key
This does require the usage of the following extra extension:
{-# LANGUAGE StandaloneDeriving #-}
Now that you have an Generic instance for Key, an instance for ToJSON can be defined:
instance TOJSON Key

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.

How to implement toJSON for an assoc-list producing an object with key-values pairs generically (using Aeson)?

I have data which is a map. To make the question more concrete, let's think that it's represented as an assoc-list type D val = [(Key,val)] (or as type D val = Map Key val).
Key is an "enum" type -- a sum of nullary constructors, e.g.:
data Key = C1 | C2 | C3
There must be instance ToJSON for val.
I'd like to implement
instance ToJSON val => ToJSON (D val)
which would be like instance Map String val (producing an object with the corresponding key-value pairs) (an example, another example for HashMaps), only more generic.
More generic means that I'm not restricted to String or Text as the key (such instances for Map are found in the aeson package).
I want to learn how to use the generic mechanisms in aeson to do this concisely.
aeson already can convert "enums" to JSON strings, and also (C val1 val2 ...) to objects with a single key (the constructor name, like this I assume: { "C":[val1,val2,...] }) with genericToJSON defaultOptions{ sumEncoding = ObjectWithSingleField }. And another conversion which uses the constructor names is with the default options for data types with non-nullary constructors: then the constructor name becomes the value of a "tag" key. I could use similar code.
I think that even such an instance (for maps whose keys are from enums) would be useful addition to the library. But AFAIU neither aeson nor generic-aeson (which has adds some nicer conversion options) has such a conversion predefined.
A quick and short, but ugly solution is:
instance (ToJSON a) => ToJSON (D a)
where toJSON kvs = object [ t .= v | (k,v) <- kvs, let (String t) = toJSON k ]
Advantages:
it re-uses the generic mechanisms of aeson to get the key representation (as text);
it would re-use the same conversion rules for the names of the fields here as defined in the instance ToJSON Key, so we don't need to write them twice, or to care about consistency.
Disadvantages:
converting first to Object just to extract a String is simply ugly;
it introduces a dangerous non-total conversion (which even may fail if toJSON for Key is implemented in a non-default way;
there is no compile-time check that Key is an enum relying on types. (Actually, I'm not very keen on understanding the generics type machinery yet, so I see that such checks and choices can be done because aeson is implemented this way, but I wouldn't be able to write this myself yet.)

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.

Creating polymorphic functions in Haskell

A short search didn't help me to find the answer, so I started to doubt in its existance. The question is simple. I want to create a polymorphic function, something like this:
f :: String -> String
f s = show (length s)
f :: Int -> String
f i = show i
A function defined differently for different data types is meant. Is it possible? If yes, how?
There are two flavors of polymorphism in Haskell:
parameteric polymorphism; and
bounded polymorphism
The first is the most general -- a function is parametrically polymorphic if it behaves uniformly for all types, in at least one of its type parameters.
For example, the function length is polymorphic -- it returns the length of a list, no matter what type is stored in its list.
length :: [a] -> Int
The polymorphism is indicated by a lower case type variable.
Now, if you have custom behavior that you want to have for a certain set of types, then you have bounded polymorphism (also known as "ad hoc"). In Haskell we use type classes for this.
The class declares which function will be available across a set of types:
class FunnyShow a where
funnyshow :: a -> String
and then you can define instances for each type you care about:
instance FunnyShow Int where
funnyshow i = show (i+1)
and maybe:
instance FunnyShow [Char] where
funnyshow s = show (show s)
Here is how you can achieve something similar using type families.
Well if you have same return types then you can achieve the behaviour without using type families and just using type classes alone as suggested by Don.
But it is better to use type families when you want to support more complex adhoc polymorphism, like different return types for each instance.
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE TypeSynonymInstances #-}
{-# LANGUAGE TypeFamilies #-}
class F a where
type Out a :: *
f :: a -> Out a
instance F String where
type Out String = String
f = show . length
instance F Int where
type Out Int = String
f = show
instance F Float where
type Out Float = Float
f = id
In ghci
*Main> f (2::Int)
"2"
*Main> f "Hello"
"5"
*Main> f (2::Float)
2.0