NDJSON的替代方案有哪些?

huangapple go评论76阅读模式
英文:

Alternatives to NDJSON?

问题

我需要一个磁盘序列化格式,它具备以下特点:

  • 支持UTF-8或二进制数据
  • 支持在一个文件中包含多个消息(类似于换行分隔的JSON)
  • (可能)无需模式定义
  • (可能)同时有Node.js和Rust的实现

我在Go语言中找不到一种方法来支持msgpackCBOR在一个文件中包含多个消息,尽管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)

}

huangapple
  • 本文由 发表于 2022年9月10日 01:52:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/73665882.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定