json-enum 1.2.3

json-enum 1.2.3

Christos Koninis维护。



json-enum 1.2.3

  • 作者
  • Christos Koninis

jsonlogic-swift

CI Status Version Platform codecov

一个本地的 Swift JsonLogic 实现。这个解析器接受 JsonLogic 规则并执行它们。

JsonLogic 是一种以 JSON 格式编写涉及计算的规则的方式,这些规则可以在 JSON 数据上应用以获得一致的结果。因此,您可以在服务器和客户端之间以通用格式共享规则。原始的 JS JsonLogic 实现由 Jeremy Wadhams 开发。

安装

使用 CocoaPods

要使用 pod 在项目中,请在 Podfile 中添加:

pod jsonlogic

要运行示例项目,只需运行

pod try jsonlogic    

使用 Swift 包管理器

如果您使用 Swift 包管理器,请在依赖项中添加以下内容:

    dependencies: [
    .package(
        url: "https://github.com/advantagefse/json-logic-swift", from: "1.0.0"
    )
]

用法

您只需导入模块,然后调用applyRule全局方法。

import jsonlogic

let rule =
"""
{ "var" : "name" }
"""
let data =
"""
{ "name" : "Jon" }
"""

//Example parsing
let result: String? = try? applyRule(rule, to: data)

print("result = \(String(describing: result))")

applyRule 会解析规则,然后将其应用于 data,并尝试将结果转换为推断的返回类型,如果失败,将抛出错误。

如果您需要将相同的规则应用于多个数据,那么最好只解析一次规则。您可以通过使用带有规则的 JsonRule 对象初始化,然后调用 applyRule 来完成此操作。

//Example parsing
let jsonlogic = try JsonLogic(rule)

var result: Bool = jsonlogic.applyRule(to: data1)
result = jsonlogic.applyRule(to: data2)
//etc..

示例

简单

let rule = """
{ "==" : [1, 1] }
"""

let result: Bool = try applyRule(rule)
//evaluates to true

这是一个简单的测试,相当于 1 == 1。一些关于格式的说明

  1. 运算符始终处于“键”位置。每个 JsonLogic 规则只有一个键。
  2. 值通常是数组。
  3. 每个值可以是字符串、数字、布尔值、数组(非关联数)、或 null

组合

我们现在开始嵌套规则。

let rule = """
  {"and" : [
    { ">" : [3,1] },
    { "<" : [1,3] }
  ] }
"""
let result: Bool = try applyRule(rule)
//evaluates to true

在一种中缀语言中,这可以写成

( (3 > 1) && (1 < 3) )

数据驱动

显然,如果这些规则只能接受静态字面量数据,那么它们就不会很有趣。通常,您会使用规则对象和数据对象来调用 jsonLogic。您可以使用 var 操作符来获取数据对象的属性

let rule = """
  { "var" : ["a"] }
"""
let data = """
  { a : 1, b : 2 }
"""
let result: Int = try applyRule(rule, to: data)
//evaluates to 1

如果您喜欢,我们支持跳过值周围的数组

let rule = """
  { "var" : "a" }
"""
let data = """
  { a : 1, b : 2 }
"""
let result: Int = try applyRule(rule, to: data)
//evaluates to 1

您还可以使用 var 操作符通过数字索引访问数组

jsonLogic.apply(
  {"var" : 1 },
  [ "apple", "banana", "carrot" ]
);
// "banana"

这里有一条复杂的规则,它混合了字面量和数据。只有当这个派比110度凉时,并且里面满了苹果,才可以食用。

let rule = """
{ "and" : [
  {"<" : [ { "var" : "temp" }, 110 ]},
  {"==" : [ { "var" : "pie.filling" }, "apple" ] }
] }
"""
let data = """
  { "temp" : 100, "pie" : { "filling" : "apple" } }
"""

let result: Bool = try applyRule(rule, to: data)
//evaluates to true

自定义操作符

您可以注册一个自定义操作符

import jsonlogic
import JSON

// the key is the operator and the value is a closure that takes as argument
// a JSON and returns a JSON
let customRules =
    ["numberOfElementsInArray": { (json: JSON?) -> JSON in                                 
        switch json {
        case let .Array(array):
            return JSON(array.count)
        default:
            return JSON(0)
        }
    }]
    
let rule = """
    { "numberOfElementsInArray" : [1, 2, 3] }
"""
    
// The value is 3
let value: Int = try JsonLogic(rule, customOperators: customRules).applyRule()

其他操作符

要查看支持的完整操作符列表及其用法,请参阅 jsonlogic 操作符

命令行界面

即将推出...

贡献

欢迎提出更改。如果您发现了一个错误,请在提交修复之前提交一个复现该错误的单元测试。

因为这个项目是用 Swift PM 创建和构建的,所以在仓库中没有提交 Xcode 项目的文件。如果需要,您可以在终端中运行 genenate-xcodeproj.sh 来生成它。

$ . generate-xcodeproj.sh

需求

iOS tvOS watchOS macOS
>=8.0 >=10.0 >=2.0 >=10.12

作者

克里斯托斯·科尼尼斯,[邮箱&保密]

许可证

JsonLogic for Swift 在 MIT 许可证下提供。有关更多信息,请参阅 LICENSE 文件。