jsonmap

package module
v1.0.4 Latest Latest
Warning

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

Go to latest
Published: Mar 29, 2019 License: Apache-2.0 Imports: 5 Imported by: 0

README

jsonmap

Circle CI Go Report Card GoDoc GitHub stars GitHub license

datasweet-logo

Jsonmap is a Go package to parse and use raw JSON with silent as Javascript language. Jsonmap was inspired by go-simplejson and the lodash javascript library.

Installation

go get github.com/datasweet/jsonmap

Usage

Parsing a new json
import (
  "fmt"
  "github.com/datasweet/jsonmap"
)

func main() {
  j := jsonmap.FromString(`{ "the": { "best": { "pi": 3.14 } } }`)
  i := j.Get("the.best.pi").AsInt()
  fmt.Println(i)
}
Lodash utilities

You can use some lodash function utilities :

  • Filter
  • Map
  • Assign
  • etc.
Tabify

Tabify was created to flatten a json into tabular datas. We created this functionality to flatten json response from elasticsearch.

Who are we ?

We are Datasweet, a french startup providing full service (big) data solutions.

Questions ? problems ? suggestions ?

If you find a bug or want to request a feature, please create a GitHub Issue.

License

This software is licensed under the Apache License, version 2 ("ALv2"), quoted below.

Copyright 2017-2018 Datasweet <http://www.datasweet.fr>

Licensed under the Apache License, Version 2.0 (the "License"); you may not
use this file except in compliance with the License. You may obtain a copy of
the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
License for the specific language governing permissions and limitations under
the License.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func IsNil

func IsNil(json *Json) bool

IsNil checks if value is null or undefined.

Types

type Json

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

Json is our wrapper to an unmarshalled json

func Assign

func Assign(source *Json, json ...*Json) *Json

Assign Assigns own enumerable string keyed properties of source objects to the destination object.

func Filter

func Filter(collection []*Json, predicate func(v *Json) bool) []*Json

Filter iterates over elements of collection, returning an array of all elements predicate returns truthy for.

func FromBytes

func FromBytes(bytes []byte) *Json

FromBytes to creates a Json from bytes

func FromMap

func FromMap(m map[string]interface{}) *Json

FromMap to creates a Json from an unmarshalled map

func FromString

func FromString(str string) *Json

FromString to creates a Json from a string

func Map

func Map(collection []*Json, iteratee func(v *Json) *Json) []*Json

Map creates an array of values by running each element in collection thru iteratee

func New

func New() *Json

New creates an empty object Json, ie {}

func Nil

func Nil() *Json

Nil creates an nil Json

func (*Json) AsArray

func (j *Json) AsArray() []interface{}

AsArray casts underlying to array ([]interface{}) Returns nil if not an array

func (*Json) AsBool

func (j *Json) AsBool() bool

AsBool casts underlying to boolean Returns false if not a boolean

func (*Json) AsFloat

func (j *Json) AsFloat() float64

AsFloat casts underlying to float64 Returns 0 if not a float

func (*Json) AsInt

func (j *Json) AsInt() int64

AsInt casts underlying to int64 Returns 0 if not an int

func (*Json) AsObject

func (j *Json) AsObject() map[string]interface{}

AsObject casts underlying to object (map[string]interface{}) Returns nil if not an object

func (*Json) AsString

func (j *Json) AsString() string

AsString casts underlying to string Returns an empty string if not a string

func (*Json) AsUint

func (j *Json) AsUint() uint64

AsUint casts underlying to uint64 Returns 0 if not an int

func (*Json) Bytes

func (j *Json) Bytes() []byte

Bytes return json bytes

func (*Json) Data

func (j *Json) Data() interface{}

Data get uncasted data

func (*Json) ForEach

func (j *Json) ForEach(iteratee func(k string, v *Json) bool)

ForEach : Iterates over elements of collection and invokes iteratee for each element. Iteratee functions may exit iteration early by explicitly returning false.

func (*Json) Get

func (j *Json) Get(path string) *Json

Get gets the value at path of object. If not found returns Nils() value

func (*Json) Has

func (j *Json) Has(path string) bool

Has checks if path is a direct property of object.

func (*Json) IsArray

func (j *Json) IsArray() bool

IsArray to check if the current Json is an array

func (*Json) IsNil

func (j *Json) IsNil() bool

IsNil to check if the current Json is nil

func (*Json) IsObject

func (j *Json) IsObject() bool

IsObject to know if the current Json is an object

func (*Json) IsValue

func (j *Json) IsValue() bool

IsValue to check if the current Json is a type value

func (*Json) Keys

func (j *Json) Keys() []string

Keys : creates an array of the own property names of object.

func (*Json) MarshalJSON

func (j *Json) MarshalJSON() ([]byte, error)

MarshalJSON implements marshaler interface from encoding/json encode.go

func (*Json) Rewrite

func (j *Json) Rewrite(oldPath string, newPath string) bool

Rewrite changes a path

func (*Json) Set

func (j *Json) Set(path string, value ...interface{}) bool

Set sets the value at path of object. If a portion of path doesn't exist, it's created. Arrays are created for missing index properties while objects are created for all other missing properties

func (*Json) SetJSON

func (j *Json) SetJSON(path string, json ...*Json) bool

SetJSON to sets a json or an array of json to path

func (*Json) Stringify

func (j *Json) Stringify() string

Stringify formats current node to a json string

func (*Json) UnmarshalJSON

func (j *Json) UnmarshalJSON(data []byte) error

UnmarshalJSON implements unmarshaler interface from encoding/json decode.go

func (*Json) Unset

func (j *Json) Unset(path string) bool

Unset deletes the value

func (*Json) Values

func (j *Json) Values() []*Json

Values : creates an array of the own enumerable string keyed property values of object.

func (*Json) Wrap

func (j *Json) Wrap(path string) *Json

Wrap the current json to a new json Example : { "pi": 3.14 }.Wrap("const") => { "const": { "pi": 3.14 }} } Returns the new parent or nilJson if error

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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