advantagefse / json-logic-swift Goto Github PK
View Code? Open in Web Editor NEWA native Swift JsonLogic implementation. This parser accepts JsonLogic rules and executes them.
License: MIT License
A native Swift JsonLogic implementation. This parser accepts JsonLogic rules and executes them.
License: MIT License
Hello,
Thank you for this library.
I have identified a runtime crash issue caused by division by zero. For instance, the following code snippet causes the library to crash:
let rule =
"""
{ "/" : [10, 0] }
"""
// Example parsing
let result: String? = try? JSONLogicClient.applyRule(rule, to: nil)
print("result = \(String(describing: result))")
This pull request #33 implements a fix to prevent this crash.
In the original JS implementation https://jsonlogic.com/truthy.htm truthy evaluation for empty arrays is false but for empty dictionaries it is not defined.
For consistency we will define it as false if empty.
So this will evaluate to false
{"==" : [{}, true]}
And this to true
{"==" : [{ "key": value}, true]}
Also fix typo thruthy->truthy
Hello. I'm applying JsonLogic to a Swift Double
data type and expecting the result to be of the same type.
{"if": [ {">": [ {"var": "double1"}, {"var": "double2"} ] }, {"var": "double1"} ]}
with the values
{ "double1":1618495500, "double2":1618495000 }
My exception is for JsonLogic.applyRule
to return a Double
data type with the value 1618495500.
However given the jsonDataOrNil
param of applyRule
is a String
data type I suspect the Double
values are cast as Int
as there is no explicit tag to denote these values as Double
. Therefore applyRule
returns an Int
data type with the value 1618495500.
Note that if I adjust my values to include a fractional component applyRule
returns a Double
data type as expected.
{ "double1":1618495500.01, "double2":1618495000.01 }
Keen to hear your thoughts on this and preferred workaround?
This is a small but important difference between the way https://jsonlogic.com JS implementation works and this swift implementation.
The original JS implementation allows only Numbers, Strings, Null, Boolean. The following eval to true:
{"===" : ["123", "123"]}
{"===" : [123, "123]}
{"===" : [false, false]}
{"===" : [null, null]}
But this implementation allows also JSON Dictionaries and Arrays. The following eval to true:
{"===" : [ ["123"], ["123"]]}
{"===" : [ {"key": 1}, {"key": 1}]}
There is bug that with strict comparison for Dictionaries and Arrays that leads to type coercion for the nested values.
So this will eval as true when it should not:
{"===" : [ ["123"], [123]]}
Also for non string equality the dictionary support was missing
Thanks for this framework! Any plans to include support for Carthage?
Hi,
Json-logic-swift is very useful for our development. It works great overall. Thank you!
We discovered a case where a rule does't evaluate to the same result compared to the test tool on https://jsonlogic.com/play.html . Assuming jsonlogic.com is the reference implementation, we think there might be a bug in json-logic-swift.
Here is the test case. It evaluates to TRUE on jsonlogic.com and FALSE with json-logic-swift.
Rule:
{"!": [
{"and" : [
{"in":["Population", { "var" : "CHOIX" }]},
{"in" : [ { "var" : "TAILLE_POP" }, ["", null] ]}
]}
]}
Data:
{ "COMPARTIEMENT" : "Faune/Wildlife", "ACTIF" : "false" }
Have a great day,
Jean-Robert
Starting Xcode 12, the minimum os version supported is iOS 9. When using this package, it shows the below warning.
'IPHONEOS_DEPLOYMENT_TARGET' is set to 8.0, but the range of supported deployment target versions is 9.0 to 14.0.99.
Request to bump the Package.swift to use v9 as developers will be using newer Xcodes to build their project. We are at 13.4 version of Xcode now.
I get the error "Value of protocol type 'Any' cannot conform to 'Equatable'; only struct/enum/class types can conform to protocols" when compiling the jsonlogic pod.
The error occurs at the
case Any?.none, nil, is NSNull:
line (line 87) in the json-enum/JSON.swift file.
I'm using Xcode 12.2 and with Swift 5.2
Hi, I'm currently trying to build my use case using your jsonlint package and I'm trying to do additions with data that might be missing some keys. To circumvent this, I'm adding defaults as documented here, but i'm always receiving keyNotFound Errors.
Are default values supported?
jsonLogic:
{
"+": [{
"var": ["MtgS-wRFsq2W6ZOZ9gq", 0]
}, {
"var": ["MtgS1WFJ2DaVGrpvUha", 0]
}, {
"var": ["MtgS7bS6e1trrSe-suX", 0]
}, {
"var": ["MtgSJyxA9kEf6b_61Vz", 0]
}]
}
Parsed jsonLogic Expression:
(jsonlogic.ArrayOfExpressions) $R11 = {
expressions = 4 values {
[0] = {
expression = {
expressions = 2 values {
[0] = {
json = String (String = "MtgS-wRFsq2W6ZOZ9gq")
}
[1] = {
json = Int (Int = 0)
}
}
}
}
[1] = {
expression = {
expressions = 2 values {
[0] = {
json = String (String = "MtgS1WFJ2DaVGrpvUha")
}
[1] = {
json = Int (Int = 0)
}
}
}
}
[2] = {
expression = {
expressions = 2 values {
[0] = {
json = String (String = "MtgS7bS6e1trrSe-suX")
}
[1] = {
json = Int (Int = 0)
}
}
}
}
[3] = {
expression = {
expressions = 2 values {
[0] = {
json = String (String = "MtgSJyxA9kEf6b_61Vz")
}
[1] = {
json = Int (Int = 0)
}
}
}
}
}
}
Data parsed to JSON:
Dictionary = 10 key/value pairs {
[0] = {
key = "MtgS7bS6e1trrSe-suX"
value = Int (Int = 0)
}
[1] = {
key = "MtgS-wRFsq2W6ZOZ9gq"
value = Int (Int = 1)
}
[2] = {
key = "MtgRwxWKfwXn-UNCzET"
value = Int (Int = 0)
}
[3] = {
key = "MtgS34Y8AXt1UJiTYyY"
value = Int (Int = 0)
}
[4] = {
key = "MtgRyq_u9Nu8FpYGnQW"
value = Int (Int = 0)
}
[5] = {
key = "MtgS6A5NxGFs2BIwYX5"
value = Int (Int = 0)
}
[6] = {
key = "MtgS1WFJ2DaVGrpvUha"
value = Int (Int = 0)
}
[7] = {
key = "MtgS9OWoc3O8CXJeSdy"
value = Int (Int = 0)
}
[8] = {
key = "MtgS4PC_qmPCcjyH5CA"
value = Int (Int = 0)
}
[9] = {
key = "MtgSAfANO6aGCRzVdcp"
value = Int (Int = 0)
}
}
}
The add_operation allows extending JsonLogic using custom libraries inside the rules. Is there any plans to support this?
https://jsonlogic.com/add_operation.html
Currently the library does not compile in Linux.
This is due to the bridging support for Cocoa types is available only in Darwin platforms, so this does not work:
throw` NSError() // In Linux -> error: thrown expression type 'NSError?' does not conform to 'Error'
Also we should add travis build for linux
The last sentence of the README states that this project is licensed under LGPL. However, the project's LICENSE file has the regular GNU GPLv3 license. What's the actual license?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.