jsone

package module
v0.0.0-...-f084f88 Latest Latest
Warning

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

Go to latest
Published: Jan 27, 2020 License: Apache-2.0 Imports: 7 Imported by: 0

README

JsonE

Single framework to parse and dynamically create/modify Json objects.

godoc travis-ci codecov Go Report Card

Install

go get github.com/libujacob/jsone

Usage

Create Json

To create this:

{  
    "name":"Ricardo Longa",
    "idade":28,
    "owner":true,
    "skills":[  
        "Golang",
        "Android"
    ]
}

Do this:

import (
    j "github.com/libujacob/jsone"
)

json := j.Object().Put("name", "Ricardo Longa").
				   Put("idade", 28).
				   Put("owner", true).
				   Put("skills", j.Array().Put("Golang").
									       Put("Android"))

log.Println(json.Indent())
log.Println(json.String())
Convert object/array to indented String:
json.Indent()
Convert object/array to String:
json.String()
To remove a field of the object:
json.Remove("skills")
To get a field of the object:
json.Get("skills") // Return is interface{}.
To get string field of the object:
skill, err := json.GetString("skills") // Return is string, error
To get int/int64 field of the object:
count, err := json.GetInt("count") // Return is int, error
bytes, err := json.GetInt64("bytes") // Return is int64, error
To get float64 field of the object:
average, err := json.GetFloat64("average") // Return is float64, error
To get boolean field of the object:
isDownSupport, err := json.GetBoolean("isDownloadSupported") // Return is boolean, error
To check the object has a key:
if json.Has("operations") { // Return is boolean
    //do something
}
To range over a array:
results := Array().Put("Golang").Put("Android").Put("Java")

for i, result := range results.Array() {
}
To get Array size:
array := j.Array().Put("Android").
                   Put("Golang").
                   Put("Java")
                   
array.Size() // Result is 3.
Parse Json

Json can be directly of Object or Array type. Both can be parsed using two different APIs which are mentioned below. After parsing you can use all the above said operations on the return value.

Parse a Json Object string:
import (
    j "github.com/libujacob/jsone"
)

parsedObject := j.ParseJsonObject([]byte(`{"type": "oper", "nameList":["John", "Dan"], "id":205896}`))
/*{
    "type": "oper",
    "nameList": [
      "John",
      "Dan"
    ],
    "id": 205896
  }*/

parsedObject.Put("dept", "Operations")
/*{
    "type": "oper",
    "nameList": [
      "John",
      "Dan"
    ],
    "id": 205896,
    "dept": "Operations"
  }*/
Parse a Json Array string:
import (
    j "github.com/libujacob/jsone"
)

parsedArray := j.ParseJsonArray([]byte(`[{"name": "John", "id": 567314}, {"name": "Dan", "id": 589725}]`))
/*[
    {
      "name": "John",
      "id": 567314
    },
    {
      "name": "Dan",
      "id": 589725
    }
  ]*/

parsedArray.Put(j.Object().Put("name", "Tom").Put("id", 589289)
/*[
    {
      "name": "John",
      "id": 567314
    },
    {
      "name": "Dan",
      "id": 589725
    },
    {
      "name": "Tom",
      "id": 589289
    }
  ]*/

Original work Copyright (c) 2015 Ricardo Longa.
Modified work Copyright (c) 2020 Libu Jacob Varghese.

Using bramp.net/antlr4/json and antlr for json parsing.

JsonE is licensed under the Apache License Version 2.0. See the LICENSE file for more information.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type A

type A []interface{}

A A represents Json Array.

func Array

func Array() *A

Array creates a json array.

func ParseJsonArray

func ParseJsonArray(inData []byte) *A

ParseJsonArray parse an array to generate the json map structure

func (*A) Indent

func (jArray *A) Indent() string

Indent generates a string of a json array with indent for formatting.

func (*A) OfString

func (jArray *A) OfString() (values []string, err error)

OfString convert the json array into an array of strings.

func (*A) Put

func (jArray *A) Put(value interface{}) *A

Put inserts an element into a json array.

func (*A) Size

func (jArray *A) Size() int

Size operation gets the number of elements in an array.

func (*A) String

func (jArray *A) String() string

String generates a string representation of a json array.

type O

type O map[string]interface{}

O represents a Json Object.

func Object

func Object() O

Object creates a json object.

func ParseJsonObject

func ParseJsonObject(inData []byte) O

ParseJsonObject parse the json object to generate the json map structure

func (O) Get

func (jObj O) Get(key string) interface{}

Get retrieves an element from a json object. Type of the return value is not predefined, caller has to check the return type.

func (O) GetArray

func (jObj O) GetArray(key string) (newArray *A, err error)

GetArray retrieves a json array data from a json object. Return error, if key not exist or data type not json array.

func (O) GetBoolean

func (jObj O) GetBoolean(key string) (bool, error)

GetBoolean retrieves a boolean data from a json object. Return error, if key not exist or data type not boolean.

func (O) GetFloat64

func (jObj O) GetFloat64(key string) (float64, error)

GetFloat64 retrieves a float64 data from a json object. Return error, if key not exist or data type not float64.

func (O) GetInt

func (jObj O) GetInt(key string) (int, error)

GetInt retrieves an int data from a json object. Return error, if key not exist or data type not int.

func (O) GetInt64

func (jObj O) GetInt64(key string) (int64, error)

GetInt64 retrieves an int64 data from a json object. Return error, if key not exist or data type not int64.

func (O) GetObject

func (jObj O) GetObject(key string) (value O, err error)

GetObject retrieves a json object data from a json object. Return error, if key not exist or data type not json object.

func (O) GetString

func (jObj O) GetString(key string) (string, error)

GetString retrieves a string data from a json object. Return error, if key not exist or data type not string.

func (O) Has

func (jObj O) Has(key string) bool

Has checks the object has an element in the name of the input string. Returns true if present, else false.

func (O) Indent

func (jObj O) Indent() string

Indent on object generates a string representation of json object with proper indent.

func (O) Keys

func (jObj O) Keys() (objKeys []string)

Keys returns the list of keys in the current object.

func (O) Put

func (jObj O) Put(key string, value interface{}) O

Put inserts an element into a json object.

func (O) Remove

func (jObj O) Remove(key string) O

Remove an element from a json object.

func (O) String

func (jObj O) String() string

String on object generates a string representation of json object.

type Stack

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

Stack data structure.

func NewStack

func NewStack() *Stack

NewStack creates a new Stack data structure object.

func (*Stack) Pop

func (s *Stack) Pop() interface{}

Pop out an entry from the stack.

func (*Stack) Push

func (s *Stack) Push(value interface{})

Push a new entry to the stack.

func (*Stack) Size

func (s *Stack) Size() int

Size returns the number of the elements in the stack.

func (*Stack) Top

func (s *Stack) Top() interface{}

Top gets the last inserted entry with out deleting from the stack.

Jump to

Keyboard shortcuts

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