org.json4s.native.Serialization.writePretty Scala Examples

The following examples show how to use org.json4s.native.Serialization.writePretty. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example.
Example 1
Source File: scalaUse.scala    From tscfg   with Apache License 2.0 5 votes vote down vote up
package tscfg.example

import java.io.File

import com.typesafe.config.{ConfigRenderOptions, ConfigFactory}


object scalaUse {

  def main(args: Array[String]): Unit = {
    val configFilename = args.headOption.getOrElse("src/main/tscfg/example/example.conf")
    println(s"Loading $configFilename")
    val configFile = new File(configFilename)

    // usual Typesafe Config mechanism to load the file
    val tsConfig = ConfigFactory.parseFile(configFile).resolve

    // create instance of the tscfg generated main class. This will
    // perform all validations according to required properties and types:
    val cfg = ScalaExampleCfg(tsConfig)

    // access the configuration properties in a type-safe fashion while also
    // enjoying your IDE features for code completion, navigation, etc:
    val path: String         = cfg.endpoint.path
    val url: String          = cfg.endpoint.url
    val serial: Option[Int]  = cfg.endpoint.serial
    val port: Int            = cfg.endpoint.interface.port
    val typ : Option[String] = cfg.endpoint.interface.`type`

    println("\n*** tscfg case class structure: *** ")
    println("  " + cfg.toString.replaceAll("\n", "\n  "))
    println("\n  *** in JSON format: *** ")
    println("  " + toJson(cfg).toString.replaceAll("\n", "\n  "))

    println("\n*** Typesafe rendering of input Config object: *** ")
    val options: ConfigRenderOptions = ConfigRenderOptions.defaults
      .setFormatted(true).setComments(true).setOriginComments(false)
    println("  " + tsConfig.root.render(options).replaceAll("\n", "\n  "))
  }

  def toJson(cfg: ScalaExampleCfg): String = {
    import org.json4s._
    import org.json4s.native.Serialization
    import org.json4s.native.Serialization.writePretty
    implicit val formats = Serialization.formats(NoTypeHints)
    writePretty(cfg)
  }
} 
Example 2
Source File: TnCmd.scala    From TopNotch   with Apache License 2.0 5 votes vote down vote up
package com.bfm.topnotch.tnengine

import java.io.{PrintWriter, StringWriter}

import org.json4s._
import org.json4s.native.Serialization
import org.json4s.native.Serialization.writePretty

/**
 * A command for TnEngine to run
 */
abstract class TnCmd {
  
  val outputKey: String
  /** Whether to cache the resulting dataframe in memory. This should be a boolean defaulting to false,
    * but json4s has a problem with default values other than None for option. Change it to a default value if json4s
    * solves the bug. */
  val cache: Option[Boolean]
  
  val outputPath: Option[String]
  /** If writing the output in hdfs, the name of the table to mount, otherwise none. Note: this will be ignored if
    * outputPath is not specified. */
  val tableName: Option[String]

  implicit val formats = Serialization.formats(NoTypeHints)
  /**
    * Overriding toString to making output of unit tests that have cmds in error logs easier to understand
    */
  override def toString = writePretty(this)
}

/**
 * The input to a command
 * @param ref The reference to the data set, either the path on hdfs or the name in the lookup table
 * @param onDisk Whether the input data set is stored on disk
 * @param delimiter The delimiter for plain text, delimited files. Leave to empty string for parquet.
 */
case class Input(ref: String, onDisk: Boolean, delimiter: Option[String] = None)

/**
 * The strings used for converting a config file into a TnCmd
 */
object TnCmdStrings {
  val ioNamespace = "io"
  val commandListStr = "commands"
  val writerStr = "writer"
  val commandStr = "command"
  val paramsStr = "params"
  val externalParamsStr = "externalParamsFile"
  val outputKeyStr = "outputKey"
  val writeToDiskStr = "writeToDisk"
  val outputPathStr = "outputPath"
}

/**
 * The class indicating that there was at least one error in the configuration for this command
 * @param cmdString The JSON string for the command.
 * @param errorStr The errors encountered in creating this command.
 * @param cmdIdx The index of the command in the plan that failed
 * @param outputKey This is meaningless in this class. This exists only so that TnErrorCmd can extend TnCmd.
 * @param writeToDisk This is meaningless in this class. This exists only so that TnErrorCmd can extend TnCmd.
 * @param outputPath This is meaningless in this class. This exists only so that TnErrorCmd can extend TnCmd.
 */
case class TnErrorCmd (
                            cmdString: String,
                            errorStr: String,
                            cmdIdx: Int,
                            outputKey: String = "",
                            cache: Option[Boolean] = None,
                            writeToDisk: Boolean = false,
                            outputPath: Option[String] = None,
                            tableName: Option[String] = None
                            ) extends TnCmd {
  override def toString: String = {
    s"There was an error with the command in position ${cmdIdx} in its plan. The command was: \n ${cmdString} \n " +
      s"The message was: \n ${errorStr} \n\n END OF ERROR MESSAGE FOR COMMAND IN POSITION ${cmdIdx} \n\n"
  }
}

object TnErrorCmd {
  /**
    * Helper method for easily getting the stack trace of an exception as a string
    * @param e The exception
    * @return The exception's stack trace
    */
  def getExceptionStackTrace(e: Exception): String = {
    val sw = new StringWriter
    e.printStackTrace(new PrintWriter(sw))
    sw.toString
  }
} 
Example 3
Source File: SaveLiveData.scala    From scaladex   with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
package ch.epfl.scala.index
package data
package elastic

import model._
import project._
import org.json4s._
import org.json4s.native.Serialization.{read, write, writePretty}
import org.json4s.native.parseJson
import java.nio.file._
import java.nio.charset.StandardCharsets
import org.slf4j.LoggerFactory

import scala.concurrent.{ExecutionContext, Future}

// this allows us to save project as json object sorted by keys
case class LiveProjects(projects: Map[Project.Reference, ProjectForm])
object LiveProjectsSerializer
    extends CustomSerializer[LiveProjects](
      format =>
        (
          {
            case JObject(obj) => {
              implicit val formats = DefaultFormats
              LiveProjects(
                obj.map {
                  case (k, v) =>
                    val List(organization, repository) = k.split('/').toList

                    (Project.Reference(organization, repository),
                     v.extract[ProjectForm])
                }.toMap
              )
            }
          }, {
            case l: LiveProjects =>
              JObject(
                l.projects.toList
                  .sortBy {
                    case (Project.Reference(organization, repository), _) =>
                      (organization, repository)
                  }
                  .map {
                    case (Project.Reference(organization, repository), v) =>
                      import ch.epfl.scala.index.search.SearchProtocol._
                      JField(s"$organization/$repository", parseJson(write(v)))
                  }
              )
          }
      )
    )

trait LiveProjectsProtocol {
  implicit val formats: Formats = DefaultFormats ++ Seq(LiveProjectsSerializer)
  implicit val serialization: Serialization = native.Serialization
}

object SaveLiveData extends LiveProjectsProtocol {

  val logger = LoggerFactory.getLogger(getClass)

  def storedProjects(paths: DataPaths): Map[Project.Reference, ProjectForm] =
    read[LiveProjects](
      Files
        .readAllLines(paths.liveProjects)
        .toArray
        .mkString("")
    ).projects

  def saveProjects(paths: DataPaths,
                   live: Map[Project.Reference, ProjectForm]): Unit = {
    val projects = LiveProjects(live)

    val liveDir = paths.liveProjects.getParent
    if (!Files.isDirectory(liveDir)) {
      Files.createDirectory(liveDir)
    }

    Files.write(
      paths.liveProjects,
      writePretty(projects).getBytes(StandardCharsets.UTF_8)
    )
  }

  // Note: we use a future here just to catch exceptions. Our code is blocking, though.
  def saveProject(project: Project,
                  paths: DataPaths)(implicit ec: ExecutionContext): Future[_] =
    Future {
      concurrent.blocking {
        val stored = SaveLiveData.storedProjects(paths)
        val newProject = (project.reference -> ProjectForm(project))

        logger.info(s"Writing projects at ${paths.liveProjects}")
        saveProjects(paths, stored + newProject)
      }
    }

} 
Example 4
Source File: JsonProtocol.scala    From midas   with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
//See LICENSE for license details.

package midas.passes.fame

import midas.widgets.SerializableBridgeAnnotation

import firrtl.annotations._

import scala.util.{Try, Failure}

import org.json4s._
import org.json4s.native.JsonMethods._
import org.json4s.native.Serialization
import org.json4s.native.Serialization.{read, writePretty}

case class DeserializationTypeHintsAnnotation(typeTags: Seq[String]) extends NoTargetAnnotation

trait HasSerializationHints {
  // For serialization of complicated constuctor arguments, let the bridge
  // designer specify additional type hints for relevant classes that might be
  // contained within
  def typeHints(): Seq[Class[_]]
}

object JsonProtocol {
  import firrtl.annotations.JsonProtocol._

  
  def serialize(annos: Seq[Annotation]): String = serializeTry(annos).get

  def serializeTry(annos: Seq[Annotation]): Try[String] = {
    val tags = annos.flatMap({
      case anno: HasSerializationHints => anno.getClass +: anno.typeHints
      case other => Seq(other.getClass)
    }).distinct

    implicit val formats = jsonFormat(classOf[DeserializationTypeHintsAnnotation] +: tags)
    Try(writePretty(DeserializationTypeHintsAnnotation(tags.map(_.getName)) +: annos))
  }

  def deserialize(in: JsonInput): Seq[Annotation] = deserializeTry(in).get

  def deserializeTry(in: JsonInput): Try[Seq[Annotation]] = Try({
    val parsed = parse(in)
    val annos = parsed match {
      case JArray(objs) => objs
      case x => throw new InvalidAnnotationJSONException(
        s"Annotations must be serialized as a JArray, got ${x.getClass.getSimpleName} instead!")
    }
    // Gather classes so we can deserialize arbitrary Annotations
    val classes = annos.flatMap({
      case JObject(("class", JString(typeHintAnnoName)) :: ("typeTags", JArray(classes)) :: Nil) =>
          typeHintAnnoName +: classes.collect({ case JString(className) => className })
      case JObject(("class", JString(c)) :: tail) => Seq(c)
      case obj => throw new InvalidAnnotationJSONException(s"Expected field 'class' not found! $obj")
    }).distinct
    val loaded = classes.map(Class.forName(_).asInstanceOf[Class[_ <: Annotation]])

    implicit val formats = jsonFormat(loaded)
    read[List[Annotation]](in)
  }).recoverWith {
    // Translate some generic errors to specific ones
    case e: java.lang.ClassNotFoundException =>
      Failure(new AnnotationClassNotFoundException(e.getMessage))
    case e: org.json4s.ParserUtil.ParseException =>
      Failure(new InvalidAnnotationJSONException(e.getMessage))
  }.recoverWith { // If the input is a file, wrap in InvalidAnnotationFileException
    case e => in match {
      case FileInput(file) =>
        Failure(new InvalidAnnotationFileException(file, e))
      case _ => Failure(e)
    }
  }
} 
Example 5
Source File: PFADocument.scala    From aardpfark   with Apache License 2.0 5 votes vote down vote up
package com.ibm.aardpfark.pfa.document

import com.ibm.aardpfark.pfa.dsl._
import com.ibm.aardpfark.pfa.expression.PFAExpression
import com.ibm.aardpfark.pfa.utils.Utils
import org.apache.avro.Schema
import org.json4s.native.Serialization
import org.json4s.native.Serialization.{write, writePretty}
import org.json4s.{FieldSerializer, NoTypeHints}

trait ToPFA {
  def pfa: PFADocument
}

trait HasAction {
  protected def action: PFAExpression
}

trait HasModelCell {
  protected def modelCell: NamedCell[_]
}

case class PFADocument(
  name: Option[String] = None,
  version: Option[Long] = Some(1L),
  doc: Option[String] = Some(s"Auto-generated by Aardpfark at ${Utils.getCurrentDate}"),
  metadata: Map[String, String] = Map(),
  // options,
  input: Schema,
  output: Schema,
  // begin: Seq[String] = Seq(),
  // end: Seq[String] = Seq(),
  // method: String = "map",
  action: Seq[PFAExpression],
  cells: Map[String, Cell[_]] = Map(),
  // pools
  fcns: Map[String, FunctionDef] = Map()
  // randseed
  // zero
  // merge
  ) {

  implicit val formats = Serialization.formats(NoTypeHints) +
    new SchemaSerializer +
    new PFAExpressionSerializer +
    new ParamSerializer +
    new FieldSerializer[Cell[_]] +
    new TreeSerializer

  def toJSON(pretty: Boolean = false) = {
    if (pretty) writePretty(this) else write(this)
  }
} 
Example 6
Source File: DeserializationRoundtripSpec.scala    From twitter4s   with Apache License 2.0 5 votes vote down vote up
package com.danielasfregola.twitter4s.entities

import com.danielasfregola.twitter4s.helpers.{FixturesSupport, JsonDiffSupport}
import org.json4s.native.Serialization.writePretty
import org.json4s.native.{JsonParser, Serialization}
import org.json4s.{JNothing, JValue}
import org.specs2.matcher.{Expectable, Matcher}
import org.specs2.mutable.Specification
import org.specs2.specification.core.Fragment

import scala.reflect._

class DeserializationRoundtripSpec extends Specification with FixturesSupport with JsonDiffSupport {

  "JSON deserialization" should {

    def roundtripTest[T <: AnyRef: Manifest](jsonFile: String): Fragment = {

      val className = classTag[T].runtimeClass.getSimpleName

      s"round-trip successfully for $className in $jsonFile" in {
        val originalJson = load(jsonFile)

        val deserializedEntity = Serialization.read[T](originalJson)

        val serializedJson = Serialization.writePretty[T](deserializedEntity)

        originalJson must beASubsetOfJson(serializedJson)
      }
    }

    roundtripTest[User]("/twitter/rest/users/user.json")
  }

  def beASubsetOfJson(otherJson: String): Matcher[String] = new Matcher[String] {

    def apply[S <: String](t: Expectable[S]) = {
      val alpha: JValue = JsonParser.parse(t.value)
      val beta: JValue = JsonParser.parse(otherJson)

      jsonDiff(alpha, beta) match {
        case diff @ JsonDiff(JNothing, _, JNothing) =>
          success(s"""${t.value}
               |is a subset of
               |$otherJson
               |${renderDiff(diff)}
             """.stripMargin,
                  t)
        case diff =>
          failure(s"""${t.value}
               |is not a subset of
               |$otherJson
               |${renderDiff(diff)}
             """.stripMargin,
                  t)
      }
    }

    private def renderDiff(diff: JsonDiff) = {
      val changed = diff.changed.toOption.map { c =>
        s"""Changed:
           |${writePretty(c)}
          """.stripMargin
      }
      val deleted = diff.deleted.toOption.map { d =>
        s"""Deleted:
           |${writePretty(d)}
          """.stripMargin
      }
      val added = diff.added.toOption.map { a =>
        s"""Added:
           |${writePretty(a)}
          """.stripMargin
      }

      (changed ++ deleted ++ added).mkString
    }
  }
} 
Example 7
Source File: BillerCache.scala    From apple-of-my-iap   with MIT License 5 votes vote down vote up
package com.meetup.iap

import com.meetup.iap.receipt.Subscription
import org.slf4j.LoggerFactory

import java.io.File
import scala.io.Source

import org.json4s.DefaultFormats
import org.json4s.native.Serialization.{read, writePretty}
import org.apache.commons.io.FileUtils


object BillerCache {
  val log = LoggerFactory.getLogger(BillerCache.getClass)

  implicit val formats = DefaultFormats

  private val ProjectName = "iap-service"
  private val inProject = new File(".").getCanonicalPath.endsWith(ProjectName)

  private val Folder = {
    val base = if(inProject) "" else "iap-service/"
    new File(s"${base}tmp/")
  }
  if(!Folder.exists) {
    Folder.mkdirs
  }

  private val TempFile = new File(Folder, "subscriptions.json")
  if(!TempFile.exists) {
    TempFile.createNewFile
  }

  private val PlansFile = new File(Folder, "plans.json")
  if (!PlansFile.exists) {
    PlansFile.createNewFile
  }

  def readFromCache(): Map[String, Subscription] = {
    log.info("Reading from file: " + TempFile.getAbsolutePath)
    val raw = Source.fromFile(TempFile).mkString.trim

    if(raw.nonEmpty) {
        Map(read[Map[String, Subscription]](raw).toSeq: _*)
    } else Map.empty
  }

  def writeToCache(subs: Map[String, Subscription]) {
      val json = writePretty(subs)
      FileUtils.writeStringToFile(TempFile, json, "UTF-8")
  }

  def readPlansFromFile(): List[Plan] = {
    log.info(s"Reading from plans file: ${PlansFile.getAbsolutePath}")
    val raw = Source.fromFile(PlansFile).mkString.trim

    if(raw.nonEmpty) {
      log.info("Found some plans")
      List(read[List[Plan]](raw).toSeq: _*)
    } else List.empty
  }
}