英文:
Alternatives to NDJSON?
问题
我需要一个磁盘序列化格式,它具备以下特点:
- 支持UTF-8或二进制数据
- 支持在一个文件中包含多个消息(类似于换行分隔的JSON)
- (可能)无需模式定义
- (可能)同时有Node.js和Rust的实现
我在Go语言中找不到一种方法来支持msgpack
或CBOR
在一个文件中包含多个消息,尽管CBOR(序列)支持此功能。目前我正在尝试使用asn.1
,它看起来不错,但我想知道是否有更好的替代方案。
英文:
I need a disk serialization format which:
-
Supports utf8 or binary data
-
Supports multiple messages per file (like newline delimited json)
-
(possibly) Is schemaless
-
(possibly) Has both a node and a rust implementation
I couldn't find a way for msgpack
or CBOR
to support multiple messages per file in go, although it's supported by cbor (sequences). At the moment I'm playing with asn.1
and it seems nice but I was wondering if there was a better alternative.
答案1
得分: 1
我已经使其同时适用于ASN.1和CBOR,现在我需要尝试使用Msgpack。
package main
import (
"bytes"
"github.com/fxamacker/cbor/v2"
"log"
)
func main() {
type Record struct {
Payload string
Counter int
}
r1 := Record{
"hello", 1}
r2 := Record{
" world", 2}
var buff []byte
b, err := cbor.Marshal(r1)
if err != nil {
log.Fatal(err)
}
buff = append(buff, b...)
b, err = cbor.Marshal(r2)
if err != nil {
log.Fatal(err)
}
buff = append(buff, b...)
log.Println(buff)
var out1, out2 Record
decoder := cbor.NewDecoder(bytes.NewReader(buff))
err = decoder.Decode(&out1)
if err != nil {
log.Fatal(err)
}
log.Println(out1)
err = decoder.Decode(&out2)
if err != nil {
log.Fatal(err)
}
log.Println(out2)
}
英文:
I made it work both with asn.1 and cbor, I need to try msgpack now
package main
import (
"bytes"
"github.com/fxamacker/cbor/v2"
"log"
)
func main() {
type Record struct {
Payload string
Counter int
}
r1 := Record{
"hello", 1}
r2 := Record{
" world", 2}
var buff []byte
b, err := cbor.Marshal(r1)
if err != nil {
log.Fatal(err)
}
buff = append(buff, b...)
b, err = cbor.Marshal(r2)
if err != nil {
log.Fatal(err)
}
buff = append(buff, b...)
log.Println(buff)
var out1, out2 Record
decoder := cbor.NewDecoder(bytes.NewReader(buff))
err = decoder.Decode(&out1)
if err != nil {
log.Fatal(err)
}
log.Println(out1)
err = decoder.Decode(&out2)
if err != nil {
log.Fatal(err)
}
log.Println(out2)
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论