bjdata

October 5, 2025 ยท View on GitHub

BJData (draft 3 specification) implementation in Dart.

Dart version License

Pub package Build status Coverage status

Encoding/decoding of BJData to/from Dart objects in an API based on the dart:convert package.

Usage

import 'package:bjdata/bjdata.dart';

void main() {
    final List<int> encoded = bjdataEncode({
        'hello': 'world',
        'pi': 3.14159,
        'happy': true,
        'list': [1, 0, 1],
        'binary': ByteData.sublistView(Uint8List.fromList([0xDE, 0xAD, 0xBE, 0xEF])),
        'nothing': null,
    });
    
    final decoded = bjdataDecode(encoded);

    print(bjdataBlockNotation(null)); // [Z]
    print(bjdataBlockNotation(true)); // [T]
    print(bjdataBlockNotation(false)); // [F]
    print(bjdataBlockNotation(42)); // [U][42]
    print(bjdataBlockNotation(3.14)); // [D][3.14]
    print(bjdataBlockNotation('Hello, world!')); // [S][U][13][Hello, world!]
    print(bjdataBlockNotation([1, 2, 3])); // [[][U][1][U][2][U][3][]]
    print(bjdataBlockNotation({'foo': 1, 'bar': 2})); // [{][U][3][foo][U][1][U][3][bar][U][2][}]
}

Tool

dart pub global activate bjdata

# Show help
bjdata -h
# or
dart pub global run bjdata -h

# Encode a JSON file to BJData
bjdata encode input.json output.bjda

# Decode a BJData file to JSON
bjdata decode input.bjd output.json

# Pretty-print a JSON file in BJData block notation
bjdata print input.json

# stdin/stdout can be used instead of filenames
cat input.json | bjdata encode
cat input.bjd | bjdata decode
echo -n "[1, 2, 3]" | bjdata print

Types

Decoding BJData to Dart

  • Multi dimensional arrays are not yet supported.
BJData TypeMarkerDart
nullZnull
trueTtrue
falseFfalse
int8iint
uint8Uint
int16uint
uint16Iint
int32lint
uint32mint
int64Lint
uint64Mint *
float16hdouble
float32ddouble
float64Ddouble
byteBint
charCString
stringSString
hugeHBigInt
array[]List
array[byte][$BByteData
array[int8][$iInt8List
array[uint8][$UUint8List
array[int16][$uInt16List
array[uint16][$IUint16List
array[int32][$lInt32List
array[uint32][$mUint32List
array[int64][$LInt64List
array[uint64][$MUint64List
array[float16][$hFloat32List
array[float32][$dFloat32List
array[float64][$DFloat64List
object{}Map

* Warning: int in Dart is a signed 64-bit integer. uint64/M values are decoded as int64 (i.e. values greater than 9223372036854775807 are decoded as negative values).

Encoding Dart to BJData

DartMarkerBJData Type
nullZnull
boolTFbool
intUiIumlLint *
doubleDfloat64
StringSstring
BigIntHhuge
List[]array
ByteData[$Barray[byte] **
Int8List[$iarray[int8]
Uint8List[$Uarray[uint8]
Int16List[$uarray[int16]
Uint16List[$Iarray[uint16]
Int32List[$larray[int32]
Uint32List[$marray[uint32]
Int64List[$Larray[int64]
Uint64List[$Marray[uint64]
Float32List[$darray[float32]
Float64List[$Darray[float64]
Map{}object

* int values are encoded using the smallest integer type possible, favouring unsigned types.

** ByteData is recommended for encoding "binary data" as per the BJData specification (as this may affect how the data is parsed in other libraries). Converting from Uint8List to ByteData can be done using ByteData.sublistView(list).

Web

The package can be used in web applications, however it is affected by the JavaScript number peculiarities.

  • int values greater than 9007199254740991 (2532^{53} - 1) may lose precision when encoding/decoding.
  • double values without a fractional part will be encoded as int (important if consumer strictly expects a double value).
  • Int64List and Uint64List are not supported on web, so array[int64] and array[uint64] will be decoded as List<int>.