Skip to content

Instantly share code, notes, and snippets.

@tidwall
Created February 9, 2021 15:04
Show Gist options
  • Save tidwall/0f2f56544b06a80535184d41588b70d2 to your computer and use it in GitHub Desktop.
Save tidwall/0f2f56544b06a80535184d41588b70d2 to your computer and use it in GitHub Desktop.
Test valid JSON
package main
import (
"fmt"
"io/ioutil"
"net/http"
gojson "encoding/json"
"github.com/goccy/go-json"
"github.com/minio/simdjson-go"
"github.com/tidwall/gjson"
)
var getCache = map[string][]byte{}
func jsonValid(url, name string, validator func(data []byte) bool) {
data, ok := getCache[url]
if !ok {
resp, err := http.Get(url)
if err != nil {
panic(err)
}
defer resp.Body.Close()
data, err = ioutil.ReadAll(resp.Body)
if err != nil {
panic(err)
}
getCache[url] = data
}
fmt.Printf("%-40s ", name)
if validator(data) {
fmt.Printf("VALID\n")
} else {
fmt.Printf("INVALID\n")
}
}
func main() {
urls := []string{
"https://raw.githubusercontent.com/simdjson/simdjson/master/jsonexamples/apache_builds.json",
"https://raw.githubusercontent.com/simdjson/simdjson/master/jsonexamples/canada.json",
"https://raw.githubusercontent.com/simdjson/simdjson/master/jsonexamples/citm_catalog.json",
"https://raw.githubusercontent.com/simdjson/simdjson/master/jsonexamples/github_events.json",
"https://raw.githubusercontent.com/simdjson/simdjson/master/jsonexamples/random.json",
"https://raw.githubusercontent.com/simdjson/simdjson/master/jsonexamples/twitter.json",
"https://raw.githubusercontent.com/simdjson/simdjson/master/jsonexamples/repeat.json",
"https://raw.githubusercontent.com/simdjson/simdjson/master/jsonexamples/semanticscholar-corpus.json",
}
for _, url := range urls {
println(url)
jsonValid(url, "encoding/json", gojson.Valid)
jsonValid(url, "github.com/tidwall/gjson", gjson.ValidBytes)
jsonValid(url, "github.com/minio/simdjson-go", func(data []byte) bool { _, err := simdjson.Parse(data, nil); return err == nil })
jsonValid(url, "github.com/goccy/go-json", json.Valid)
println()
}
}
@tidwall
Copy link
Author

tidwall commented Feb 9, 2021

OUTPUT

https://raw.githubusercontent.com/simdjson/simdjson/master/jsonexamples/apache_builds.json
encoding/json                            VALID
github.com/tidwall/gjson                 VALID
github.com/minio/simdjson-go             VALID
github.com/goccy/go-json                 INVALID

https://raw.githubusercontent.com/simdjson/simdjson/master/jsonexamples/canada.json
encoding/json                            VALID
github.com/tidwall/gjson                 VALID
github.com/minio/simdjson-go             VALID
github.com/goccy/go-json                 VALID

https://raw.githubusercontent.com/simdjson/simdjson/master/jsonexamples/citm_catalog.json
encoding/json                            VALID
github.com/tidwall/gjson                 VALID
github.com/minio/simdjson-go             VALID
github.com/goccy/go-json                 INVALID

https://raw.githubusercontent.com/simdjson/simdjson/master/jsonexamples/github_events.json
encoding/json                            VALID
github.com/tidwall/gjson                 VALID
github.com/minio/simdjson-go             VALID
github.com/goccy/go-json                 INVALID

https://raw.githubusercontent.com/simdjson/simdjson/master/jsonexamples/random.json
encoding/json                            VALID
github.com/tidwall/gjson                 VALID
github.com/minio/simdjson-go             VALID
github.com/goccy/go-json                 VALID

https://raw.githubusercontent.com/simdjson/simdjson/master/jsonexamples/twitter.json
encoding/json                            VALID
github.com/tidwall/gjson                 VALID
github.com/minio/simdjson-go             VALID
github.com/goccy/go-json                 INVALID

https://raw.githubusercontent.com/simdjson/simdjson/master/jsonexamples/repeat.json
encoding/json                            VALID
github.com/tidwall/gjson                 VALID
github.com/minio/simdjson-go             VALID
github.com/goccy/go-json                 VALID

https://raw.githubusercontent.com/simdjson/simdjson/master/jsonexamples/semanticscholar-corpus.json
encoding/json                            VALID
github.com/tidwall/gjson                 VALID
github.com/minio/simdjson-go             VALID
github.com/goccy/go-json                 INVALID

https://raw.githubusercontent.com/simdjson/simdjson/master/jsonexamples/gsoc-2018.json
encoding/json                            VALID
github.com/tidwall/gjson                 VALID
github.com/minio/simdjson-go             VALID
github.com/goccy/go-json                 INVALID

https://raw.githubusercontent.com/simdjson/simdjson/master/jsonexamples/update-center.json
encoding/json                            VALID
github.com/tidwall/gjson                 VALID
github.com/minio/simdjson-go             VALID
github.com/goccy/go-json                 INVALID

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment