Giter Club home page Giter Club logo

cgsslwg's Introduction

tahwil

Cyclic graph structures serialization library written in go.

How can it be useful?

Sometimes you need to serialize a structure that has circular references. This library lets you transform your cyclic graph to a tree and then serialize to json.

How to use it?

Encoding

package main

import (
	"encoding/json"
	"fmt"

	"github.com/go-extras/tahwil"
)

type Person struct {
	Name     string
	Parent   *Person
	Children []*Person
}

func main() {
	parent := &Person{
		Name: "Arthur",
		Children: []*Person{
			{
				Name: "Ford",
			},
			{
				Name: "Trillian",
			},
		},
	}
	parent.Children[0].Parent = parent
	parent.Children[1].Parent = parent
	v, err := tahwil.ToValue(parent)
	if err != nil {
		panic(err)
	}
	res, err := json.Marshal(v)
	if err != nil {
		panic(err)
	}
	fmt.Println(string(res))
}

The output will be one-line equivalent of the following JSON:

    {
      "refid": 1,
      "kind": "ptr",
      "value": {
        "refid": 2,
        "kind": "struct",
        "value": {
          "Children": {
            "refid": 5,
            "kind": "slice",
            "value": [
              {
                "refid": 6,
                "kind": "ptr",
                "value": {
                  "refid": 7,
                  "kind": "struct",
                  "value": {
                    "Children": {
                      "refid": 10,
                      "kind": "slice",
                      "value": []
                    },
                    "Name": {
                      "refid": 8,
                      "kind": "string",
                      "value": "Ford"
                    },
                    "Parent": {
                      "refid": 9,
                      "kind": "ref",
                      "value": 1
                    }
                  }
                }
              },
              {
                "refid": 11,
                "kind": "ptr",
                "value": {
                  "refid": 12,
                  "kind": "struct",
                  "value": {
                    "Children": {
                      "refid": 15,
                      "kind": "slice",
                      "value": []
                    },
                    "Name": {
                      "refid": 13,
                      "kind": "string",
                      "value": "Trillian"
                    },
                    "Parent": {
                      "refid": 14,
                      "kind": "ref",
                      "value": 1
                    }
                  }
                }
              }
            ]
          },
          "Name": {
            "refid": 3,
            "kind": "string",
            "value": "Arthur"
          },
          "Parent": {
            "refid": 4,
            "kind": "ptr",
            "value": null
          }
        }
      }
    }

Decoding

package main

import (
	"encoding/json"
	"fmt"

	"github.com/go-extras/tahwil"
)

type Person struct {
	Name     string    `json:"name"`
	Parent   *Person   `json:"parent"`
	Children []*Person `json:"children"`
}

func prepareData() []byte {
	parent := &Person{
		Name: "Arthur",
		Children: []*Person{
			{
				Name: "Ford",
			},
			{
				Name: "Trillian",
			},
		},
	}
	parent.Children[0].Parent = parent
	parent.Children[1].Parent = parent
	v, err := tahwil.ToValue(parent)
	if err != nil {
		panic(err)
	}
	res, err := json.Marshal(v)
	if err != nil {
		panic(err)
	}
	return res
}

func main() {
	data := &tahwil.Value{}
	res := prepareData()
	err := json.Unmarshal(res, data)
	if err != nil {
		panic(err)
	}
	person := &Person{}
	err = tahwil.FromValue(data, person)
	if err != nil {
		panic(err)
	}
	fmt.Printf(`Name: %s
Children:
    - %s
	-- parent name: %s
    - %s
	-- parent name: %s
`, person.Name,
		person.Children[0].Name,
		person.Children[0].Parent.Name,
		person.Children[1].Name,
		person.Children[1].Parent.Name)
}

This should output:

Name: Arthur
Children:
    - Ford
	-- parent name: Arthur
    - Trillian
	-- parent name: Arthur

As you can see, Arthur is displayed here 3 times - first as a main person, and then as a parent of the both children.

Limitations

Supported types (kinds)

We support the following go built-in types (kinds):

  • string
  • bool
  • int
  • int8
  • int16
  • int32
  • int64
  • uint
  • uint8
  • uint16
  • uint32
  • uint64
  • float32
  • float64
  • ptr
  • struct
  • map
  • slice

In addition, tahwil adds a ref type, which is used to refer to the cyclic reference and thus avoid endless recursion.

Structs, slices, maps and pointers must pointer to one of the supported types.

cgsslwg's People

Contributors

denisvmedia avatar rkrisanoff 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.