All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.json4s.jackson.Json.scala Maven / Gradle / Ivy

There is a newer version: 3.2.11
Show newest version
package org.json4s
package jackson

import com.fasterxml.jackson.databind.ObjectMapper
import java.io.{Writer => JWriter}

object Json {
  private class UtilMethods(override val mapper: ObjectMapper) extends JsonMethods
  def apply(fmts: Formats, mapper: ObjectMapper = JsonMethods.mapper) = new Json(fmts, mapper)

}
class Json(fmts: Formats, mapper: ObjectMapper = JsonMethods.mapper) extends JsonUtil(fmts) {
  private[this] val meth: JsonMethods = new Json.UtilMethods(mapper)

  def write[A <: AnyRef : Manifest](a: A): String = mapper.writeValueAsString(decompose(a))
  def write[A <: AnyRef : Manifest, W <: JWriter](a: A, out: W): W = {
    mapper.writeValue(out, decompose(a))
    out
  }

  def writePretty[A <: AnyRef](a: A): String =
    mapper.writerWithDefaultPrettyPrinter.writeValueAsString(decompose(a))

  def writePretty[A <: AnyRef, W <: JWriter](a: A, out: W): W = {
    mapper.writerWithDefaultPrettyPrinter.writeValue(out, decompose(a))
    out
  }

  def parse(json: JsonInput): JValue = meth.parse(json, fmts.wantsBigDecimal)
  def parseOpt(json: JsonInput): Option[JValue] = meth.parseOpt(json, fmts.wantsBigDecimal)

  def withFormats(fmts: Formats): JsonUtil = new Json(fmts, mapper)
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy