Git Product home page Git Product logo

json's Introduction

zkjson

zero knowledge json(zkjson) is a very simple library that only provides marshalling.

When we calls external service, we usually don't cares its request structure.

For example:

{
	"user_id": "xnum",
	"token": "27364107346120846143",
	"action": "buy",
	"req": [{
			"item": "apple",
			"amount": 100
		},
		{
			"item": "bear",
			"amount": 200
		},
		{
			"item": "charlie",
			"amount": 300
		}
	]
}

In Golang, you have to declare a structure with json tag to marshal json. So you are writing...

type req struct {
	UserID string `json:"user_id"`
	Token string `json:"token"`
	Action string `json:"buy"`
	Reqs []Orders `json:"req"`
}

req := &req{
	UserID: "xnum",
	....
}

I don't wanna write type, struct to deal with these dynamic json anymore.

Lots of JSON lib focus on how to unmarshal or parse and nobody provides an easy to use approach. So there is the solution.

Install

go get -u -v github.com/xnum/json

Array

func main() {
	arrName := zkjson.Array("Alice", "Bob")
	arrName.Append("Kate", "John")

	arrScore := zkjson.Array()
	arrScore.Append(55, 66, 77, 88)

	students := zkjson.Object( // Using `Object` to combine it.
	    zkjson.Any("name", arrName),
	    zkjson.Any("score", arrScore),
	)

	fmt.Println(arrName)
	fmt.Println(arrScore)
	fmt.Println(students)
}

output:

["Alice","Bob","Kate","John"]
[70,80,90,100]
{"name":["Alice","Bob","Kate","John"],"score":[70,80,90,100]}

object/member

func main() {
	// Object accepts key-value pair.
	ezObj := zkjson.Object(
	    zkjson.Attr("Url", "http://www.google.com"),
	    zkjson.Attr("TTL", 128),
	    zkjson.Attr("Delay", 3.21),
	)

	fmt.Println(ezObj)
}

output:

{"Url":"http://www.google.com","TTL":128,"Delay":3.21}

complex struct

func main() {
	res := zkjson.Object(
		zkjson.Attr("name", "xnum"),
		zkjson.Attr("age", 27),
		zkjson.Attr("skill",
			zkjson.Array("C/C++", "Golang", "PHP"),
		),
		zkjson.Attr("no idea", zkjson.Object()), 
		zkjson.Attr("education", zkjson.Object(
			zkjson.Attr("NCTU", "MS"),
			zkjson.Attr("NTCU", "BS"),
		)),
	)

	fmt.Println(res)
}
{"name": "xnum","age": 27,"skill": ["C/C++","Golang","PHP"],"no idea": {},"education": {"NCTU": "MS","NTCU": "BS"}}

json's People

Contributors

xnum avatar

Watchers

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