-
Notifications
You must be signed in to change notification settings - Fork 259
Expand file tree
/
Copy pathtest_jsparser.go
More file actions
48 lines (41 loc) · 975 Bytes
/
test_jsparser.go
File metadata and controls
48 lines (41 loc) · 975 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package main
import (
"bufio"
"bytes"
"fmt"
"github.com/tamerh/jsparser"
"io/ioutil"
"net"
"os"
"strconv"
)
func notify(msg string) {
conn, err := net.Dial("tcp", "localhost:9001")
if err == nil {
fmt.Fprintf(conn, msg)
conn.Close()
}
}
func main() {
content, err := ioutil.ReadFile("/tmp/1.json")
if err != nil {
panic(fmt.Sprintf("%v", err))
}
notify(fmt.Sprintf("Go jsparser\t%d", os.Getpid()))
f := bytes.NewReader(content)
br := bufio.NewReaderSize(f, 16384)
parser := jsparser.NewJSONParser(br, "coordinates").SkipProps([]string{"name", "opts"})
x, y, z := 0.0, 0.0, 0.0
len := 0.0
for json := range parser.Stream() {
xx, _ := strconv.ParseFloat(json.ObjectVals["x"].StringVal, 64)
yy, _ := strconv.ParseFloat(json.ObjectVals["y"].StringVal, 64)
zz, _ := strconv.ParseFloat(json.ObjectVals["z"].StringVal, 64)
x += xx
y += yy
z += zz
len += 1.0
}
fmt.Printf("%.8f\n%.8f\n%.8f\n", x/len, y/len, z/len)
notify("stop")
}