Git Product home page Git Product logo

gohessian's Introduction

This is a feature-complete golang hessian serializer.

It's cloned from viant/gohessian , and do the following works:

  • fix lots of bugs
  • large scope refactoring to let code structure simple and human-friendly
  • add ref and date features implement
  • more unit tests
  • api refactoring

Usage

name map and type map

  • name map: a map[string]string used by encoder to determine the class name of a object
  • type map: a map[string]reflect.Type used by decoder to determine the type of instance to initialize

You can use function hessian.ExtractTypeNameMap(interface{}) to generate both the type map and name map. It's the recommendation way. Of course, you can create by yourself, but make sure them contain all names and types which encoder and decoder needed.

simple example

type circular struct {
	Num      int
	Previous *circular
	Next     *circular
}

func main() {
	c := &circular{}
	c.Num = 12345
	c.Previous = c
	c.Next = c

	// create hessian serializer
	serializer := hessian.NewSerializer(hessian.ExtractTypeNameMap(c))

	fmt.Println("source object: ", c)

	// encode to bytes
	bytes, err := serializer.Encode(c)
	if err != nil {
		panic(err)
	}

	// decode from bytes
	decoded, err := serializer.Decode(bytes)
	if err != nil {
		panic(err)
	}
	fmt.Println("decode object: ", decoded)
}

using java class name

You can define a function HessianCodecName() string for your struct if using hessian.ExtractTypeNameMap(interface{}) to generate type map and name map.

type TraceVo struct {
	Key   string
	Value string
}

func (TraceVo) HessianCodecName() string {
	return "hessian.TraceVo"
}

typeMap,nameMap := hessian.ExtractTypeNameMap(&TraceVo{})

If you create type map and name map manually, you should also add the java class name mapping.

concurrently

hessian.NewSerializer contains serialization processing data, so a serializer can't be used concurrently, you should create a new one when needed.

If there is only one type of data to serialize , a goroutine can continue use the same serializer to Encode() or Decode().

streaming transport

The following is a client-server streaming transport example:

server side:

_,nameMap := hessian.ExtractTypeNameMap(object)
encoder := hessian.NewEncoder(outputStreamWriter, nameMap) // write stream to outputStreamWriter
for {
    data := getNewData()
    err = encoder.Write(data) // write new data
    if err != nil {
        panic(err)
    }
}

client side:

typeMap,_ := hessian.ExtractTypeNameMap(object)
decoder := hessian.NewDecoder(inputStreamReader, typeMap) // read stream from inputStreamReader
for {
    obj,err := decoder.Read() // read new object
    if err != nil {
        panic(err)
    }
    
    // process obj
}

Reference

gohessian's People

Contributors

wongoo avatar supercoupe9725 avatar michaelthecsguy avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.