StupidBeauty
Read times:11246Posted at:Fri Aug 20 05:49:26 2021 - no title specified

hx_cbor : ruby 的 cbor 编码及解码库

Table of Contents

1 概述

2 使用

2.1 安装这个库

2.2 代码中使用

1 概述

以 C 语言实现的一个 ruby 扩展,用于 cbor 格式的编码和解码

基于 https://github.com/cabo/cbor-ruby

做了一些修改,增加选项 tolerant ,使得解码过程中可以容忍 cbor 字节流中的一些格式错误,尽可能多地解码出有效内容。

以上修改的目的,主要是为了支持自己的其它项目。未经过系统性地测试和验证,不打算合并回原作者的项目中。

源代码在这里: https://github.com/hxcan/cbor-ruby

2 使用

2.1 安装这个库

gem installhx_cbor

2.2 代码中使用

require 'cbor'

partCborFile = 'partly transferred cbor file' # cbor 格式传输的文件,只传输了一部分。

partCborFileContent = File .read partCborFile

options = { :tolerant => true } # 要求解码过程中容错。

decoded = CBOR .decode(partCborFileContent,options)

#decoded = CBOR.decode partCborFileContent # 遇到有问题的 cbor 会抛出异常。

puts decoded.class.name

# Hash

puts decoded.length

# 4

老虎装饰

Your opinions
Your name:Email:Website url:Opinion content: