Permalink
Switch branches/tags
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
42 lines (34 sloc) 1.55 KB
package org.reactivecouchbase.rs.scaladsl.json
import com.couchbase.client.java.document.json.{JsonArray, JsonNull, JsonObject}
import play.api.libs.json._
private[json] object JsonConverter {
import collection.JavaConverters._
def convertJsonValue(value: JsValue): Any = value match {
case JsNull => JsonNull.INSTANCE
case JsString(s) => s
case JsBoolean(b) => b
case JsNumber(n) => n.bigDecimal
case JsArray(values) => values.foldLeft(JsonArray.create())((a, b) => a.add(convertJsonValue(b)))
case JsObject(values) => values.toSeq.foldLeft(JsonObject.create())((a, b) => a.put(b._1, convertJsonValue(b._2)))
case _ => throw new RuntimeException("Unknown type")
}
def convertToJson(value: JsObject): JsonObject =
value.value.toSeq.foldLeft(JsonObject.create())((a, b) => a.put(b._1, convertJsonValue(b._2)))
def convertToJsValue(value: Any): JsValue = value match {
case a: JsonObject => JsObject(a.toMap.asScala.toMap.mapValues(convertToJsValue))
case a: JsonArray => JsArray(a.toList.asScala.toIndexedSeq.map(convertToJsValue))
case a: Boolean => JsBoolean(a)
case a: Double => JsNumber(a)
case a: Long => JsNumber(a)
case a: Int => JsNumber(a)
case a: String => JsString(a)
case null => JsNull
case _ => throw new RuntimeException("Unknown type")
}
def safeConversion(json: AnyRef): JsValue = {
json match {
case s: String => Json.parse(s)
case a => convertToJsValue(a)
}
}
}