SenML
Sensor measurements with base resolution, streams, and CBOR.
Website · Installation · Quick start · Scope and reference data · Development · License
Read, validate, write, and resolve RFC 8428 sensor measurements in Ruby. Supports SenML JSON, incremental SenSML JSON, record fragments, and CBOR.
Installation
Add the gem to your Gemfile:
gem "senml"Then run bundle install.
Requires Ruby 3.3 or later and json >= 2.19.4 and < 4.
Quick start
require "senml"
pack = SenML::Pack.build(base_name: "room:", base_time: 1_700_000_000, base_unit: "Cel") do |p|
p.add(n: "temperature", v: 21.5, t: 2)
p.add(n: "temperature", v: 21.8, t: 1)
end
parsed = SenML.parse(pack.to_json)
parsed.first.name # => "room:temperature"
parsed.first.time # => 1700000002
parsed.first["t"] # => 2 (the original field)
resolved = parsed.resolve(now: Time.at(1_700_000_000))
resolved.map(&:value) # => [21.8, 21.5]
resolved.first.time_at # => a UTC Timename, unit, time, value, and sum apply the current base fields. [] and to_h preserve original fields, including unknown noncritical extensions. Record#data lazily decodes unpadded base64url vd into a binary String. String and boolean measurements do not receive numeric base values. A record may have a value, a sum, or both; an inherited base value or base sum can supply a missing measurement field.
Resolution removes base fields, expands names, units, values and sums, converts relative times using now, and stably sorts by time. Version 10 is implicit; legacy positive versions up to 10 are retained when resolving. Future versions and mixed versions are rejected. A relative Record#time_at needs an explicit now:. Integers retain Ruby precision; nonintegral numbers use binary64 Float, and generated JSON preserves finite Float values across parsing.
Streams and fragments
File.open("measurements.json", "rb") do |io|
SenML::StreamReader.new(io).each { |record| puts record.name }
end
selected = pack.select_fragment("#rec=1,2-*")The stream reader yields each complete object without retaining the whole array, carrying base fields and version state between records. allow_incomplete: true accepts a SenSML stream that ends after a complete record without a closing array bracket; an unfinished object still fails. This reader handles JSON streams; CBOR parsing consumes a complete input String.
Fragments use one-based record indices and support single records, closed ranges, open ranges, and comma-separated combinations. Selection keeps original order, removes duplicates, and carries each selected record's effective base context, emitting inherited base fields only at the first selected record and when they change. Out-of-range and reversed ranges fail.
Validation and limits
Strict parsing checks field types, value counts, resolved names, registered units, versions, and canonical unpadded base64url. Duplicate JSON keys and uppercase numeric exponents fail. strict: false preserves recoverable semantic problems as pack.warnings; field types, unknown critical labels ending in _, and version problems always fail. Writing and resolving perform strict validation again.
All APIs that construct packs or readers accept these positive integer limits:
| Option | Default | Applies to |
|---|---|---|
max_bytes |
16,777,216 | Complete inputs/outputs and the JSON representation of native or decoded packs |
max_records |
100,000 | Packs and streams |
max_record_bytes |
1,048,576 | Each record, including stream buffers |
max_data_bytes |
786,432 | Decoded binary measurement size |
max_depth |
32 | Nested values; hard maximum 128 |
max_warnings |
1,000 | Retained lenient diagnostics in packs and streams |
Native array construction and CBOR decoding also check the cumulative JSON representation size against max_bytes; the decoded representation may be larger than its CBOR input. Streams use record limits instead of a total byte limit. Exceeding max_warnings raises LimitError before yielding the next record; reader.warnings retains the diagnostics collected so far. Resolution checks max_bytes incrementally as base fields expand, before retaining an oversized resolved pack. Binary size is checked before decoding. SenML::ParseError reports malformed input or field types, ValidationError reports semantic problems with a pointer, UnsupportedVersionError identifies unknown versions, and LimitError reports exceeded bounds. A conversion that cannot preserve supported semantics raises ConversionError.
CBOR
require "senml/cbor"
bytes = pack.to_cbor
SenML.parse_cbor(bytes)
# Accept a complete SenSML indefinite-length CBOR array:
SenML.parse_cbor(stream_bytes, streaming: true)CBOR maps the RFC's integer labels and binary data values, preserves JSON-compatible extension fields, and supports decimal-fraction tag 4 with exponents from -400 to 400. The decoder checks lengths, nesting, UTF-8, duplicate map keys, and allowed types while reading. No separate CBOR gem is required. Unknown integer labels, non-JSON binary extension values, other tags, indefinite strings, and lossy conversion of large integers fail explicitly. JSON nonintegral values use Float; conversion of arbitrary decimal fractions can round to binary64.
Scope and reference data
| Supported | Outside this release |
|---|---|
| SenML and incremental SenSML JSON | XML and EXI |
| Base resolution and record fragments | Unit conversion and CoAP transport |
| CBOR | Incremental CBOR decoding |
| RFC 8428 version rules and registered labels | FETCH/PATCH and RFC 9100 feature versions |
Registered labels include vlo and the ct/bct labels from RFC 9193. Content-format strings are preserved opaquely, and bct resolves onto binary measurements. Unknown base labels stop resolution and fragment selection because their inheritance rules are unknown, including when resolving a later record that inherits the unsupported base.
The library makes no network requests when parsing or resolving measurements.
Development
bundle install
bundle exec rake test
COVERAGE=1 bundle exec rake test
bundle exec rbs -I sig validate
bundle exec ruby script/benchmark.rbThe first test run downloads RFC/IANA fixtures into ignored tmp/ directories and requires network access.
Run bundle exec ruby script/update_registry.rb to check IANA metadata. Review registry changes before updating it with --write.
Contributing
Bug reports and pull requests are welcome on GitHub. See Development for the checks to run before submitting a change. Report security vulnerabilities privately as described in SECURITY.md.
License
Released under the MIT License.