goson

package module
v0.0.0-...-27f0da4 Latest Latest
Warning

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

Go to latest
Published: Oct 21, 2016 License: MIT Imports: 2 Imported by: 1

README

Goson

Handle JSON with ease in golang.

About

Goson was created to simplify reading JSON data within Golang. This library has been inspired by SwiftyJSON

Install

go get github.com/panthesingh/goson

Starting

Create a goson object from JSON data. Returns an error if the data is not valid JSON.

g, err := goson.Parse(data)

Data

Every Get() call will return another goson object. You can access the underlying data with a value function. The default value types are float64, int, bool, string. If the key does not exist the function will return the default zero value. To check if a key exists read the section on existence.

name := g.Get("name").String()
age := g.Get("age").Int()
weight := g.Get("weight").Float()
married := g.Get("married").Bool()

Chaining

Chaining is a nice way to quickly traverse the data and grab what you need.

g.Get("key").Get("object").Index(0).Get("item").String()

Existance

To check if a value exists use a type check on the Value() function. This returns the underlying value as an interface{}.

v, ok := g.Get("key").Value().(string)
if !ok {
  println("key does not exist")
}

Loop

Calling Len() will return len() on the underlying value. You can use the Index() function to loop through all the values.

for i := 0; i < g.Len(); i++ {
    name := g.Index(i).Get("name").String()
    age := g.Index(i).Get("age").Int()
}

Printing

A very useful feature is pretty printing the JSON structure at any value. Likewise calling String() returns the same string.

v := g.Get("child")
fmt.Println(v)

Example

package main

import (
  "github.com/panthesingh/goson"
)

func main() {

  json := `{
    "name": "Bob",
    "age": 100,
    "cars": [
      "Honda",
      "Toyota"
    ],
    "details": {
      "weight": 160.5,
      "married": false
    }
  }`

  g, _ := goson.Parse([]byte(json))
  name := g.Get("name").String()
  age := g.Get("age").Int()
  cars := g.Get("cars")
  carOne := car.Index(0).String()
  carTwo := car.Index(1).String()
  weight := g.Get("details").Get("weight").String()
  married := g.Get("details").Get("married").Bool()

}

Documentation

Documentation can be found on godoc:

https://godoc.org/github.com/panthesingh/goson

Author

Panthe Singh, http://twitter.com/panthesingh

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Goson

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

Goson object

func New

func New(i interface{}) *Goson

func Parse

func Parse(data []byte) (*Goson, error)

Parse will create a goson object from json data

func (*Goson) Bool

func (g *Goson) Bool() bool

Bool returns the bool value.

func (*Goson) Float

func (g *Goson) Float() float64

Float returns the underlying float64 value.

func (*Goson) Get

func (g *Goson) Get(key string) *Goson

Get returns a goson object from a key. If the value does not exist this will still return a goson object.

func (*Goson) Index

func (g *Goson) Index(index int) *Goson

Index is used to access the index of an array object.

func (*Goson) Int

func (g *Goson) Int() int

Int returns the underlying Int value converted from a float64.

func (*Goson) Len

func (g *Goson) Len() int

Len will return len() on the underlying value. If the value does not have a length the return value will be 0.

func (*Goson) Map

func (g *Goson) Map() map[string]interface{}

Map returns the underlying map value.

func (*Goson) Set

func (g *Goson) Set(key string, value interface{}) *Goson

Set returns a goson object after change the value If the value does not exist this will still return a goson object.

func (*Goson) Slice

func (g *Goson) Slice() []interface{}

Slice returns the underlying slice value.

func (*Goson) String

func (g *Goson) String() string

String will convert the underlying value as a string if it can or return an empty string. If the object is a JSON map or array it will return the structure in an indented format.

func (*Goson) Value

func (g *Goson) Value() interface{}

Value will retrieve the underlying interface value.

Jump to

Keyboard shortcuts

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