-
Notifications
You must be signed in to change notification settings - Fork 8
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
Provide play-json <-> circe conversions #28
Provide play-json <-> circe conversions #28
Conversation
play-json-circe/src/main/scala/com/evolutiongaming/util/PlayCirceConversions.scala
Outdated
Show resolved
Hide resolved
jsonObject = o => PlayJson.JsObject(o.toIterable.map { case (k, v) => (k, circeToPlay(v)) }.toSeq), | ||
) | ||
|
||
def playToCirce(value: PlayJson.JsValue): CirceJson = |
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.
tailrec?
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.
circeToPlay
is not in tail position
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.
this is a question - we either decide that we are not going to support ~1000 nested elements or try to rewrite so it could be tailrec-ed
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.
Well, I don't know.
It's another part which was not changed compared to original solution and worked so far without blowing the stack.
If we keep it, then both these conversions deserve disclaimer about safety in scaladoc.
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.
Suggestion:
@tailrec
private def playToCirceSeq(seq: scala.collection.IndexedSeq[PlayJson.JsValue], acc: List[CirceJson] = Nil): Iterable[CirceJson] = {
seq.headOption match {
case Some(value) =>
playToCirceSeq(seq.tail, playToCirce(value) :: acc)
case None =>
acc.reverse
}
}
@tailrec
private def playToCirceKeyValue(list: List[(String, JsValue)], acc: List[(String, CirceJson)] = Nil): Iterable[(String, CirceJson)] = {
list match {
case head :: tail =>
val (k, v) = head
playToCirceKeyValue(tail, (k, playToCirce(v)) :: acc)
case Nil =>
acc.reverse
}
}
def playToCirce(value: PlayJson.JsValue): CirceJson = {
value match {
case PlayJson.JsNull =>
CirceJson.Null
case PlayJson.JsTrue =>
CirceJson.True
case PlayJson.JsFalse =>
CirceJson.False
case PlayJson.JsNumber(value) =>
CirceJson.fromBigDecimal(value)
case PlayJson.JsString(value) =>
CirceJson.fromString(value)
case PlayJson.JsArray(value) =>
CirceJson.fromValues(playToCirceSeq(value))
case PlayJson.JsObject(value) =>
CirceJson.fromFields(playToCirceKeyValue(value.toList))
}
}
Data structures used could be changed (e.g. to avoid List.reverse
).
No description provided.