
sjsonnew.SupportHasher.scala Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of sjson-new-core_2.13 Show documentation
Show all versions of sjson-new-core_2.13 Show documentation
A Scala library for JSON (de)serialization
The newest version!
package sjsonnew
import scala.util.Try
trait SupportHasher[J] {
implicit def facade: BuilderFacade[J]
def makeBuilder: Builder[J] = new Builder(facade)
/**
* Convert an object of type `A` to a JSON AST of type `J`.
*/
def hash[A](obj: A)(implicit writer: HashWriter[A]): Try[J] =
Try(hashUnsafe(obj)(writer))
/**
* Convert an object of type `A` to a JSON AST of type `J`.
* This might fail by throwing an exception.
*/
def hashUnsafe[A](obj: A)(implicit writer: HashWriter[A]): J =
{
val builder = makeBuilder
writer.write(obj, builder)
builder.result match {
case Some(r) => r
case _ => facade.jnull()
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy