### Install Go-JitJSON Source: https://github.com/mcwalrus/go-jitjson/blob/main/README.md Installs the Go-JitJSON library using the go get command. Requires Go version 1.18 or later. ```bash go get github.com/mcwalrus/go-jitjson ``` -------------------------------- ### Unmarshal JSON Map of Objects in Go Source: https://github.com/mcwalrus/go-jitjson/blob/main/README.md Illustrates how to unmarshal a JSON map into a map of go-jitjson objects in Go. The example shows how to select and unmarshal a specific object from the map just-in-time. ```Go package main import ( "fmt" "github.com/mcwalrus/go-jitjson" ) type Person struct { Name string Age int City string } func main() { jsonMap := []byte(`{ 1: {"Name":"John","Age":30,"City":"New York"}, 2: {"Name":"Jane","Age":25,"City":"Los Angeles"}, 3: {"Name":"Jim","Age":35,"City":"Chicago"} }`) // Unmarshal map of jitjson objects var jitMap map[int]*jitjson.JitJSON[Person] err := json.Unmarshal(jsonMap, &jitMap) if err != nil { panic(err) } // Select a person by key jit, ok := jitMap[1] if !ok { panic("missing person") } // Unmarshal only person one just-in-time person, err := jit.Unmarshal() if err != nil { panic(err) } fmt.Println(person) // Output: {John 30 New York} } ``` -------------------------------- ### Bash: Run Go Benchmarks Source: https://github.com/mcwalrus/go-jitjson/blob/main/README.md Command to execute benchmarks for Go projects, including memory allocation statistics. The `-bench=.` flag runs all benchmarks, and `-benchmem` displays memory usage. This is useful for performance analysis. ```Bash go test -bench=. -benchmem ``` -------------------------------- ### Bash: Run Go Benchmarks with Parse Percentage Source: https://github.com/mcwalrus/go-jitjson/blob/main/README.md Command to run specific Go benchmarks, controlling the percentage of data parsed using the PARSE_PERCENTAGE environment variable. This allows for granular performance testing based on data volume. Requires the PARSE_PERCENTAGE environment variable to be set. ```Bash PARSE_PERCENTAGE=0.3 go test -bench='^BenchmarkParsePercentage$' -benchmem ``` -------------------------------- ### Go: Dynamic Type Inference with Arrays using AnyJitJSON Source: https://github.com/mcwalrus/go-jitjson/blob/main/README.md Demonstrates how to use AnyJitJSON for dynamic type inference with JSON arrays. It shows how to unmarshal an array containing mixed types (numbers, strings, objects, booleans) and access individual elements with type assertions. Requires the go-jitjson library. ```Go package main import ( "fmt" "github.com/mcwalrus/go-jitjson" ) func main() { jsonData := []byte(`[ 1.23, "Hello, world!", {"Name": "John", "Age": 30}, true ]`) // Support for multiple types var jit []*jitjson.AnyJitJSON err := json.Unmarshal(jsonData, &jit) if err != nil { panic(err) } num, ok := jit[0].AsNumber() if !ok { panic("not a number") } fmt.Println(num) // Output: 1.23 str, ok := jit[1].AsString() if !ok { panic("not a string") } fmt.Println(str) // Output: Hello, world! if jit[2].Type() != jitjson.Object { panic("not an object") } fmt.Println(jit[2]) // Output: {"Name": "John", "Age": 30} if jit[3].Type() != jitjson.Boolean { panic("not a boolean") } fmt.Println(jit[3]) // Output: true } ``` -------------------------------- ### Register Custom JSON Parser Source: https://github.com/mcwalrus/go-jitjson/blob/main/README.md Registers a custom jitjson.JSONParser implementation with the library. This allows for flexible parsing strategies beyond the standard Go JSON libraries. ```go jitjson.MustRegisterParser(&parser) ``` -------------------------------- ### Parse JSON Multiple Times in Go Source: https://github.com/mcwalrus/go-jitjson/blob/main/README.md Demonstrates how to parse JSON data multiple times using go-jitjson without incurring performance penalties or extra memory allocations. This is useful for scenarios where data needs to be accessed or re-processed repeatedly. ```Go package main import ( "fmt" "github.com/mcwalrus/go-jitjson" ) type Person struct { Name string Age int City string } func main() { jit := jitjson.New(Person{ Name: "John", Age: 30, City: "New York", }) _, err := jit.Marshal() // Initial Marshal if err != nil { panic(err) } for i := 0; i < 10; i++ { // Repeated Marshal without new allocations _, err = jit.Marshal() if err != nil { panic(err) } } jit = jitjson.NewFromBytes([]byte(`{ "name": "John", "age": 30, "city": "New York" }`)) _, err = jit.Unmarshal() // Initial Unmarshal if err != nil { panic(err) } for i := 0; i < 10; i++ { // Repeated Unmarshal without new allocations _, err = jit.Unmarshal() if err != nil { panic(err) } } } ``` -------------------------------- ### Go: Conditional Type Inference with AnyJitJSON Source: https://github.com/mcwalrus/go-jitjson/blob/main/README.md Illustrates dynamic type inference with AnyJitJSON for various JSON types (null, object, array, boolean). This function determines the type of the input JSON data and prints a corresponding message. It showcases the flexibility of AnyJitJSON in handling different JSON structures. Requires the go-jitjson library. ```Go package main import ( "fmt" "github.com/mcwalrus/go-jitjson" ) func whichType(data []byte) { var jit *jitjson.AnyJitJSON err := json.Unmarshal(data, &jit) if err != nil { panic(err) } switch typ := jit.Type(); typ { case jitjson.Null: fmt.Println("null") case jitjson.Object: fmt.Println("Hmmm, an object?") case jitjson.Array: fmt.Println("An array? Interesting...") default: fmt.Println("Huh, I have no idea what this is...") } } func main() { whichType([]byte(`null`)) // Output: null whichType([]byte(`{"Name": "John", "Age": 30}`)) // Output: Hmmm, an object? whichType([]byte(`[1, 2, 3]`)) // Output: An array? Interesting... whichType([]byte(`true`)) // Output: Huh, I have no idea what this is... } ``` -------------------------------- ### Set Default Parser to json/v2 Source: https://github.com/mcwalrus/go-jitjson/blob/main/README.md Configures Go-JitJSON to use the experimental encoding/json/v2 library as its default parser. This requires Go 1.25 with the GOEXPERIMENT=jsonv2 build argument. ```go jitjson.SetDefaultParser("encoding/json/v2") ``` -------------------------------- ### Marshal Value with Go-JitJSON Source: https://github.com/mcwalrus/go-jitjson/blob/main/README.md Demonstrates how to create a JitJSON object from a Go struct and then marshal it into a JSON encoding. The marshaling process is performed lazily upon calling the Marshal method. ```go package main import ( "fmt" "github.com/mcwalrus/go-jitjson" ) type Person struct { Name string Age int City string } func main() { // Create new JitJSON: jit := jitjson.New(Person{ Name: "John", Age: 30, City: "New York", }) // Marshal value just-in-time: jsonEncoding, err := jit.Marshal() if err != nil { panic(err) } fmt.Println(string(jsonEncoding)) // Output: {"age":30,"city":"New York","name":"John"} } ``` -------------------------------- ### Dynamic Type Inference with AnyJitJSON in Go Source: https://github.com/mcwalrus/go-jitjson/blob/main/README.md Explains how to use `AnyJitJSON` from go-jitjson for dynamic type inference in Go. This allows handling JSON data where the structure or type is not known beforehand, including nested arrays and objects. ```Go package main import ( "fmt" "github.com/mcwalrus/go-jitjson" ) type Person struct { Name string Age int Friends []Person } func main() { jsonData := []byte(`{ "Name": "John", "Age": 30, "Friends": [ {"Name": "Jane", "Age": 25}, {"Name": "Jim", "Age": 35}, {"Name": "Jill", "Age": 45} ] }`) // Support for multiple types using AnyJitJSON var jit jitjson.AnyJitJSON err := json.Unmarshal(jsonData, &jit) if err != nil { panic(err) } // Get the object and extract the name obj, ok := jit.AsObject() if !ok { panic("not object") } if name, ok := obj["Name"].AsString(); ok { fmt.Println(name) } // Get the friends array and process elements if friends, ok := obj["Friends"].AsArray(); ok { for _, friend := range friends { fmt.Println(friend) } // Marshal and unmarshal a friend just-in-time data, err := friends[0].Marshal() if err != nil { panic(err) } var person Person err = json.Unmarshal(data, &person) if err != nil { panic(err) } } } ``` -------------------------------- ### Update Value in Go-JitJSON Source: https://github.com/mcwalrus/go-jitjson/blob/main/README.md Illustrates how to update the underlying value of a JitJSON object using the Set method and then marshal the modified data. This demonstrates the mutability of the JitJSON structure. ```go package main import ( "fmt" "github.com/mcwalrus/go-jitjson" ) func main() { // Create new JitJSON: jit := jitjson.New(Person{ Name: "John", Age: 30, City: "New York", }) // Marshal the initial value: jsonEncoding, err := jit.Marshal() if err != nil { panic(err) } // Update the value: jit.Set(Person{ Name: "Jane", Age: 25, City: "Los Angeles", }) // Marshal the updated value: jsonEncoding, err = jit.Marshal() if err != nil { panic(err) } fmt.Println(string(jsonEncoding)) // Output: {"age":25,"city":"Los Angeles","name":"Jane"} } ``` -------------------------------- ### Unmarshal JSON with Go-JitJSON Source: https://github.com/mcwalrus/go-jitjson/blob/main/README.md Shows how to create a JitJSON object from a byte slice containing JSON data and then unmarshal it into a specific Go struct type. The unmarshaling is performed lazily when the Unmarshal method is called. ```go package main import ( "fmt" "github.com/mcwalrus/go-jitjson" ) type Person struct { Name string Age int City string } func main() { // Create new JitJSON: jit := jitjson.NewFromBytes[Person]([]byte(`{ "Name": "John", "Age": 30, "City": "New York" }`)) // Unmarshal value just-in-time: value, err := jit.Unmarshal() if err != nil { panic(err) } fmt.Println(value) // Output: {John 30 New York} } ``` -------------------------------- ### Unmarshal JSON Array of Objects in Go Source: https://github.com/mcwalrus/go-jitjson/blob/main/README.md Shows how to unmarshal a JSON array into a slice of go-jitjson objects in Go. It demonstrates accessing and unmarshalling individual elements from the slice just-in-time. ```Go package main import ( "fmt" "github.com/mcwalrus/go-jitjson" ) type Person struct { Name string Age int City string } func main() { jsonArray := []byte(`[ {"Name":"John","Age":30,"City":"New York"}, {"Name":"Jane","Age":25,"City":"Los Angeles"}, {"Name":"Jim","Age":35,"City":"Chicago"} ]`) // Unmarshal slice of jitjson objects var jit []*jitjson.JitJSON[Person] err := json.Unmarshal(jsonArray, &jit) if err != nil { panic(err) } // Unmarshal the first person just-in-time value, err := jit[0].Unmarshal() if err != nil { panic(err) } fmt.Println(value) // Output: {John 30 New York} } ``` -------------------------------- ### Unmarshal Nested JSON Fields in Go Source: https://github.com/mcwalrus/go-jitjson/blob/main/README.md Demonstrates how to handle nested JSON structures in Go using go-jitjson. It shows how to define structs with nested jitjson fields and unmarshal nested objects on demand. ```Go package main import ( "fmt" "github.com/mcwalrus/go-jitjson" ) type Address struct { Street string City string Zip string } type Person struct { Name string Age int Address *jitjson.JitJSON[Address] } func main() { jsonData := []byte(`{ "Name": "John", "Age": 30, "Address": { "Street": "123 Main St", "City": "New York", "Zip": "10001" } }`) // Unmarshal person with nested Address var person Person err := json.Unmarshal(jsonData, &person) if err != nil { panic(err) } // Unmarshal person's address just-in-time address, err := person.Address.Unmarshal() if err != nil { panic(err) } fmt.Println(address) // Output: {123 Main St New York 10001} } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.