A PHP implementation of Binson. Binson is an exceptionally simple data serialization format; see binson.org.
Just place src/binson.php
into your project's source directory and "require" it.
You need nothing to know about binson to start using it:
$src = ["a"=>[true, 123, "b", 5, binson::BYTES("\x01\x02")], "b"=>false, "c"=>7];
$binson_raw = binson_encode($src); // encode arbitrary associative array to binary string
$decoded = binson_decode($binson_raw); // decode, if possible, from binson-encoded binary string
// now $decoded should be equal to $src
To check if random binary string represents valid well-formed binson object:
if (null !== binson_decode($raw))
{
echo 'valid!';
}
Required PHP version is at least 7.2.x (64bit only)
Known to be stable on following platforms:
- PHP 7.2.8 (Ubuntu 14.04.5 LTS, x86_64)
Notes:
- no PHP5 compatibility (by design)
- no 32bit PHP version support
- Release v1.0 (created 2018-Nov-5)
- Schedulled release v1.1 for 2018-Dec-31
- Unit test line coverage is 90%
- binson-java (reference implementation)
- binson-java-light
- binson-c-light (reference implementation)
- binson-js
- binson-swift
- binson-erlang
- Iterative parsing (no recursion)
- No extension dependencies (should work with any custom PHP7 build)
- Instant serialization/deserialization to/from PHP native arrays
- Declarative "state transition matrix" parsing algorithm
- Error handling is PHP7 exception based (see
BinsonException
class)
Current implementation is not mature, is it why before any usage highly recommended to run full unit test suite.
Initialize environment first:
make init
make update
Now run PHPUnit tests:
make test.writer
make test.parser
make test.serializer
make test.deserializer
Not all arbitrary multilevel arrays are serializable. Next rules should be applied to make sure array is serializer-compatible:
- For values only primitive types
bool
,integer
,float
,string
,array
are allowed. - When use instance of the class
BinsonWriter
as value, it's content (bytes) will be placed inline, without any framing. - For field names only
string
is allowed. Numeric field names must have dot suffix. E.g. PHP array['3.' => true]
will be translated to binson object{'3' => true}
, then to raw byte sequence:\x40\x14\x01\x33\x44\x41
.
Typical usage (serialization):
$writer = new BinsonWriter();
$writer->put( ["a"=>[true, 123, "b", 5, binson::BYTES("\x01\x02")], "b"=>false, "c"=>7] );
"Streaming" to existing string:
$buffer = "";
$writer = new BinsonWriter($buffer);
...
Serializing multiple variables/literals:
$arr = [1,2,3];
$writer->put($arr, ["a"=>1, "b"=>"c"], true);
Specifying binson OBJECT instead of ARRAY:
$writer->put([]); // [] - empty binson array
$writer->put([[]]); // [[]] - nested empty arrays
$writer->put(binson::EMPTY_OBJECT); // {} - empty binson object
$writer->put([binson::EMPTY_OBJECT]); // [{}] - empty object inside the empty array
Include external writer:
$writer_external->put(['a'=>'b']);
...
$writer->put(['ext'=>$writer_external]);
Include raw bytes:
$writer_external->putRaw("\x42\x44\x43");
...
$writer->put(['ext'=>$writer_external]);
Low-level API:
$writer->objectBegin();
$writer->objectEnd();
$writer->arrayBegin();
$writer->arrayEnd();
$writer->putBoolean(true);
$writer->putInteger(123);
$writer->putDouble(1.23);
$writer->putName("aaa");
$writer->putString("abc");
$writer->putBytes("\x00\x3f\xff");
$writer->putInline($src_writer);
$len = $writer->length();
$str = $writer->toBytes();
$res = $writer->verify();
Method chaining:
$writer->objectBegin()
->putName("aaa")
->arrayBegin()
->putBoolean(false)
->putInteger(123)
->arrayEnd()
->objectEnd();
Include external writer:
$writer_ext->arrayBegin()
->putFalse()
->arrayEnd();
$writer->arrayBegin()
->putInline($writer_ext)
->arrayEnd();
Include raw bytes:
$writer->arrayBegin()
->putRaw("\x42\x45\x43")
->arrayEnd();
Low-level API:
// {"a":[true,123,"b",5],"b":false,"c":7}
$buf = "\x40\x14\x01\x61\x42\x44\x10\x7b\x14\x01\x62\x10\x05\x43\x14\x01\x62\x45\x14\x01\x63\x10\x07\x41";
$parser = new BinsonParser($buf);
$parser->enterObject();
$parser->field("a");
$parser->enterArray();
$parser->next();
$out = [];
$out[] = $parser->getValue(binson::TYPE_BOOLEAN); // type checks are optional
$parser->next();
$out[] = $parser->getValue(binson::TYPE_INTEGER); // type checks are optional
$parser->next();
$out[] = $parser->getValue(binson::TYPE_STRING); // type checks are optional
$parser->leaveArray();
$parser->field("c");
$out[] = $parser->getName();
$out[] = $parser->getValue(binson::TYPE_INTEGER); // type checks are optional
$parser->leaveObject();
$out[] = $parser->isDone();
echo implode(PHP_EOL, $out);
Will output:
1
123
b
c
7
1
To check for data before parsing for being valid binson:
$is_valid = $parser->verify();
- Documented all public methods
- Schedulled release v1.1 for 2018-Dec-31
- Release v1.0
- Schedulled release v1.0 for 2018-Nov-5
- 32bit PHP versions support dropped.
- Minimal PHP version now is 7.2.x; previously claimed backward compatibility with lower versions now is cancaled.
- Library fixed to be backward compatible with PHP 7.0.x
- Added ability to mark string values to be encoded into BYTES binson type instead of default STRING by wrapping strings with
binson::BYTES()
.
- Added functionality to place inline data from external writer in both log and high level API. See:
BinsonWriter::putRaw()
andBinsonWriter::putInline()
.
- top-level functions
binson_encode()
andbinson_decode()
are implemented (as wrappers aroundBinsonWriter
&BinsonParser
)