I'm trying to encode a Base64 to JSON. I came across this dialogue. I noticed snoyberg has already included the Base64
newtype
in the FP Complete codebase, so I thought I would give that a try.
import qualified Data.ByteString as B
import qualified Data.ByteString.Base64 as B64
newtype Base64 = Base64 { toByteString :: B.ByteString }
deriving ( Eq, Ord, Show,IsString)
The ToJSON
instance seemed simple enough. I'd appreciate a sanity check.
instance ToJSON Base64 where toJSON (Base64 bs) =
toJSON $ B.unpack $ B64.decodeLenient bs
The FromJSON instance is where I run into problems.
Studying other examples I surmise I must use withArray
which wants a (Array -> Parser a)
. This is where I get stuck.
parseBase64 :: Array -> Parser
parseBase64 (Array a) = ...
I've tried many approaches here, I'm confused as to what needs to happen here or even if I'm on the right track at all. If I could get some feedback as simple as "you're on the right track keep going", or being pointed in a different direction, that would be appreciated.