@@ -6,6 +6,7 @@ import com.fasterxml.jackson.databind.node.*
66import org.json.JSONArray
77import org.json.JSONObject
88import java.time.Instant
9+ import kotlin.math.*
910
1011
1112/* *
@@ -44,6 +45,25 @@ object GSJson {
4445 }
4546 }
4647
48+ /* *
49+ * Getting JSON value from string and selection path with fallback default
50+ *
51+ * @param json: as string
52+ * @param selection: The selection string
53+ * @param defaultValue: Default value to return if path doesn't exist or is null
54+ *
55+ * @return value according to the selection path or default value
56+ */
57+ fun get (json : String , selection : String , defaultValue : Any ): Any {
58+ inputType = DataType .STRING
59+ val result = if (isJsonLinesSelection(selection)) {
60+ handleJsonLines(json, selection)
61+ } else {
62+ parseAndGet(objectMapper.readTree(json), selection)
63+ }
64+ return result ? : defaultValue
65+ }
66+
4767 /* *
4868 * Getting JSON result from string and selection path
4969 *
@@ -70,6 +90,21 @@ object GSJson {
7090 inputType = DataType .GSON
7191 return parseAndGet(objectMapper.readTree(json.toString()), selection)
7292 }
93+
94+ /* *
95+ * Getting JSON value from JSONObject and selection path with fallback default
96+ *
97+ * @param json: as JSONObject
98+ * @param selection: The selection string
99+ * @param defaultValue: Default value to return if path doesn't exist or is null
100+ *
101+ * @return value according to the selection path or default value
102+ */
103+ fun get (json : JSONObject , selection : String , defaultValue : Any ): Any {
104+ inputType = DataType .GSON
105+ val result = parseAndGet(objectMapper.readTree(json.toString()), selection)
106+ return result ? : defaultValue
107+ }
73108
74109 /* *
75110 * Getting JSON value from JSONArray and selection path
@@ -83,6 +118,21 @@ object GSJson {
83118 inputType = DataType .GSON
84119 return parseAndGet(objectMapper.readTree(json.toString()), selection)
85120 }
121+
122+ /* *
123+ * Getting JSON value from JSONArray and selection path with fallback default
124+ *
125+ * @param json: as JSONArray
126+ * @param selection: The selection string
127+ * @param defaultValue: Default value to return if path doesn't exist or is null
128+ *
129+ * @return value according to the selection path or default value
130+ */
131+ fun get (json : JSONArray , selection : String , defaultValue : Any ): Any {
132+ inputType = DataType .GSON
133+ val result = parseAndGet(objectMapper.readTree(json.toString()), selection)
134+ return result ? : defaultValue
135+ }
86136
87137 /* *
88138 * Getting JSON value from JsonNode and selection path
@@ -96,6 +146,21 @@ object GSJson {
96146 inputType = DataType .JACKSON
97147 return parseAndGet(json, selection)
98148 }
149+
150+ /* *
151+ * Getting JSON value from JsonNode and selection path with fallback default
152+ *
153+ * @param json: JsonNode (Jackson JsonNode)
154+ * @param selection: The selection string
155+ * @param defaultValue: Default value to return if path doesn't exist or is null
156+ *
157+ * @return value according to the selection path or default value
158+ */
159+ fun get (json : JsonNode , selection : String , defaultValue : Any ): Any {
160+ inputType = DataType .JACKSON
161+ val result = parseAndGet(json, selection)
162+ return result ? : defaultValue
163+ }
99164
100165
101166 /* *
@@ -1080,6 +1145,176 @@ object GSJson {
10801145 }
10811146 else -> json
10821147 }
1148+ " @multiply" , " @mul" -> when (json) {
1149+ is ArrayNode -> {
1150+ val multiplier = argument?.toDoubleOrNull() ? : 1.0
1151+ val resultArray = objectMapper.createArrayNode()
1152+ json.forEach { node ->
1153+ val value = when {
1154+ node.isNumber -> node.asDouble() * multiplier
1155+ node.isTextual -> (node.asText().toDoubleOrNull() ? : 0.0 ) * multiplier
1156+ else -> 0.0
1157+ }
1158+ resultArray.add(value)
1159+ }
1160+ resultArray
1161+ }
1162+ else -> {
1163+ val multiplier = argument?.toDoubleOrNull() ? : 1.0
1164+ val value = when {
1165+ json.isNumber -> json.asDouble() * multiplier
1166+ json.isTextual -> (json.asText().toDoubleOrNull() ? : 0.0 ) * multiplier
1167+ else -> 0.0
1168+ }
1169+ objectMapper.valueToTree(value)
1170+ }
1171+ }
1172+ " @divide" , " @div" -> when (json) {
1173+ is ArrayNode -> {
1174+ val divisor = argument?.toDoubleOrNull() ? : 1.0
1175+ if (divisor == 0.0 ) return json // Avoid division by zero
1176+ val resultArray = objectMapper.createArrayNode()
1177+ json.forEach { node ->
1178+ val value = when {
1179+ node.isNumber -> node.asDouble() / divisor
1180+ node.isTextual -> (node.asText().toDoubleOrNull() ? : 0.0 ) / divisor
1181+ else -> 0.0
1182+ }
1183+ resultArray.add(value)
1184+ }
1185+ resultArray
1186+ }
1187+ else -> {
1188+ val divisor = argument?.toDoubleOrNull() ? : 1.0
1189+ if (divisor == 0.0 ) return json // Avoid division by zero
1190+ val value = when {
1191+ json.isNumber -> json.asDouble() / divisor
1192+ json.isTextual -> (json.asText().toDoubleOrNull() ? : 0.0 ) / divisor
1193+ else -> 0.0
1194+ }
1195+ objectMapper.valueToTree(value)
1196+ }
1197+ }
1198+ " @add" , " @plus" -> when (json) {
1199+ is ArrayNode -> {
1200+ val addend = argument?.toDoubleOrNull() ? : 0.0
1201+ val resultArray = objectMapper.createArrayNode()
1202+ json.forEach { node ->
1203+ val value = when {
1204+ node.isNumber -> node.asDouble() + addend
1205+ node.isTextual -> (node.asText().toDoubleOrNull() ? : 0.0 ) + addend
1206+ else -> addend
1207+ }
1208+ resultArray.add(value)
1209+ }
1210+ resultArray
1211+ }
1212+ else -> {
1213+ val addend = argument?.toDoubleOrNull() ? : 0.0
1214+ val value = when {
1215+ json.isNumber -> json.asDouble() + addend
1216+ json.isTextual -> (json.asText().toDoubleOrNull() ? : 0.0 ) + addend
1217+ else -> addend
1218+ }
1219+ objectMapper.valueToTree(value)
1220+ }
1221+ }
1222+ " @subtract" , " @sub" -> when (json) {
1223+ is ArrayNode -> {
1224+ val subtrahend = argument?.toDoubleOrNull() ? : 0.0
1225+ val resultArray = objectMapper.createArrayNode()
1226+ json.forEach { node ->
1227+ val value = when {
1228+ node.isNumber -> node.asDouble() - subtrahend
1229+ node.isTextual -> (node.asText().toDoubleOrNull() ? : 0.0 ) - subtrahend
1230+ else -> - subtrahend
1231+ }
1232+ resultArray.add(value)
1233+ }
1234+ resultArray
1235+ }
1236+ else -> {
1237+ val subtrahend = argument?.toDoubleOrNull() ? : 0.0
1238+ val value = when {
1239+ json.isNumber -> json.asDouble() - subtrahend
1240+ json.isTextual -> (json.asText().toDoubleOrNull() ? : 0.0 ) - subtrahend
1241+ else -> - subtrahend
1242+ }
1243+ objectMapper.valueToTree(value)
1244+ }
1245+ }
1246+ " @power" , " @pow" -> when (json) {
1247+ is ArrayNode -> {
1248+ val exponent = argument?.toDoubleOrNull() ? : 1.0
1249+ val resultArray = objectMapper.createArrayNode()
1250+ json.forEach { node ->
1251+ val value = when {
1252+ node.isNumber -> node.asDouble().pow(exponent)
1253+ node.isTextual -> (node.asText().toDoubleOrNull() ? : 0.0 ).pow(exponent)
1254+ else -> 0.0
1255+ }
1256+ resultArray.add(value)
1257+ }
1258+ resultArray
1259+ }
1260+ else -> {
1261+ val exponent = argument?.toDoubleOrNull() ? : 1.0
1262+ val value = when {
1263+ json.isNumber -> json.asDouble().pow(exponent)
1264+ json.isTextual -> (json.asText().toDoubleOrNull() ? : 0.0 ).pow(exponent)
1265+ else -> 0.0
1266+ }
1267+ objectMapper.valueToTree(value)
1268+ }
1269+ }
1270+ " @round" -> when (json) {
1271+ is ArrayNode -> {
1272+ val digits = argument?.toIntOrNull() ? : 0
1273+ val multiplier = 10.0 .pow(digits.toDouble())
1274+ val resultArray = objectMapper.createArrayNode()
1275+ json.forEach { node ->
1276+ val value = when {
1277+ node.isNumber -> round(node.asDouble() * multiplier) / multiplier
1278+ node.isTextual -> round((node.asText().toDoubleOrNull() ? : 0.0 ) * multiplier) / multiplier
1279+ else -> 0.0
1280+ }
1281+ resultArray.add(value)
1282+ }
1283+ resultArray
1284+ }
1285+ else -> {
1286+ val digits = argument?.toIntOrNull() ? : 0
1287+ val multiplier = 10.0 .pow(digits.toDouble())
1288+ val value = when {
1289+ json.isNumber -> round(json.asDouble() * multiplier) / multiplier
1290+ json.isTextual -> round((json.asText().toDoubleOrNull() ? : 0.0 ) * multiplier) / multiplier
1291+ else -> 0.0
1292+ }
1293+ objectMapper.valueToTree(value)
1294+ }
1295+ }
1296+ " @abs" -> when (json) {
1297+ is ArrayNode -> {
1298+ val resultArray = objectMapper.createArrayNode()
1299+ json.forEach { node ->
1300+ val value = when {
1301+ node.isNumber -> abs(node.asDouble())
1302+ node.isTextual -> abs(node.asText().toDoubleOrNull() ? : 0.0 )
1303+ else -> 0.0
1304+ }
1305+ resultArray.add(value)
1306+ }
1307+ resultArray
1308+ }
1309+ else -> {
1310+ val value = when {
1311+ json.isNumber -> abs(json.asDouble())
1312+ json.isTextual -> abs(json.asText().toDoubleOrNull() ? : 0.0 )
1313+ else -> 0.0
1314+ }
1315+ objectMapper.valueToTree(value)
1316+ }
1317+ }
10831318 else -> json
10841319 }
10851320 }
0 commit comments