github.com/dgraph-io/simdjson-go@v0.3.0/examples/simdjson_example.go (about)

     1  package main
     2  
     3  import (
     4  	"fmt"
     5  	"io/ioutil"
     6  	"log"
     7  
     8  	"github.com/minio/simdjson-go"
     9  )
    10  
    11  func printKey(iter simdjson.Iter, key string) (err error) {
    12  
    13  	obj, tmp, elem := &simdjson.Object{}, &simdjson.Iter{}, simdjson.Element{}
    14  
    15  	for {
    16  		typ := iter.Advance()
    17  
    18  		switch typ {
    19  		case simdjson.TypeRoot:
    20  			if typ, tmp, err = iter.Root(tmp); err != nil {
    21  				return
    22  			}
    23  
    24  			if typ == simdjson.TypeObject {
    25  				if obj, err = tmp.Object(obj); err != nil {
    26  					return
    27  				}
    28  
    29  				e := obj.FindKey(key, &elem)
    30  				if e != nil && elem.Type == simdjson.TypeString {
    31  					v, _ := elem.Iter.StringBytes()
    32  					fmt.Println(string(v))
    33  				}
    34  			}
    35  
    36  		default:
    37  			return
    38  		}
    39  	}
    40  }
    41  
    42  func main() {
    43  	if !simdjson.SupportedCPU() {
    44  		log.Fatal("Unsupported CPU")
    45  	}
    46  	msg, err := ioutil.ReadFile("parking-citations.json")
    47  	if err != nil {
    48  		log.Fatalf("Failed to load file: %v", err)
    49  	}
    50  
    51  	parsed, err := simdjson.ParseND(msg, nil)
    52  	if err != nil {
    53  		log.Fatalf("Failed to parse JSON: %v", err)
    54  	}
    55  
    56  	printKey(parsed.Iter(), "Make")
    57  }