ourjson

package module
v0.0.0-...-341d481 Latest Latest
Warning

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

Go to latest
Published: Jun 13, 2018 License: MIT Imports: 4 Imported by: 3 modules, 4 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

ourjson

Ourjson is an easy to use JSON library for go. It is designed just like Fastjson in Java.

Install

go get github.com/W1llyu/ourjson

Import

import (
  "github.com/W1llyu/ourjson"
)

Usage

Create a JsonObject from string
jsonObject, err := ParseObject(jsonStr)
Create a JsonArray from string
jsonArray, err := ParseArray(jsonStr)
Get a Child JsonObject
jsonObject := parentObject.GetJsonObject("user")
jsonObject := parentArray.GetJsonObject(0)
Get values
name, err := parentObject.GetString("name")
age, err := parentObject.GetInt("age")
id, err := parentObject.GetInt64("id")
isMarried, err := parentObject.GetBoolean("is_married")
Get values which could be null
age, err := parentObject.GetNullInt("age")
id, err := parentObject.GetNullLong("id")
isMarried, err := parentObject.GetNullBoolean("is_married")
Chaining call
balance, err := parentObject.GetJsonObject("user").GetJsonObject("account").GetFloat("balance")
email, err := parentObject.GetJsonObject("user").GetJsonArray("emails").GetString(0)

Example

package main

import (
  "fmt"
  "github.com/W1llyu/ourjson"
)

func main() {
	defer func() {
		if err := recover(); err != nil {
			fmt.Println(err)
		}
	}()
	jsonStr := `{
		"user": {
			"name": "aa",
			"age": 10,
			"phone": "12222222222",
			"emails": [
				"aa@164.com",
				"aa@165.com"
			],
			"address": [
				{
					"number": "101",
					"now_live": true
				},
				{
					"number": "102",
					"now_live": null
				}
			],
			"account": {
				"balance": 999.9
			}
		}
	}
	`
	jsonObject, err := ourjson.ParseObject(jsonStr)
	fmt.Println(jsonObject, err)

	user := jsonObject.GetJsonObject("user")
	fmt.Println(user)

	name, err := user.GetString("name")
	fmt.Println(name, err)

	phone, err := user.GetInt64("phone")
	fmt.Println(phone, err)

	age, err := user.GetInt64("age")
	fmt.Println(age, err)

	account := user.GetJsonObject("account")
	fmt.Println(account)

	balance, err := account.GetFloat64("balance")
	fmt.Println(balance, err)

	email1, err := user.GetJsonArray("emails").GetString(0)
	fmt.Println(email1, err)

	address := user.GetJsonArray("address")
	fmt.Println(address)

	address1nowLive, err := user.GetJsonArray("address").GetJsonObject(0).GetBoolean("now_live")
	fmt.Println(address1nowLive, err)

	address2, err := address.Get(1)
	fmt.Println(address2, err)

	address2NowLive, err := address2.JsonObject().GetNullBoolean("now_live")
	fmt.Println(address2NowLive, err)
}

Documentation

Index

Constants

View Source
const (
	JSONOBJECTTYPE = "JsonObject"
	JSONARRAYTYPE  = "JsonArray"
	STRINGTYPE     = "string"
	BOOLEANTYPE    = "bool"
)

Variables

View Source
var (
	IndexOutOfRangeError = errors.New("index out of range")
	ValueNotNumberError  = errors.New("value is not number")
)

Functions

This section is empty.

Types

type Boolean

type Boolean struct {
	Valid bool
	Value bool
}

---------- Boolean ----------

func (*Boolean) UnmarshalJSON

func (b *Boolean) UnmarshalJSON(data []byte) error

type Float

type Float struct {
	Valid bool
	Value float64
}

---------- Float ----------

func (*Float) UnmarshalJSON

func (b *Float) UnmarshalJSON(data []byte) error

type Integer

type Integer struct {
	Valid bool
	Value int
}

---------- Integer ----------

func (*Integer) UnmarshalJSON

func (b *Integer) UnmarshalJSON(data []byte) error

type JsonArray

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

func ParseArray

func ParseArray(jsonStr string) (*JsonArray, error)

func (*JsonArray) Get

func (j *JsonArray) Get(index int) (*Value, error)

func (*JsonArray) GetBoolean

func (j *JsonArray) GetBoolean(index int) (bool, error)

func (*JsonArray) GetFloat64

func (j *JsonArray) GetFloat64(index int) (float64, error)

func (*JsonArray) GetInt

func (j *JsonArray) GetInt(index int) (int, error)

func (*JsonArray) GetInt64

func (j *JsonArray) GetInt64(index int) (int64, error)

func (*JsonArray) GetJsonArray

func (j *JsonArray) GetJsonArray(index int) *JsonArray

func (*JsonArray) GetJsonObject

func (j *JsonArray) GetJsonObject(index int) *JsonObject

func (*JsonArray) GetNullBoolean

func (j *JsonArray) GetNullBoolean(index int) (*Boolean, error)

func (*JsonArray) GetNullFloat

func (j *JsonArray) GetNullFloat(index int) (*Float, error)

func (*JsonArray) GetNullInt

func (j *JsonArray) GetNullInt(index int) (*Integer, error)

func (*JsonArray) GetNullLong

func (j *JsonArray) GetNullLong(index int) (*Long, error)

func (*JsonArray) GetString

func (j *JsonArray) GetString(index int) (string, error)

func (*JsonArray) Values

func (j *JsonArray) Values() []*Value

type JsonObject

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

func ParseObject

func ParseObject(jsonStr string) (*JsonObject, error)

func (*JsonObject) Get

func (j *JsonObject) Get(key string) (*Value, error)

func (*JsonObject) GetBoolean

func (j *JsonObject) GetBoolean(key string) (bool, error)

func (*JsonObject) GetFloat64

func (j *JsonObject) GetFloat64(key string) (float64, error)

func (*JsonObject) GetInt

func (j *JsonObject) GetInt(key string) (int, error)

func (*JsonObject) GetInt64

func (j *JsonObject) GetInt64(key string) (int64, error)

func (*JsonObject) GetJsonArray

func (j *JsonObject) GetJsonArray(key string) *JsonArray

Get a child node of JsonArray from this parent node This will raise an error when the value if not existed or not a JsonArray Considering having a recover function with it

func (*JsonObject) GetJsonObject

func (j *JsonObject) GetJsonObject(key string) *JsonObject

Get a child node of JsonObject from this parent node This will raise an error when the value is not existed or not a JsonObject Considering having a recover function with it

func (*JsonObject) GetNullBoolean

func (j *JsonObject) GetNullBoolean(key string) (*Boolean, error)

Get a boolean which could be null example:

   isHit, _ := jsonObject.GetNullBoolean("is_hit")
   if !i.Valid {
       fmt.Println("unknown")
       return
   }
	  if i.Value {
       fmt.Println("hit!")
   } else {
	      fmt.Println("now hit)
   }

func (*JsonObject) GetNullFloat

func (j *JsonObject) GetNullFloat(key string) (*Float, error)

func (*JsonObject) GetNullInt

func (j *JsonObject) GetNullInt(key string) (*Integer, error)

Get an integer which could be null example:

   i, _ := jsonObject.GetNullInt("age")
   if !i.Valid {
       fmt.Println("null age")
   } else {
       fmt.Printf("the age is %d", i.Value)
	  }

func (*JsonObject) GetNullLong

func (j *JsonObject) GetNullLong(key string) (*Long, error)

func (*JsonObject) GetString

func (j *JsonObject) GetString(key string) (string, error)

func (*JsonObject) HasKey

func (j *JsonObject) HasKey(key string) bool

Check if the key is existed

func (*JsonObject) Put

func (j *JsonObject) Put(key string, val interface{})

type KeyNotFoundError

type KeyNotFoundError struct {
	Key string
}

func (KeyNotFoundError) Error

func (e KeyNotFoundError) Error() string

type Long

type Long struct {
	Valid bool
	Value int64
}

---------- Long ----------

func (*Long) UnmarshalJSON

func (b *Long) UnmarshalJSON(data []byte) error

type Value

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

func (*Value) Boolean

func (v *Value) Boolean() (bool, error)

func (*Value) Float64

func (v *Value) Float64() (float64, error)

func (*Value) Int

func (v *Value) Int() (int, error)

func (*Value) Int64

func (v *Value) Int64() (int64, error)

func (*Value) JsonArray

func (v *Value) JsonArray() *JsonArray

func (*Value) JsonObject

func (v *Value) JsonObject() *JsonObject

func (*Value) MarshalJSON

func (v *Value) MarshalJSON() ([]byte, error)

func (*Value) NullBoolean

func (v *Value) NullBoolean() (*Boolean, error)

func (*Value) NullFloat

func (v *Value) NullFloat() (*Float, error)

func (*Value) NullInt

func (v *Value) NullInt() (*Integer, error)

func (*Value) NullLong

func (v *Value) NullLong() (*Long, error)

func (*Value) String

func (v *Value) String() (string, error)

type ValueTransformTypeError

type ValueTransformTypeError struct {
	Type string
}

func (ValueTransformTypeError) Error

func (e ValueTransformTypeError) Error() string

Jump to

Keyboard shortcuts

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