项目作者: akshitgrover

项目描述 :
JSON o Golang | Forget static types, No more complex structure definitions, Focus on code. Go Reflect!
高级语言: Go
项目地址: git://github.com/akshitgrover/jogo.git
创建时间: 2018-12-30T12:14:55Z
项目社区:https://github.com/akshitgrover/jogo

开源协议:MIT License

下载



LOGO



LOGO





JSON o Golang | Forget static types, No more complex structure definitions, Focus on code. Go Reflect!

JoGO uses memoization to return results faster. JoGO facilitates handling of large and complex JSON structures by making use of go reflections and type assertions.

Take a look at benchmarks

Installing

Type the following in Command Line

go get -u github.com/akshitgrover/jogo

Usage

Import JoGO in .go source files as follows

import "github.com/akshitgrover/jogo/jogo"

Note:

JoGO is distributed as a module with one package, To use JoGO package packed within the JoGO module above import path is to be used.

To find more about go modules, Read the wiki

Export Method

Export method is used to parse underlying json and return ExportedJson, ResultJson and Error objects.

Usage

  1. package main
  2. import (
  3. "github.com/akshitgrover/jogo/jogo"
  4. "fmt"
  5. )
  6. func main() {
  7. exp, r, err := jogo.Export(`{"hello":"world"}`)
  8. if err != nil {
  9. fmt.Println(err)
  10. } else {
  11. fmt.Println(r.Type) // OBJECT
  12. _, _ = exp.Get("hello")
  13. }
  14. }

Get Method

Get method is used to fetch value from an ExportedJson object. It returns ResultJson and Error objects.

  1. package main
  2. import (
  3. "github.com/akshitgrover/jogo/jogo"
  4. "fmt"
  5. )
  6. func main() {
  7. exp, r, err := jogo.Export(`{"name":{"firstname":"akshit", "lastname":"grover"}}`)
  8. if err != nil {
  9. fmt.Println(err)
  10. } else {
  11. fmt.Println(r.Type) //OBJECT
  12. r2, _ := exp.Get("name.firstname")
  13. r3, _ := exp.Get("name.lastname")
  14. fmt.Println(r2.Type, r3.Type) //STRING STRING
  15. fmt.Println(r2.String() + r3.String())
  16. }
  17. }

R method

R method is used to convert any interface to ResultJson struct.
It accepts interface{} as an argument and returns ResultJson{}.

Note: R method makes it intuitive to iterate over Objects and Slices.

  1. package main
  2. import (
  3. "github.com/akshitgrover/jogo/jogo"
  4. "fmt"
  5. )
  6. func main() {
  7. exp, r, err := jogo.Export(`{"name":{"firstname":"akshit", "lastname":"grover"}}`)
  8. if err != nil {
  9. fmt.Println(err)
  10. } else {
  11. fmt.Println(r.Type) //OBJECT
  12. r2, _ := exp.Get("name")
  13. for k, v := range r2 {
  14. fmt.Println("Key: " + k)
  15. fmt.Println("Value: " + jogo.R(v).String())
  16. }
  17. }
  18. /* Output
  19. OBJECT
  20. Key: firstname
  21. Value: akshit
  22. --------
  23. Key: lastname
  24. Value: grover
  25. --------
  26. */
  27. }

ExportedJson

Exported Json object holds parsed JSON, If an underlying json represents an OBJECT (javascript alike),

{ExportedJson Object}.Get(“{prop1}.{prop2}.{….}.{propN}”)

Is used to access any value in that json.

Any access to the value of a JSON property is to be done using ExportedJson object’s Get Method.

ResultJson

Go being statically typed, Value fetched from GET method holds an underlying representation of value in the form of an interface, To convert it into a native type, Type assertion is to be used.

JoGO provides various method to do type assertion.

ResultJson object has Type attribute.

Types supported by JoGO

  • NUMBER
  • STRING
  • LIST
  • OBJECT
  • BOOLEAN

Note: Type attribute holds one of the above.

ResultJson type assertion methods

  • {ResultJson Object}.Int() # Returns: int64
  • {ResultJson Object}.Float() # Returns: float64
  • {ResultJson Object}.String() # Returns: string
  • {ResultJson Object}.Bool() # Returns: bool
  • {ResultJson Object}.Object() # Returns: map[string]interface {}
  • {ResultJson Object}.List() # Returns: []interface {}

Note: If underlying interface value is not the same as the one it is asserted as, go program panics.

To avoid panic state, Following methods are included.

These methods checks for interface type, If it does not match, an error is returned.

  • {ResultJson Object}.IntStrict() # Returns: int64, error
  • {ResultJson Object}.FloatStrict() # Returns: float64, error
  • {ResultJson Object}.StringStrict() # Returns: string, error
  • {ResultJson Object}.BoolStrict() # Returns: bool, error
  • {ResultJson Object}.ObjectStrict() # Returns: map[string]interface {}, error
  • {ResultJson Object}.ListStrict() # Returns: []interface {}, error

Note: These methods are slower than non-strict methods, It is advised to first check type using Type attribute then call non-strict type assert methods.

Example

  1. package main
  2. import (
  3. "github.com/akshitgrover/jogo/jogo"
  4. "fmt"
  5. )
  6. func main() {
  7. exp, _, err := jogo.Export(`{"name":{"firstname":"akshit", "lastname":"grover"}}`)
  8. if err != nil {
  9. fmt.Println(err)
  10. } else {
  11. r2, _ := exp.Get("name.firstname")
  12. if r2.Type == "STRING" {
  13. fmt.Println(r2.String()) //akshit
  14. }
  15. }
  16. }

Error

Error object is native error interface provided by go.

Benchmarks

  1. BenchmarkJoGOGet-4 15000000 317 ns/op 61 B/op 2 allocs/op
  2. BenchmarkGJSONGet-4 3000000 475 ns/op 0 B/op 0 allocs/op
  3. BenchmarkGJSONGetMany4Paths-4 4000000 470 ns/op 56 B/op 0 allocs/op
  4. BenchmarkGJSONGetMany8Paths-4 8000000 463 ns/op 56 B/op 0 allocs/op
  5. BenchmarkGJSONGetMany16Paths-4 16000000 496 ns/op 56 B/op 0 allocs/op
  6. BenchmarkGJSONGetMany32Paths-4 32000000 480 ns/op 56 B/op 0 allocs/op
  7. BenchmarkGJSONGetMany64Paths-4 64000000 485 ns/op 64 B/op 0 allocs/op
  8. BenchmarkGJSONGetMany128Paths-4 128000000 509 ns/op 64 B/op 0 allocs/op
  9. BenchmarkGJSONUnmarshalMap-4 900000 5060 ns/op 1920 B/op 26 allocs/op
  10. BenchmarkGJSONUnmarshalStruct-4 900000 4984 ns/op 992 B/op 4 allocs/op
  11. BenchmarkJSONUnmarshalMap-4 600000 11394 ns/op 2968 B/op 69 allocs/op
  12. BenchmarkJSONUnmarshalStruct-4 600000 8674 ns/op 784 B/op 9 allocs/op
  13. BenchmarkJSONDecoder-4 300000 17150 ns/op 4133 B/op 179 allocs/op
  14. BenchmarkFFJSONLexer-4 1500000 3821 ns/op 896 B/op 8 allocs/op
  15. BenchmarkEasyJSONLexer-4 3000000 1129 ns/op 501 B/op 5 allocs/op
  16. BenchmarkJSONParserGet-4 3000000 498 ns/op 21 B/op 0 allocs/op
  17. BenchmarkJSONIterator-4 3000000 1136 ns/op 677 B/op 14 allocs/op
  18. BenchmarkConvertNone-4 50000 30790 ns/op 0 B/op 0 allocs/op
  19. BenchmarkConvertGet-4 50000 39405 ns/op 49152 B/op 1 allocs/op
  20. BenchmarkConvertGetBytes-4 50000 30780 ns/op 48 B/op 1 allocs/op

Benchmark testing was done on 2.3 GHz Intel Core i5, BenchMarks funcs were taken from here.

Copyright & License

MIT License

Copyright (c) 2018 Akshit Grover