gitee.com/quant1x/gox@v1.7.6/fastjson/update_example_test.go (about)

     1  package fastjson_test
     2  
     3  import (
     4  	"fmt"
     5  	"gitee.com/quant1x/gox/fastjson"
     6  	"log"
     7  )
     8  
     9  func ExampleObject_Del() {
    10  	v := fastjson.MustParse(`{"foo": 123, "bar": [1,2], "baz": "xyz"}`)
    11  	o, err := v.Object()
    12  	if err != nil {
    13  		log.Fatalf("cannot otain object: %s", err)
    14  	}
    15  	fmt.Printf("%s\n", o)
    16  
    17  	o.Del("bar")
    18  	fmt.Printf("%s\n", o)
    19  
    20  	o.Del("foo")
    21  	fmt.Printf("%s\n", o)
    22  
    23  	o.Del("baz")
    24  	fmt.Printf("%s\n", o)
    25  
    26  	// Output:
    27  	// {"foo":123,"bar":[1,2],"baz":"xyz"}
    28  	// {"foo":123,"baz":"xyz"}
    29  	// {"baz":"xyz"}
    30  	// {}
    31  }
    32  
    33  func ExampleValue_Del() {
    34  	v := fastjson.MustParse(`{"foo": 123, "bar": [1,2], "baz": "xyz"}`)
    35  	fmt.Printf("%s\n", v)
    36  
    37  	v.Del("foo")
    38  	fmt.Printf("%s\n", v)
    39  
    40  	v.Get("bar").Del("0")
    41  	fmt.Printf("%s\n", v)
    42  
    43  	// Output:
    44  	// {"foo":123,"bar":[1,2],"baz":"xyz"}
    45  	// {"bar":[1,2],"baz":"xyz"}
    46  	// {"bar":[2],"baz":"xyz"}
    47  }
    48  
    49  func ExampleValue_Set() {
    50  	v := fastjson.MustParse(`{"foo":1,"bar":[2,3]}`)
    51  
    52  	// Replace `foo` value with "xyz"
    53  	v.Set("foo", fastjson.MustParse(`"xyz"`))
    54  	// Add "newv":123
    55  	v.Set("newv", fastjson.MustParse(`123`))
    56  	fmt.Printf("%s\n", v)
    57  
    58  	// Replace `bar.1` with {"x":"y"}
    59  	v.Get("bar").Set("1", fastjson.MustParse(`{"x":"y"}`))
    60  	// Add `bar.3="qwe"
    61  	v.Get("bar").Set("3", fastjson.MustParse(`"qwe"`))
    62  	fmt.Printf("%s\n", v)
    63  
    64  	// Output:
    65  	// {"foo":"xyz","bar":[2,3],"newv":123}
    66  	// {"foo":"xyz","bar":[2,{"x":"y"},null,"qwe"],"newv":123}
    67  }