Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Failed to parse field some-string: expected Text, encountered Null #325

Closed
tolysz opened this issue Dec 12, 2015 · 1 comment
Closed

Failed to parse field some-string: expected Text, encountered Null #325

tolysz opened this issue Dec 12, 2015 · 1 comment

Comments

@tolysz
Copy link
Contributor

tolysz commented Dec 12, 2015

I am testing the git.master version and got this error:

Object (fromList [("some-string",Null)])
aeson-example-simple: failed to parse field some-string: expected Text, encountered Null

I would expect that fromJSON would coerce Null to Nothing :: Maybe Text but it sees Null as a Type ( I tested it on both GHC 7.10.2 and 7.10.3 )

Which is a result of the following code:

{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE OverloadedStrings #-}

import Data.Aeson
import Data.Text

data Some = Some {someString :: Maybe Text}
  deriving Show

instance FromJSON Some where
  parseJSON = withObject "Some" $ \o -> do
        someString     <- o .:?  "some-string"
        return Some{..}

a :: Value
a = object ([("some-string",Null)])

main = do
   print a
   case fromJSON $ a of
       Error e -> error e
       Success some -> print (some :: Some)

and in .cabal

executable aeson-example-simple
  main-is: Simple.hs
  ghc-options: -Wall
  build-depends:
    aeson,
    base,
    bytestring,
    text

I will investigate it a bit longer but if you have anything to suggest It would be awesome...

tolysz added a commit to tolysz/aeson that referenced this issue Dec 12, 2015
tolysz added a commit to tolysz/aeson that referenced this issue Jan 15, 2016
@bergmark
Copy link
Collaborator

This seems to have been fixed. Currently on master:

λ> main
Object (fromList [("some-string",Null)])
Some {someString = Nothing}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants