Skip to content

erkl-old/jo

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 

Repository files navigation

jo provides a high-performance JSON scanner in Go.

Why? Because the lowest-level JSON parsing primitive provided by the Go standard library is json.Unmarshaling a byte slice into an interface{} value and inspecting it using runtime reflection... and then crying in the shower until the pain goes away.

Example
func minify(dst io.Writer, src io.Reader) error {
	var buf = make([]byte, 4096)
	var s = jo.NewScanner()
	var w, r int

	for {
		// Read the next chunk of data.
		n, err := src.Read(buf)
		if err != nil {
			if err == io.EOF {
				break
			}
			return err
		}

		// Minify the buffer in-place.
		for r, w = 0, 0; r < n; r++ {
			ev := s.Scan(buf[r])

			// Bail on syntax errors.
			if ev == jo.Error {
				return s.LastError()
			}

			// Ignore whitespace characters.
			if ev&jo.Space == 0 {
				buf[w] = buf[r]
				w++
			}
		}

		// Write the now compressed buffer.
		_, err = dst.Write(buf[:w])
		if err != nil {
			return err
		}
	}

	// Check for syntax errors caused by incomplete values.
	if ev := s.End(); ev == jo.Error {
		return s.LastError()
	}

	return nil
}
License

ISC.

About

A JSON scanner for Go.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages