json2msgpackStreamer

package module
v0.1.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 20, 2022 License: MIT Imports: 10 Imported by: 2

README

json2msgpackStreamer

This module converts a (ND)JSON stream to a messagepack stream on-the-fly.

See package documentation

How to use

package main

import (
	"bytes"
	"encoding/json"
	"fmt"

	"github.com/arivum/json2msgpackStreamer"
	"github.com/vmihailenco/msgpack/v5"
)

func main() {
	var rawJSON = map[string]interface{}{
		"a": 0,
		"b": true,
		"c": []string{"a", "b", "c", "d", "e", "f", "g", "h", "i", "j"},
		"d": map[string]interface{}{
			"d_a": 1,
			"d_b": "teststring",
		},
		"e": float32(1.2),
		"f": float64(2),
		"g": 0x5a,
		"h": nil,
		"i": -(1 << 33),
		"j": true,
		"k": []string{"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p"},
		"l": map[string]interface{}{
			"d_a": 1,
			"d_b": "teststring",
		},
		"m": float32(1.2),
		"n": float64(2),
		"o": 0x5a,
		"p": nil,
	}
	var rawJSON2 = map[string]interface{}{
		"a": 0,
		"b": true,
		"c": []string{"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p"},
		"d": map[string]interface{}{
			"d_a": 1,
			"d_b": "teststring",
		},
	}

	// Create the JSON buffer that needs to be converted to a msgpack stream
	var jsonObj, _ = json.Marshal(rawJSON)
	var jsonObj2, _ = json.Marshal(rawJSON2)
	jsonObj = append(jsonObj, jsonObj...)
	jsonObj = append(jsonObj, jsonObj2...)
	jsonObj = append(jsonObj, jsonObj...)
	jsonObj = append(jsonObj, jsonObj2...)
	fmt.Println(string(jsonObj))

	// Create the converter:
	// JSON2MsgPackStreamer is of type io.Reader and can be passed to e.g. "github.com/vmihailenco/msgpack/v5".NewDecoder(r io.Reader)
	var conv = json2msgpackStreamer.NewJSON2MsgPackStreamer(bytes.NewBuffer(jsonObj))

	// Create the msgpack decoder that reads from the converter
	msgpackDec := msgpack.NewDecoder(conv)

	i := 0
	for {
		var entry interface{}

		if err := msgpackDec.Decode(&entry); err != nil {
			break
		}
		fmt.Printf("Entry #%d: %+v\n", i, entry)
		i++
	}
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type JSON2MsgPackStreamer

type JSON2MsgPackStreamer struct {
	// contains filtered or unexported fields
}

func NewJSON2MsgPackStreamer

func NewJSON2MsgPackStreamer(r io.Reader) *JSON2MsgPackStreamer

func (*JSON2MsgPackStreamer) GetLastError

func (t *JSON2MsgPackStreamer) GetLastError() error

func (*JSON2MsgPackStreamer) Read

func (t *JSON2MsgPackStreamer) Read(p []byte) (int, error)

Directories

Path Synopsis
scripts
#nosec

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL