Skip to content

Commit 58de7b7

Browse files
author
Kenneth Endfinger
committed
Add a hexjson example for proving epicness.
1 parent 0cd8152 commit 58de7b7

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

example/hexjson.dart

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import "package:msgpack/msgpack.dart";
2+
3+
import "dart:io";
4+
import "dart:convert";
5+
import "dart:typed_data";
6+
7+
import "package:crypto/crypto.dart";
8+
9+
main() async {
10+
var string = await stdin.transform(const Utf8Decoder()).join();
11+
var hexBytes = string.split(" ").where((s) => s.trim().isNotEmpty).map((s) {
12+
return int.parse(s, radix: 16);
13+
}).toList();
14+
15+
var data = unpack(hexBytes);
16+
var encoder = new JsonEncoder((e) {
17+
if (e is Float) {
18+
return e.value;
19+
} else if (e is ByteData) {
20+
return CryptoUtils.bytesToBase64(
21+
e.buffer.asUint8List(e.offsetInBytes, e.lengthInBytes)
22+
);
23+
} else {
24+
return e;
25+
}
26+
});
27+
28+
print(encoder.convert(data));
29+
}

lib/src/packer.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ class MsgPackBuffer implements PackBuffer {
142142
for (var i = 0; i < bufferCount; i++) {
143143
Uint8List buff = _buffers[i];
144144

145-
for (var x = 0; x < buff.lengthInBytes; x++) {
145+
for (var x = buff.offsetInBytes; x < buff.lengthInBytes; x++) {
146146
out[off] = buff[x];
147147
off++;
148148
}

0 commit comments

Comments
 (0)