kson

package
v0.0.0-...-6c31828 Latest Latest
Warning

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

Go to latest
Published: Apr 12, 2013 License: MIT Imports: 12 Imported by: 3

README

kson

Package kson implements encoding and decoding of kson

overview

  • kson, Keep It Simple & Stupid Object Notation
  • json-like style, doesn't need quote name with "" and values are separated by \n
  • doesn't care data type, doesn't depend on indent(yaml)
  • design for some usual scene, such as config file, print readable dump

kson example

Name:	value 		
Int:	-1024		
Float:	6.4			
Bool:	true		
Date: 	2012-12-21
String:	abcdefghijklmnopqrstuvwxyz/字符串 #:[]{} 
Quote:	"[0,1,2,3,4,5,6,7,8,9]"

Json: 	`
		var father = {
		    "Name": "John",
		    "Age": 32,
		    "Children": [
		        {
		            "Name": "Richard",
		            "Age": 7
		        },
		        {
		            "Name": "Susan",
		            "Age": 4
		        }
		    ]
		};
	`		
Xml: "	<root>
			<!-- a node -->
			<text>
				I'll be back
			</text>
		</root>

	"
Empty:	

list example

[
	line one
	"[line two]"
	"

	line three

	"
]

hash example

{				
	int:	1024	
	float:	6.4		
	bool:	true	
	string:	string	
	text: 	"
			I'm not a great programmer, 
			I'm a pretty good programmer with great habits.
			"
} 

compose example

{	
	Log_Level:	debug
	Listen:		8000

	Roles: [
		{
			Name:	user
			Allow:	[
				/user		
				/order
			]
		} 
		{
			Name:	*				
			Deny: 	[
				/user
				/order
			]
		} 
	]

	Db_Log:	{
		Driver:		mysql			
		Host: 		127.0.0.1
		User:		user
		Password:	password
		Database:	log
	}

	Env:	{
		auth:		http://auth.io
		browser:	ie, chrome, firefox, safari
		key:
	}
}	

usage

Marshal & Unmarshal example

func example() {
	t := newConfig()

	b, err := kson.Marshal(t)
	if err != nil {
		fmt.Println("kson.Marshal error", err, b)
		return
	}
	fmt.Println(string(b))

	var p Config
	err = kson.Unmarshal(b, &p)
	if err != nil {
		fmt.Println("kson.Unmarshal error", err)
		return
	}

	b, err = json.Marshal(p)
	fmt.Println(string(b))

}

Parse to kson.Node example

func parse() {
	data := `
	{				
		int:	-1024	
		float:	6.4		
		bool:	true	
		string:	string			
		list:	[
					line 1
					line 2
					line 3
				]
	} 
	`
	node, err := kson.Parse([]byte(data))
	if err != nil {
		fmt.Println("kson.Parse error", err)
		return
	}

	fmt.Println("int", node.ChildInt("int"))
	fmt.Println("float", node.ChildFloat("float"))
	fmt.Println("bool", node.ChildBool("bool"))
	fmt.Println("string", node.ChildString("string"))

	var list []string
	if child, ok := node.Child("list"); ok {
		child.Value(&list)
		fmt.Println("list", list)
	}

	hash := make(map[string]string)
	node.Value(&hash)
	fmt.Println("hash", hash)

}

For more example usage, please see *_test.go or example.go

Performance

It should be faster than json

cache reflect.type info maybe improve performance, haven't test it yet

License

MIT

Documentation

Index

Constants

View Source
const (
	NodeNone = iota
	NodeLiteral
	NodeHash
	NodeList
)

Variables

This section is empty.

Functions

func Marshal

func Marshal(a interface{}) (data []byte, err error)

Marshal returns the kson encoding of v.

func Unmarshal

func Unmarshal(data []byte, v interface{}) error

Unmarshal unmarshal data to the value pointed to by v.

Types

type FormatError

type FormatError struct {
	Message string
}

func (*FormatError) Error

func (e *FormatError) Error() string

type InvalidNodeTypeError

type InvalidNodeTypeError struct {
	NodeType int
}

func (*InvalidNodeTypeError) Error

func (e *InvalidNodeTypeError) Error() string

type Node

type Node struct {
	Type    int
	Literal string
	List    []*Node
	Hash    map[string]*Node
}

Node

func Parse

func Parse(data []byte) (node *Node, err error)

func ParseFile

func ParseFile(filename string) (node *Node, err error)

ParseFile parse a file, remove any line start with #(comments), add {\} auto at begin and end

func (*Node) Bool

func (n *Node) Bool() (b bool, err error)

Bool returns n's underlying value, as an bool.

func (*Node) Child

func (n *Node) Child(name string) (child *Node, ok bool)

Child return child node by name, ok is false if name doesn't exist

func (*Node) ChildBool

func (n *Node) ChildBool(name string) bool

ChildBool return child value as bool

func (*Node) ChildBoolOrDefault

func (n *Node) ChildBoolOrDefault(name string, defaultValue bool) bool

ChildBoolOrDefault return child value as bool, return defaultValue if child doesn't exist

func (*Node) ChildFloat

func (n *Node) ChildFloat(name string) float64

ChildFloat return child value as float64

func (*Node) ChildFloatOrDefault

func (n *Node) ChildFloatOrDefault(name string, defaultValue float64) float64

ChildFloatOrDefault return child value as float64, return defaultValue if child doesn't exist

func (*Node) ChildFold

func (n *Node) ChildFold(name string) (child *Node, ok bool)

Child return child node by name, ok is false if name doesn't exist

func (*Node) ChildInt

func (n *Node) ChildInt(name string) int64

ChildInt return child value as int64

func (*Node) ChildIntOrDefault

func (n *Node) ChildIntOrDefault(name string, defaultValue int64) int64

ChildIntOrDefault return child value as int64, return defaultValue if child doesn't exist

func (*Node) ChildString

func (n *Node) ChildString(name string) string

ChildString return child value as string

func (*Node) ChildStringOrDefault

func (n *Node) ChildStringOrDefault(name string, defaultValue string) string

ChildStringOrDefault return child value as string, return defaultValue if child doesn't exist

func (*Node) ChildUint

func (n *Node) ChildUint(name string) uint64

ChildUint return child value as uint64

func (*Node) ChildUintOrDefault

func (n *Node) ChildUintOrDefault(name string, defaultValue uint64) uint64

ChildUintOrDefault return child value as uint64, return defaultValue if child doesn't exist

func (*Node) Dump

func (n *Node) Dump() string

Dump return dump of node as string

func (*Node) Float

func (n *Node) Float() (f float64, err error)

Float returns n's underlying value, as an float64.

func (*Node) Int

func (n *Node) Int() (i int64, err error)

Int returns n's underlying value, as an int64.

func (*Node) Map

func (n *Node) Map() (data map[string]string, err error)

Map returns map[string]string

func (*Node) MustChild

func (n *Node) MustChild(name string) *Node

Child return child node by name, ok is false if name doesn't exist

func (*Node) Query

func (n *Node) Query(path string) (child *Node, ok bool)

Query return child node by path, just like query

func (*Node) Slice

func (n *Node) Slice() (data []string, err error)

Slice returns []string.

func (*Node) String

func (n *Node) String() (s string, err error)

String returns n's underlying value, as an string.

func (*Node) Uint

func (n *Node) Uint() (i uint64, err error)

Uint returns n's underlying value, as an uint64.

func (*Node) Value

func (n *Node) Value(a interface{}) (err error)

Value unmarshal data to the value pointed to by a.

type NodeNotExistsError

type NodeNotExistsError struct {
	Name string
}

func (*NodeNotExistsError) Error

func (e *NodeNotExistsError) Error() string

Jump to

Keyboard shortcuts

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