-
Notifications
You must be signed in to change notification settings - Fork 221
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
Pluggable JSON Libraries #78
Conversation
} | ||
} | ||
|
||
object JsonResponseConverter extends Service[JSONType, JsonResponse] { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it possible to get rid of this JsonResponseConverver
but having an implicit converter imported? It guess scala compiler will figure out that it has to use an implicit converter and return not JSONObject
but Json
. Could you please check it?
Thanks @rpless! That is definitely what I had in mind. The only concern from my side is an additional transformation layer. We have to get rid of it and it would be just perfect. |
Excellent catch! I changed the return type of the |
@@ -70,7 +67,7 @@ package object finch { | |||
|
|||
type HttpRequest = Request | |||
type HttpResponse = Response | |||
type JsonResponse = JSONType | |||
type JsonResponse = Json |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we also get rid of this? Just use Json
type directly.
Great then! That how I saw it: user imports required implicit converters for his JSON library and lets the magic happen. |
GetUserTickets(id) ! TurnModelIntoJson | ||
import scala.util.parsing.json.{JSONArray, JSONObject} | ||
|
||
object TurnModelIntoJson extends Service[Any, JsonResponse] { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is still a JsonResponse
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ugh, I though I had gotten them all. Give me a minute to fix.
Cool, thanks! The code looks good to me. The only question left is how we provide those @travisbrown, could you please review this? I would love to hear your opinion on this idea. I personally like the approach of |
@rodrigopr, could you also take a look? |
} | ||
} | ||
|
||
object Ticket extends Endpoint[HttpRequest, JsonResponse] { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should keep this endpoint example here (also the User
endpoint above).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice catch! I missed that. @rpless could you please keep this Ticket
endpoint?
Beside the changes in the documentation, it looks good to me too. 👍 @vkostyukov, @rpless, about the json conversion, one thing that i'm testing here is a We could use the same approach on the serialization: class TurnModelIntoJson[A](implicit encoder: EncodeJson[A]) extends Service[A, Json] {
def apply(obj: A) = {
val json = encoder(obj)
turn(req).toFuture
}
} But using new Is that what you guys are thinking? |
Using a pair of let's say trait EncodeJson[A] {
def apply[A](json: A): String
} Then class TurnJsonIntoHttp[A](implicit encode: EncodeJson) extends Service[A, HttpResponse] {
def apply(req: A) = {
val json = encode(req) // string
// wire string
}
} I like this approach by the way. The things are getting symmetric. We can user encode/decode for responses/requests. I like it. What do you think @rpless, @rodrigopr? |
Ok, fixed the docs. It looks like I overwrite the wrong example. But I agree with encode/decode implicit idea. Should we close this PR and start a new one or just keep adding things here? Also we'll have the same issue with implicit encode/decode that we did with this PR. I don't think Finch should provide them (because of the library dependencies). I think substantial sized converters should be separate repos and small ones (~5 lines) should just be written by the user. (Users of libraries like argonaut have to set up implicits anyway and jackson users have to write domain specific object mappers each time). |
I agree @vkostyukov, the Also agree with @rpless, we should close this PR and start a new one. |
Why don't you want to keep everything in a one thread? I agree with Ryan @rpless. The |
If we keep this PR that means we'll have all of the commits from above that we don't want in the repository's history. (Unless we're ok with rebasing this PR before merging it to master, then we can get rid of those commits) |
Alright. Let's start a new thread. Thanks guys @rpless, @rodrigopr for all your support. Special kudos for Ryan @rpless who did all the hard work! Ryan are you on Twitter? |
@vkostyukov I do now. https://twitter.com/ryan_plessner |
@rpless, @vkostyukov, by close I meant merge it if it's good to go. :-) |
Agreed. Let's merge it and start from here. |
A potential solution to #45 (it also increases test coverage to ~81% which benefits #76).
Basically, I removed most of the old json wrapper. Instead Finch users must implement either implicits or filters to convert their preferred Json representation to something that Finch can send in an HTTP response.
As examples, the implicit for the scala 2.10 library is included in the updated docs.
The implicit conversion for argonaut is similar. It basically just involves calling
asJson
instead oftoString
inside the implicit (after you have setup the implicit conversions for argonaut, of course).Jackson is a bit more complicated, but basically involves invoking an object mapper inside the implicit.
Because the nature of the conversion can be very dependent on the nature of the data (depending on library choice), I've left the conversion itself to Finch users to implement.
I would like any thoughts on this PR. I'm not sure its exactly what Finch needs for pluggable json libraries, but I think its a step in the right direction.