Skip to content
New issue

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

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

Already on GitHub? # to your account

Respondable Json instance should return json blob instead of a string #55

Closed
cdepillabout opened this issue Jan 7, 2016 · 1 comment · Fixed by #56
Closed

Respondable Json instance should return json blob instead of a string #55

cdepillabout opened this issue Jan 7, 2016 · 1 comment · Fixed by #56

Comments

@cdepillabout
Copy link
Member

The Respondable instance for Json looks like this:

responseTypeToString :: forall a. (ResponseType a) -> String
responseTypeToString ArrayBufferResponse = "arraybuffer"
responseTypeToString BlobResponse = "blob"
responseTypeToString DocumentResponse = "document"
responseTypeToString JSONResponse = "text" -- IE doesn't support "json" responseType
responseTypeToString StringResponse = "text"

type ResponseContent = Foreign

type F = Either ForeignError

class Respondable a where
  responseType :: Tuple (Maybe MimeType) (ResponseType a)
  fromResponse :: ResponseContent -> F a

instance responsableJson :: Respondable Json where
  responseType = Tuple (Just applicationJSON) JSONResponse
  fromResponse = Right <<< unsafeCoerce

Since responseTypeToString JSONResponse returns "text" instead of a "json", the fromResponse function is basically just wrapping a JavaScript string instead of a JSON blob.

This causes functions from Data.Argonaut.Decode (like toObject) to fail when they really should be succeeding.

I think the Respondable Json instance should look something like this:

import Data.Argonaut.Parser (jsonParser)

instance responsableJson :: Respondable Json where
  responseType = Tuple (Just applicationJSON) JSONResponse
  fromResponse = jsonParser <=< readString

This will give you a correct blob of JSON data and things like toObject will succeed.

@cdepillabout
Copy link
Member Author

It looks like this is what used to happen? But maybe it was changed?

https://github.com/slamdata/purescript-affjax/pull/25/files#diff-19be80dce5228e5284fe77a9c9bf847fR143

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Development

Successfully merging a pull request may close this issue.

1 participant