mirror of
https://github.com/json-iterator/go.git
synced 2025-04-23 11:37:32 +02:00
update README
This commit is contained in:
parent
cb86934dc8
commit
60981a2642
137
README.md
137
README.md
@ -1,109 +1,80 @@
|
|||||||
# go
|
# json iterator (jsoniter)
|
||||||
|
|
||||||
faster than DOM, more usable than SAX/StAX
|
faster than DOM, more usable than SAX/StAX
|
||||||
|
|
||||||
# string
|
for performance numbers, see https://github.com/json-iterator/go-benchmark
|
||||||
|
|
||||||
|
# DOM style api
|
||||||
|
|
||||||
```
|
```
|
||||||
func Benchmark_jsoniter_string(b *testing.B) {
|
type StructOfTag struct {
|
||||||
for n := 0; n < b.N; n++ {
|
field1 string `json:"field-1"`
|
||||||
lexer := NewLexerWithArray([]byte(`"\ud83d\udc4a"`))
|
field2 string `json:"-"`
|
||||||
lexer.LexString()
|
field3 int `json:",string"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func Test_reflect_struct_tag_field(t *testing.T) {
|
||||||
|
iter := ParseString(`{"field-1": "hello", "field2": "", "field3": "100"}`)
|
||||||
|
struct_ := StructOfTag{field2: "world"}
|
||||||
|
iter.Read(&struct_)
|
||||||
|
if struct_.field1 != "hello" {
|
||||||
|
fmt.Println(iter.Error)
|
||||||
|
t.Fatal(struct_.field1)
|
||||||
|
}
|
||||||
|
if struct_.field2 != "world" {
|
||||||
|
fmt.Println(iter.Error)
|
||||||
|
t.Fatal(struct_.field2)
|
||||||
|
}
|
||||||
|
if struct_.field3 != 100 {
|
||||||
|
fmt.Println(iter.Error)
|
||||||
|
t.Fatal(struct_.field3)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
10000000 140 ns/op
|
# StAX style api
|
||||||
|
|
||||||
|
Array
|
||||||
|
|
||||||
```
|
```
|
||||||
func Benchmark_json_string(b *testing.B) {
|
iter := ParseString(`[1,2,3]`)
|
||||||
for n := 0; n < b.N; n++ {
|
for iter.ReadArray() {
|
||||||
result := ""
|
iter.ReadUint64()
|
||||||
json.Unmarshal([]byte(`"\ud83d\udc4a"`), &result)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
````
|
|
||||||
|
|
||||||
2000000 710 ns/op (5x slower)
|
|
||||||
|
|
||||||
# int
|
|
||||||
|
|
||||||
```
|
|
||||||
func Benchmark_jsoniter_int(b *testing.B) {
|
|
||||||
for n := 0; n < b.N; n++ {
|
|
||||||
lexer := NewLexerWithArray([]byte(`-100`))
|
|
||||||
lexer.LexInt64()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
30000000 60.1 ns/op
|
Object
|
||||||
|
|
||||||
```
|
```
|
||||||
func Benchmark_json_int(b *testing.B) {
|
type TestObj struct {
|
||||||
for n := 0; n < b.N; n++ {
|
Field1 string
|
||||||
result := int64(0)
|
Field2 uint64
|
||||||
json.Unmarshal([]byte(`-100`), &result)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
3000000 505 ns/op (8x slower)
|
|
||||||
|
|
||||||
# array
|
|
||||||
|
|
||||||
```
|
```
|
||||||
func Benchmark_jsoniter_array(b *testing.B) {
|
iter := ParseString(`{"field1": "1", "field2": 2}`)
|
||||||
for n := 0; n < b.N; n++ {
|
obj := TestObj{}
|
||||||
iter := ParseString(`[1,2,3]`)
|
for field := iter.ReadObject(); field != ""; field = iter.ReadObject() {
|
||||||
for iter.ReadArray() {
|
switch field {
|
||||||
iter.ReadUint64()
|
case "field1":
|
||||||
}
|
obj.Field1 = iter.ReadString()
|
||||||
}
|
case "field2":
|
||||||
|
obj.Field2 = iter.ReadUint64()
|
||||||
|
default:
|
||||||
|
iter.ReportError("bind object", "unexpected field")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
10000000 189 ns/op
|
Skip
|
||||||
|
|
||||||
```
|
```
|
||||||
func Benchmark_json_array(b *testing.B) {
|
iter := ParseString(`[ {"a" : [{"b": "c"}], "d": 102 }, "b"]`)
|
||||||
for n := 0; n < b.N; n++ {
|
iter.ReadArray()
|
||||||
result := []interface{}{}
|
iter.Skip()
|
||||||
json.Unmarshal([]byte(`[1,2,3]`), &result)
|
iter.ReadArray()
|
||||||
}
|
if iter.ReadString() != "b" {
|
||||||
|
t.FailNow()
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
1000000 1327 ns/op
|
|
||||||
|
|
||||||
# object
|
|
||||||
|
|
||||||
```
|
|
||||||
func Benchmark_jsoniter_object(b *testing.B) {
|
|
||||||
for n := 0; n < b.N; n++ {
|
|
||||||
iter := ParseString(`{"field1": "1", "field2": 2}`)
|
|
||||||
obj := TestObj{}
|
|
||||||
for field := iter.ReadObject(); field != ""; field = iter.ReadObject() {
|
|
||||||
switch field {
|
|
||||||
case "field1":
|
|
||||||
obj.Field1 = iter.ReadString()
|
|
||||||
case "field2":
|
|
||||||
obj.Field2 = iter.ReadUint64()
|
|
||||||
default:
|
|
||||||
iter.ReportError("bind object", "unexpected field")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
5000000 401 ns/op
|
|
||||||
|
|
||||||
```
|
|
||||||
func Benchmark_json_object(b *testing.B) {
|
|
||||||
for n := 0; n < b.N; n++ {
|
|
||||||
result := TestObj{}
|
|
||||||
json.Unmarshal([]byte(`{"field1": "1", "field2": 2}`), &result)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
1000000 1318 ns/op
|
|
Loading…
x
Reference in New Issue
Block a user