Skip to content

Commit ccddf2e

Browse files
committed
add API Doc to deserialize package
1 parent f95ef60 commit ccddf2e

File tree

12 files changed

+184
-0
lines changed

12 files changed

+184
-0
lines changed

src/main/scala/com/celadari/jsonlogicscala/deserialize/Deserializer.scala

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// Copyright 2019 celadari. All rights reserved. MIT license.
12
package com.celadari.jsonlogicscala.deserialize
23

34
import play.api.libs.json.{JsArray, JsLookupResult, JsNull, JsObject, JsValue}
@@ -7,8 +8,18 @@ import com.celadari.jsonlogicscala.tree.types.{AnyTypeValue, ArrayTypeValue, Map
78
import com.celadari.jsonlogicscala.tree.{ComposeLogic, JsonLogicCore, ValueLogic}
89

910

11+
/**
12+
* Companion object holding implicit deserializer.
13+
* Useful to invoke methods using implicit [[com.celadari.jsonlogicscala.deserialize.Deserializer]] that do not require
14+
* custom Deserializer.
15+
*/
1016
object Deserializer {
1117

18+
/**
19+
* Returns [[com.celadari.jsonlogicscala.tree.types.TypeValue]] from json input.
20+
* @param jsLookupResult: input json representing a type.
21+
* @return TypeValue.
22+
*/
1223
def unmarshType(jsLookupResult: JsLookupResult): Option[TypeValue] = {
1324
if (jsLookupResult.isDefined) {
1425
try {
@@ -23,10 +34,33 @@ object Deserializer {
2334
implicit val defaultDeserializer: Deserializer = new Deserializer()
2435
}
2536

37+
/**
38+
* Responsible for deserializing json into scala [[com.celadari.jsonlogicscala.tree.JsonLogicCore]] data structure.
39+
* May be extended to fit custom use cases. Providing the right configuration via
40+
* [[com.celadari.jsonlogicscala.deserialize.DeserializerConf]] is enough to cover most cases.
41+
* You may redefine methods to handle extreme uncommon cases.
42+
* @param conf: informs of mappings (type_codename -> unmarshaller).
43+
*/
2644
class Deserializer(implicit val conf: DeserializerConf) {
45+
/**
46+
* Maps type_codename to [[com.celadari.jsonlogicscala.deserialize.Unmarshaller]].
47+
* @note More specifically, keys should be type_codename of [[com.celadari.jsonlogicscala.tree.types.SimpleTypeValue]]
48+
* as generic types (OptionTypeValue, MapTypeValue, ArrayTypeValue) are handled recursively by getMarshaller.
49+
*/
2750
protected[this] val unmarshallers: Map[String, Unmarshaller] = if (conf.isPriorityToManualAdd) conf.unmarshallerMetaInfAdd ++ conf.unmarshallersManualAdd
2851
else conf.unmarshallersManualAdd ++ conf.unmarshallerMetaInfAdd
2952

53+
/**
54+
* Returns [[com.celadari.jsonlogicscala.deserialize.Unmarshaller]] associated with input typeValue.
55+
* If input typeValue is [[com.celadari.jsonlogicscala.tree.types.SimpleTypeValue]] then returns mapped value
56+
* by unmarshallers attribute.
57+
* If input typeValue is [[com.celadari.jsonlogicscala.tree.types.OptionTypeValue]],
58+
* [[com.celadari.jsonlogicscala.tree.types.ArrayTypeValue]], [[com.celadari.jsonlogicscala.tree.types.MapTypeValue]]
59+
* then a new [[com.celadari.jsonlogicscala.deserialize.Unmarshaller]] is recursively created by checking paramType of
60+
* input typeValue.
61+
* @param typeValue: input type to return associated Unmarshaller.
62+
* @return Unmarshaller associated to typeValue.
63+
*/
3064
protected[this] def getUnmarshaller(typeValue: TypeValue): Unmarshaller = {
3165
typeValue match {
3266
case SimpleTypeValue(codename) => unmarshallers.getOrElse(codename, throw new IllegalInputException(s"No unmarshaller defined for $codename"))
@@ -50,6 +84,12 @@ class Deserializer(implicit val conf: DeserializerConf) {
5084
}
5185
}
5286

87+
/**
88+
* Returns [[com.celadari.jsonlogicscala.tree.ValueLogic]] by combining logic from jsonLogic and data from jsonLogicData.
89+
* @param jsonLogic: logic.
90+
* @param jsonLogicData: data values.
91+
* @return ValueLogic.
92+
*/
5393
protected[this] def deserializeValueLogic(jsonLogic: JsObject, jsonLogicData: JsObject): ValueLogic[_] = {
5494
val isTypeDefined = (jsonLogic \ "type").isDefined
5595
val typeValueOpt = Deserializer.unmarshType(jsonLogic \ "type")
@@ -73,6 +113,12 @@ class Deserializer(implicit val conf: DeserializerConf) {
73113
ValueLogic(valueOpt, typeValueOpt, variableNameOpt, pathDataOpt)
74114
}
75115

116+
/**
117+
* Returns [[com.celadari.jsonlogicscala.tree.ComposeLogic]] by combining logic from jsonLogic and data from jsonLogicData.
118+
* @param jsonLogic: logic.
119+
* @param jsonLogicData: data values.
120+
* @return ComposeLogic.
121+
*/
76122
protected[this] def deserializeComposeLogic(jsonLogic: JsObject, jsonLogicData: JsObject): ComposeLogic = {
77123
// check for operator field
78124
val fields = jsonLogic.fields
@@ -90,6 +136,13 @@ class Deserializer(implicit val conf: DeserializerConf) {
90136
new ComposeLogic(operator, deserializeArrayOfConditions((jsonLogic \ operator).get, jsonLogicData))
91137
}
92138

139+
/**
140+
* Returns array of [[com.celadari.jsonlogicscala.tree.JsonLogicCore]] from tuple of serialized (logic, data).
141+
* Logic is assumed to be an array of JsValue (JsArray) and each member of array is deserialized.
142+
* @param json: JsValue representing array of logics.
143+
* @param jsonLogicData: data values.
144+
* @return array of [[com.celadari.jsonlogicscala.tree.JsonLogicCore]].
145+
*/
93146
protected[this] def deserializeArrayOfConditions(json: JsValue, jsonLogicData: JsObject): Array[JsonLogicCore] = {
94147
val jsArray = json.asInstanceOf[JsArray]
95148
jsArray
@@ -100,6 +153,12 @@ class Deserializer(implicit val conf: DeserializerConf) {
100153
.toArray
101154
}
102155

156+
/**
157+
* Returns [[com.celadari.jsonlogicscala.tree.JsonLogicCore]] by combining logic from jsonLogic and data from jsonLogicData.
158+
* @param jsonLogic: logic.
159+
* @param jsonLogicData: data values.
160+
* @return JsonLogicCore.
161+
*/
103162
// scalastyle:off return
104163
def deserialize(jsonLogic: JsObject, jsonLogicData: JsObject): JsonLogicCore = {
105164
// check for operator field

src/main/scala/com/celadari/jsonlogicscala/deserialize/DeserializerConf.scala

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// Copyright 2019 celadari. All rights reserved. MIT license.
12
package com.celadari.jsonlogicscala.deserialize
23

34
import org.apache.xbean.finder.ResourceFinder
@@ -7,7 +8,14 @@ import com.celadari.jsonlogicscala.configuration.ConfigurationFetcher
78
import com.celadari.jsonlogicscala.deserialize.defaults._
89

910

11+
/**
12+
* Companion object to hold implicit: [[com.celadari.jsonlogicscala.deserialize.DeserializerConf]], mapping of default
13+
* unmarshallers (type_codename -> Unmarshaller), method to create a custom configuration.
14+
*/
1015
object DeserializerConf {
16+
/**
17+
* Maps default type_codenames to default unmarshallers.
18+
*/
1119
val DEFAULT_UNMARSHALLERS: Map[String, Unmarshaller] = Map(
1220
BOOL_CODENAME -> UnmarshallerBoolean,
1321
DOUBLE_CODENAME -> UnmarshallerDouble,
@@ -20,6 +28,13 @@ object DeserializerConf {
2028
NULL_CODENAME -> UnmarshallerNull
2129
)
2230

31+
/**
32+
* Returns a custom deserializer configuration.
33+
* @param path: folder to provider-configuration files.
34+
* @param unmarshallersManualAdd: map of (type_codename -> Unmashaller) to be added to the deserializer configuration.
35+
* @param isPriorityToManualAdd: boolean indicating if unmarshallers manually added have priority over those fetched from provider-configuration folder.
36+
* @return custom deserializer configuration.
37+
*/
2338
def createConf(
2439
path: String = "META-INF/services/",
2540
unmarshallersManualAdd: Map[String, Unmarshaller] = DEFAULT_UNMARSHALLERS,
@@ -34,6 +49,13 @@ object DeserializerConf {
3449
implicit val deserializerConf: DeserializerConf = createConf()
3550
}
3651

52+
/**
53+
* Represents a deserializer's configuration.
54+
* It informs the deserializer how to map a type_codename to a [[com.celadari.jsonlogicscala.deserialize.Unmarshaller]].
55+
* @param unmarshallerMetaInfAdd: map of (type_codename -> Unmarshaller) fetched from META-INF resources folder.
56+
* @param unmarshallersManualAdd: map of (type_codename -> Unmarshaller) manually added to the configuration.
57+
* @param isPriorityToManualAdd: boolean indicating if unmarshallers manually added have priority over those fetched from META-INF folder.
58+
*/
3759
case class DeserializerConf(
3860
unmarshallerMetaInfAdd: Map[String, Unmarshaller],
3961
unmarshallersManualAdd: Map[String, Unmarshaller],

src/main/scala/com/celadari/jsonlogicscala/deserialize/Unmarshaller.scala

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,23 @@
1+
// Copyright 2019 celadari. All rights reserved. MIT license.
12
package com.celadari.jsonlogicscala.deserialize
23

34
import scala.reflect.runtime.{universe => ru}
45
import play.api.libs.json.JsValue
56

67

8+
/**
9+
* Defines interface of class/object defining unmarshal method.
10+
* This method deserializes a specific scala data structure to json format.
11+
*/
712
trait Unmarshaller {
13+
14+
/**
15+
* Deserializes a json to a specific scala data structure.
16+
* @param jsValue: json to be deserialized into scala data structure.
17+
* @return scala data structure value.
18+
* @note serialized value of basic types (Int, String, Boolean, ...) are the values themselves, custom defined classes
19+
* are usually serialized to map of (attribute -> value).
20+
*/
821
def unmarshal(jsValue: JsValue): Any
922

1023
// scalastyle:off return

src/main/scala/com/celadari/jsonlogicscala/deserialize/defaults/UnmarshallerBoolean.scala

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,21 @@
1+
// Copyright 2019 celadari. All rights reserved. MIT license.
12
package com.celadari.jsonlogicscala.deserialize.defaults
23

34
import play.api.libs.json.{JsBoolean, JsValue}
45
import com.celadari.jsonlogicscala.deserialize.Unmarshaller
56
import com.celadari.jsonlogicscala.exceptions.InvalidJsonParsingException
67

78

9+
/**
10+
* Default unmarshaller that converts json format boolean value to scala boolean value.
11+
*/
812
object UnmarshallerBoolean extends Unmarshaller {
13+
14+
/**
15+
* Converts json boolean value to scala boolean value.
16+
* @param jsValue: json to deserialized to Boolean value.
17+
* @return scala boolean value.
18+
*/
919
def unmarshal(jsValue: JsValue): Any = {
1020
jsValue match {
1121
case JsBoolean(bool) => bool

src/main/scala/com/celadari/jsonlogicscala/deserialize/defaults/UnmarshallerByte.scala

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,21 @@
1+
// Copyright 2019 celadari. All rights reserved. MIT license.
12
package com.celadari.jsonlogicscala.deserialize.defaults
23

34
import play.api.libs.json.{JsNumber, JsValue}
45
import com.celadari.jsonlogicscala.deserialize.Unmarshaller
56
import com.celadari.jsonlogicscala.exceptions.InvalidJsonParsingException
67

78

9+
/**
10+
* Default unmarshaller that converts json format byte value to scala byte value.
11+
*/
812
object UnmarshallerByte extends Unmarshaller{
13+
14+
/**
15+
* Converts json byte value to scala byte value.
16+
* @param jsValue: json to deserialized to Byte value.
17+
* @return scala byte value.
18+
*/
919
def unmarshal(jsValue: JsValue): Any = {
1020
jsValue match {
1121
case JsNumber(num) => num.toByte

src/main/scala/com/celadari/jsonlogicscala/deserialize/defaults/UnmarshallerDouble.scala

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,21 @@
1+
// Copyright 2019 celadari. All rights reserved. MIT license.
12
package com.celadari.jsonlogicscala.deserialize.defaults
23

34
import play.api.libs.json.{JsNumber, JsValue}
45
import com.celadari.jsonlogicscala.deserialize.Unmarshaller
56
import com.celadari.jsonlogicscala.exceptions.InvalidJsonParsingException
67

78

9+
/**
10+
* Default unmarshaller that converts json format double value to scala double value.
11+
*/
812
object UnmarshallerDouble extends Unmarshaller{
13+
14+
/**
15+
* Converts json double value to scala double value.
16+
* @param jsValue: json to deserialized to Double value.
17+
* @return scala double value.
18+
*/
919
def unmarshal(jsValue: JsValue): Any = {
1020
jsValue match {
1121
case JsNumber(num) => num.toDouble

src/main/scala/com/celadari/jsonlogicscala/deserialize/defaults/UnmarshallerFloat.scala

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,21 @@
1+
// Copyright 2019 celadari. All rights reserved. MIT license.
12
package com.celadari.jsonlogicscala.deserialize.defaults
23

34
import play.api.libs.json.{JsNumber, JsValue}
45
import com.celadari.jsonlogicscala.deserialize.Unmarshaller
56
import com.celadari.jsonlogicscala.exceptions.InvalidJsonParsingException
67

78

9+
/**
10+
* Default unmarshaller that converts json format float value to scala float value.
11+
*/
812
object UnmarshallerFloat extends Unmarshaller{
13+
14+
/**
15+
* Converts json float value to scala float value.
16+
* @param jsValue: json to deserialized to Float value.
17+
* @return scala float value.
18+
*/
919
def unmarshal(jsValue: JsValue): Any = {
1020
jsValue match {
1121
case JsNumber(num) => num.toFloat

src/main/scala/com/celadari/jsonlogicscala/deserialize/defaults/UnmarshallerInt.scala

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,21 @@
1+
// Copyright 2019 celadari. All rights reserved. MIT license.
12
package com.celadari.jsonlogicscala.deserialize.defaults
23

34
import play.api.libs.json.{JsNumber, JsValue}
45
import com.celadari.jsonlogicscala.deserialize.Unmarshaller
56
import com.celadari.jsonlogicscala.exceptions.InvalidJsonParsingException
67

8+
9+
/**
10+
* Default unmarshaller that converts json format int value to scala int value.
11+
*/
712
object UnmarshallerInt extends Unmarshaller{
13+
14+
/**
15+
* Converts json int value to scala int value.
16+
* @param jsValue: json to deserialized to Int value.
17+
* @return scala int value.
18+
*/
819
def unmarshal(jsValue: JsValue): Any = {
920
jsValue match {
1021
case JsNumber(num) => num.toInt

src/main/scala/com/celadari/jsonlogicscala/deserialize/defaults/UnmarshallerLong.scala

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,21 @@
1+
// Copyright 2019 celadari. All rights reserved. MIT license.
12
package com.celadari.jsonlogicscala.deserialize.defaults
23

34
import play.api.libs.json.{JsNumber, JsValue}
45
import com.celadari.jsonlogicscala.deserialize.Unmarshaller
56
import com.celadari.jsonlogicscala.exceptions.InvalidJsonParsingException
67

78

9+
/**
10+
* Default unmarshaller that converts json format long value to scala long value.
11+
*/
812
object UnmarshallerLong extends Unmarshaller{
13+
14+
/**
15+
* Converts json long value to scala long value.
16+
* @param jsValue: json to deserialized to Long value.
17+
* @return scala long value.
18+
*/
919
def unmarshal(jsValue: JsValue): Any = {
1020
jsValue match {
1121
case JsNumber(num) => num.toLong

src/main/scala/com/celadari/jsonlogicscala/deserialize/defaults/UnmarshallerNull.scala

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,21 @@
1+
// Copyright 2019 celadari. All rights reserved. MIT license.
12
package com.celadari.jsonlogicscala.deserialize.defaults
23

34
import play.api.libs.json.{JsNull, JsValue}
45
import com.celadari.jsonlogicscala.deserialize.Unmarshaller
56
import com.celadari.jsonlogicscala.exceptions.InvalidJsonParsingException
67

78

9+
/**
10+
* Default unmarshaller that converts json format null value to scala null value.
11+
*/
812
object UnmarshallerNull extends Unmarshaller {
913

14+
/**
15+
* Converts json null value to scala null value.
16+
* @param jsValue: json to deserialized to Null value.
17+
* @return scala null value.
18+
*/
1019
// scalastyle:off null
1120
def unmarshal(jsValue: JsValue): Any = {
1221
jsValue match {

0 commit comments

Comments
 (0)