是否有查询CBOR的语言?

时间:2018-01-15 16:15:09

标签: jsonpath cbor

我正在寻找用于查询CBOR的语言,例如JsonPathjq,但是用于CBOR二进制格式。我不想从CBOR转换为JSON,因为JSON中不存在某些CBOR类型,并且性能问题。

2 个答案:

答案 0 :(得分:0)

当然,您可以使用任何通用编程语言来查询CBOR,例如JavaScript可能是一个不错的选择。但是如果你正在寻找像JsonPath这样的“查询语言”,我不知道是否有任何针对CBOR的专门开发。

答案 1 :(得分:0)

C ++库jsoncons允许您使用JSONPath查询CBOR,例如,

#include <jsoncons/json.hpp>
#include <jsoncons_ext/cbor/cbor.hpp>
#include <jsoncons_ext/jsonpath/json_query.hpp>
#include <iomanip>

using namespace jsoncons; // For convenience

int main()
{
    std::vector<uint8_t> v = {0x85,0xfa,0x40,0x0,0x0,0x0,0xfb,0x3f,0x12,0x9c,0xba,0xb6,0x49,0xd3,0x89,0xc3,0x49,0x1,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0xc4,0x82,0x38,0x1c,0xc2,0x4d,0x1,0x8e,0xe9,0xf,0xf6,0xc3,0x73,0xe0,0xee,0x4e,0x3f,0xa,0xd2,0xc5,0x82,0x20,0x3};
/*
    85 -- Array of length 5     
      fa -- float 
        40a00000 -- 5.0
      fb -- double 
        3f129cbab649d389 -- 0.000071
      c3 -- Tag 3 (negative bignum)
        49 -- Byte string value of length 9
          010000000000000000
      c4 -- Tag 4 (decimal fraction)
        82 -- Array of length 2
          38 -- Negative integer of length 1
            1c -- -29
          c2 -- Tag 2 (positive bignum)
            4d -- Byte string value of length 13
              018ee90ff6c373e0ee4e3f0ad2
      c5 -- Tag 5 (bigfloat)
        82 -- Array of length 2
          20 -- -1
          03 -- 3   
*/

    // Decode to a json value (despite its name, it is not JSON specific.)
    json j = cbor::decode_cbor<json>(v);

    // Serialize to JSON
    std::cout << "(1)\n";
    std::cout << pretty_print(j);
    std::cout << "\n\n";

    // as<std::string>() and as<double>()
    std::cout << "(2)\n";
    std::cout << std::dec << std::setprecision(15);
    for (const auto& item : j.array_range())
    {
        std::cout << item.as<std::string>() << ", " << item.as<double>() << "\n";
    }
    std::cout << "\n";

    // Query with JSONPath
    std::cout << "(3)\n";
    json result = jsonpath::json_query(j,"$.[?(@ < 1.5)]");
    std::cout << pretty_print(result) << "\n\n";

    // Encode result as CBOR
    std::vector<uint8_t> val;
    cbor::encode_cbor(result,val);

    std::cout << "(4)\n";
    for (auto c : val)
    {
        std::cout << std::hex << std::setprecision(2) << std::setw(2)
                  << std::setfill('0') << static_cast<int>(c);
    }
    std::cout << "\n\n";

/*
    83 -- Array of length 3
      fb -- double
        3f129cbab649d389 -- 0.000071
    c3 -- Tag 3 (negative bignum)
      49 -- Byte string value of length 9
        010000000000000000
    c4 -- Tag 4 (decimal fraction)
      82 -- Array of length 2
        38 -- Negative integer of length 1
          1c -- -29
        c2 -- Tag 2 (positive bignum)
          4d -- Byte string value of length 13
            018ee90ff6c373e0ee4e3f0ad2
*/
}

输出:

(1)
[
    2.0,
    7.1e-05,
    "-18446744073709551617",
    "1.23456789012345678901234567890",
    [-1, 3]
]

(2)
2.0, 2
7.1e-05, 7.1e-05
-18446744073709551617, -1.84467440737096e+19
1.23456789012345678901234567890, 1.23456789012346
1.5, 1.5

(3)
[
    7.1e-05,
    "-18446744073709551617",
    "1.23456789012345678901234567890"
]

(4)
83fb3f129cbab649d389c349010000000000000000c482381cc24d018ee90ff6c373e0ee4e3f0ad2