Пример #1
0
func BenchmarkGojScanning(b *testing.B) {
	if codeJSON == nil {
		b.StopTimer()
		codeInit()
		b.StartTimer()
	}
	parser := goj.NewParser()
	for i := 0; i < b.N; i++ {
		err := parser.Parse([]byte(codeJSON), func(t goj.Type, k []byte, v []byte) bool {
			return true
		})
		if err != nil {
			b.Fatal("Scanning:", err)
		}
	}
	b.SetBytes(int64(len(codeJSON)))
}
Пример #2
0
func testParse(json string) (results string) {
	var stack []bool
	parser := goj.NewParser()
	err := parser.Parse([]byte(json), func(t goj.Type, k []byte, v []byte) bool {
		if len(k) > 0 {
			results += fmt.Sprintf("key: '%s'\n", string(k))
		}
		switch t {
		case goj.True:
			results += "bool: true\n"
		case goj.False:
			results += "bool: false\n"
		case goj.Null:
			results += "null\n"
		case goj.String:
			results += fmt.Sprintf("string: '%s'\n", v)
		case goj.Array:
			results += "array open '['\n"
			stack = append(stack, true)
		case goj.Object:
			results += "map open '{'\n"
			stack = append(stack, false)
		case goj.Integer, goj.NegInteger, goj.Float:
			results += fmt.Sprintf("%s: %s\n", t.String(), v)
		case goj.ArrayEnd:
			stack = stack[:len(stack)-1]
			results += "array close ']'\n"
		case goj.ObjectEnd:
			stack = stack[:len(stack)-1]
			results += "map close '}'\n"
		}
		return true
	})
	if err != nil {
		results += fmt.Sprintf("parse error: %s\n", err)
	}

	return results
}