Skip to content

Commit 6a26bab

Browse files
author
Charles LADARI
committed
change decode signature to be more customizable
1 parent f4ce9e5 commit 6a26bab

File tree

2 files changed

+8
-7
lines changed

2 files changed

+8
-7
lines changed

src/main/scala/com/github/celadari/jsonlogicscala/core/Decoder.scala

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package com.github.celadari.jsonlogicscala.core
22

3-
import play.api.libs.json.{JsValue, Reads}
3+
import play.api.libs.json.{JsObject, JsValue, Reads}
44

55
object Decoder {
66
implicit val defaultDecoder: Decoder = new Decoder {
@@ -12,8 +12,12 @@ object Decoder {
1212
abstract class Decoder {
1313
def customDecode(json: JsValue, otherType: String)(implicit reads: Array[Reads[_]]): Any
1414

15-
def decode(jsValue: JsValue, `type`: String)(implicit reads: Array[Reads[_]] = Array()): ValueLogic[_] = {
16-
val value = `type` match {
15+
def decode(jsonLogic: JsObject, jsonLogicData: JsObject)(implicit reads: Array[Reads[_]] = Array()): ValueLogic[_] = {
16+
val typeData = (jsonLogic \ "type").as[String]
17+
val pathData = (jsonLogic \ "var").as[String]
18+
val jsValue = (jsonLogicData \ pathData).get
19+
20+
val value = typeData match {
1721
case "byte" => jsValue.as[Byte]
1822
case "short" => jsValue.as[Short]
1923
case "int" => jsValue.as[Int]

src/main/scala/com/github/celadari/jsonlogicscala/core/ValueLogic.scala

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,7 @@ object ValueLogic {
1010
}
1111

1212
private[core] def decode(jsonLogic: JsObject, jsonLogicData: JsObject)(implicit decoder: Decoder): ValueLogic[_] = {
13-
val typeData = (jsonLogic \ "type").as[String]
14-
val pathData = (jsonLogic \ "var").as[String]
15-
val json = (jsonLogicData \ pathData).get
16-
ValueLogic("var", decoder.decode(json, typeData))
13+
ValueLogic("var", decoder.decode(jsonLogic, jsonLogicData))
1714
}
1815

1916
implicit def valueLogicReads[T](implicit fmt: Reads[T]): Reads[ValueLogic[T]] = new Reads[ValueLogic[T]] {

0 commit comments

Comments
 (0)