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

JSON return type for Handlers? #1481

Closed
MaxGabriel opened this issue Feb 2, 2018 · 3 comments
Closed

JSON return type for Handlers? #1481

MaxGabriel opened this issue Feb 2, 2018 · 3 comments

Comments

@MaxGabriel
Copy link
Member

So I was thinking about this blog post recently http://tech.frontrowed.com/2015/12/21/servant-style-handlers-for-yesod/, that uses UndecidableInstances to let you specify the return type of your Handlers that return JSON as Handler <RecordName> rather than Handler Value

Why not just have something like this?

data JSONResponse a = ToJSON a => JSONResponse a

instance ToContent (JSONResponse a) where
  toContent (JSONResponse a) = toContent $ toEncoding a

instance ToTypedContent (JSONResponse a) where
  toTypedContent = TypedContent typeJson . toContent

Then handlers are typed as:

postSignupR :: Handler (JSONResponse CreateUserResponse)

and the function returns:

return $ JSONResponse $ CreateUserResponse userId

Slightly more overhead than the blog post version. I don’t know if UndecidableInstances is actually worth avoiding though. Thoughts? I think endorsing one or the other of these options would be good for Yesod to do—it seems much better than returning Value.

@MaxGabriel
Copy link
Member Author

cc @gregwebs since you authored that blog post

@gregwebs
Copy link
Member

gregwebs commented Feb 9, 2018

Looks good

MaxGabriel added a commit that referenced this issue Mar 12, 2018
@MaxGabriel MaxGabriel mentioned this issue Mar 12, 2018
5 tasks
@StevenXL StevenXL mentioned this issue Apr 12, 2019
5 tasks
@StevenXL
Copy link
Member

Closed by #1592

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.

3 participants