lua-resty-jq

LuaJIT FFI 绑定到 libjq

$ opm get bungle/lua-resty-jq

lua-resty-jq

lua-resty-jq 是一个小型 LuaJIT FFI 包装器,用于 jq

lua-resty-jq 的 Hello World

    -- <example-input> from https://api.github.com/repos/stedolan/jq/commits?per_page=5
    local jq = require "resty.jq".new()
    
    jq:compile("[ .[] | {message: .commit.message, name: .commit.committer.name} ]")
    local output = jq:filter(<example-input>)
    
    print(output)
    
    jq:teardown()

运行以上代码将输出(或类似内容)

    [
      {
        "message": "Add some missing code quoting to the manual",
        "name": "William Langford"
      },
      {
        "message": "Reduce allocation on string multiplication",
        "name": "William Langford"
      },
      {
        "message": "Fix multiple string multiplication",
        "name": "William Langford"
      },
      {
        "message": "Fix error handling in strftime",
        "name": "William Langford"
      },
      {
        "message": "Makefile: prepend srcdir to jq.1.prebuilt to fix out of source compilation",
        "name": "William Langford"
      }
    ]

新建

语法:jq, err = require("resty.jq").new()

分配一个 libjq 上下文。

拆卸

语法:jq:teardown()

销毁 libjq 上下文,释放资源。

编译

语法:ok, err = jq:compile(program)

如果程序已编译,则返回 true,否则返回 nil 和错误 compilation failed

请注意,目前无法检查编译错误的详细信息。如有疑问,请在 CLI jq 中尝试您的程序。

过滤

语法:res, err = jq:filter(data, options)

使用先前编译的程序过滤 dataoptions 表格可以包含更改过滤器行为的标志,类似于 jq 的 CLI 选项的子集

  • compact_output:以紧凑形式返回输出,不包含额外的空格,并且每个 JSON 对象在一行上。默认为 true。设置为 false 以获得“漂亮”的输出。

  • raw_output:以原始字符串输出,而不是 JSON 引号。默认为 false

  • join_output:与 raw_output 相同,但此外不输出换行符。默认为 false

  • ascii_output:jq 通常将非 ASCII Unicode 代码点输出为 UTF-8,即使输入将其指定为转义序列(如“\u03bc”)。使用此选项,您可以强制 jq 生成纯 ASCII 输出,其中每个非 ASCII 字符都替换为等效的转义序列。默认为 false

  • sort_keys:按排序顺序输出每个对象的字段的键。默认为 false

另请参阅

许可证

lua-resty-jq 使用双条款 BSD 许可证。

    Copyright (c) 2020 – 2021 Aapo Talvensaari, James Hurst
    All rights reserved.
    
    Redistribution and use in source and binary forms, with or without modification,
    are permitted provided that the following conditions are met:
    
    * Redistributions of source code must retain the above copyright notice, this
      list of conditions and the following disclaimer.
    
    * Redistributions in binary form must reproduce the above copyright notice, this
      list of conditions and the following disclaimer in the documentation and/or
      other materials provided with the distribution.
    
    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
    ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
    WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
    DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
    ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES

作者

Aapo Talvensaari (@bungle),James Hurst (@pintsized)

许可证

2bsd

依赖项

luajit

版本