Skip to content

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

Closed

Description

@tolysz

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...

Activity

  1. added a commit that references this issue on Dec 12, 2015
  2. added a commit that references this issue on Jan 15, 2016
  3. bergmark commented on Jan 23, 2016

    @bergmark
    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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions