From 735153bb6dd470d0185bf62eb2828f6796e491e4 Mon Sep 17 00:00:00 2001 From: Jan Ehmueller Date: Wed, 6 Jun 2018 17:44:15 +0200 Subject: [PATCH] Refs #646: add json serialization to jsonparser --- .../hpi/ingestion/dataimport/JSONParser.scala | 47 ++++++++++++++++++- 1 file changed, 46 insertions(+), 1 deletion(-) diff --git a/src/main/scala/de/hpi/ingestion/dataimport/JSONParser.scala b/src/main/scala/de/hpi/ingestion/dataimport/JSONParser.scala index 2e8d69d8..bc4d9e2f 100644 --- a/src/main/scala/de/hpi/ingestion/dataimport/JSONParser.scala +++ b/src/main/scala/de/hpi/ingestion/dataimport/JSONParser.scala @@ -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. @@ -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) + )) + } +}