Skip to content

Commit

Permalink
Refs #646: add json serialization to jsonparser
Browse files Browse the repository at this point in the history
  • Loading branch information
janehmueller committed Jun 6, 2018
1 parent 3fbfd83 commit 735153b
Showing 1 changed file with 46 additions and 1 deletion.
47 changes: 46 additions & 1 deletion src/main/scala/de/hpi/ingestion/dataimport/JSONParser.scala
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ package de.hpi.ingestion.dataimport
import java.text.SimpleDateFormat
import java.util.Date

import play.api.libs.json.{JsArray, JsObject, JsString, JsValue}
import de.hpi.ingestion.datalake.models.Subject
import de.hpi.ingestion.implicits._
import play.api.libs.json._

/**
* Trait to parse JSON-Objects. Contains all methods needed to parse JSON into Scala Objects.
Expand Down Expand Up @@ -141,3 +143,46 @@ trait JSONParser {
}
}
}

object JSONParser {
def toJson[T](data: T): JsValue = data match {
case x: JsValue => x
case x: String => this(x)
case x: Double => this(x)
case x: Int => this(x)
case x: Boolean => this(x)
case x: Subject => this(x)
case x: List[Any] => this(x)
case x: Map[Any, Any @unchecked] => this(x)
case x => this(x.toString)
}

def apply(data: String): JsValue = JsString(data)
def apply(data: Int): JsValue = JsNumber(data)
def apply(data: Double): JsValue = JsNumber(data)
def apply(data: Boolean): JsValue = JsBoolean(data)

def apply[T](data: List[T]): JsValue = {
JsArray(data.map(this.toJson))
}

def apply[K, V](data: Map[K, V]): JsValue = {
JsObject(data
.mapKeys(_.toString)
.mapValues(this.toJson)
)
}

def apply(data: Subject): JsValue = {
this(Map(
"id" -> this.toJson(data.id),
"master" -> this.toJson(data.master),
"datasource" -> this(data.datasource),
"name" -> this(data.name.getOrElse("")),
"aliases" -> this(data.aliases),
"category" -> this(data.category.getOrElse("")),
"properties" -> this(data.properties),
"relations" -> this(data.relations)
))
}
}

0 comments on commit 735153b

Please sign in to comment.