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

Comments

Projects
None yet
3 participants
@MaxGabriel
Copy link
Member

MaxGabriel commented Feb 2, 2018

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

This comment has been minimized.

Copy link
Member Author

MaxGabriel commented Feb 2, 2018

cc @gregwebs since you authored that blog post

@gregwebs

This comment has been minimized.

Copy link
Member

gregwebs commented Feb 9, 2018

Looks good

MaxGabriel added a commit that referenced this issue Mar 12, 2018

@MaxGabriel MaxGabriel referenced this issue Mar 12, 2018

Closed

Add JSONResponse type #1494

4 of 5 tasks complete

@StevenXL StevenXL referenced this issue Apr 12, 2019

Merged

Introduce JSONResponse. #1592

5 of 5 tasks complete
@StevenXL

This comment has been minimized.

Copy link
Member

StevenXL commented Apr 13, 2019

Closed by #1592

@StevenXL StevenXL closed this Apr 13, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.