-
-
Save tidwall/0f2f56544b06a80535184d41588b70d2 to your computer and use it in GitHub Desktop.
Test valid JSON
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
OUTPUT