Skip to content

Instantly share code, notes, and snippets.

@mugli
Forked from sugab/json_read.go
Created February 20, 2021 17:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mugli/ff215ace1cdbfbb3bee960767e3fc75a to your computer and use it in GitHub Desktop.
Save mugli/ff215ace1cdbfbb3bee960767e3fc75a to your computer and use it in GitHub Desktop.
Read JSON File form a filePath. American movies dataset can be found here: https://raw.githubusercontent.com/prust/wikipedia-movie-data/master/movies.json. 200,000+ Jeopardy question dataset can be found here: https://www.reddit.com/r/datasets/comments/1uyd0t/200000_jeopardy_questions_in_a_json_file/.
package main
import (
"encoding/json"
"io/ioutil"
)
func readJSON(fileName string, filter func(map[string]interface{}) bool) []map[string]interface{} {
datas := []map[string]interface{}{}
file, _ := ioutil.ReadFile(fileName)
json.Unmarshal(file, &datas)
filteredData := []map[string]interface{}{}
for _, data := range datas {
// Do some filtering
if filter(data) {
filteredData = append(filteredData, data)
}
}
return filteredData
}
package main
import (
"encoding/json"
"os"
)
func readJSONToken(fileName string, filter func(map[string]interface{}) bool) []map[string]interface{} {
file, _ := os.Open(fileName)
defer file.Close()
decoder := json.NewDecoder(file)
filteredData := []map[string]interface{}{}
// Read the array open bracket
decoder.Token()
data := map[string]interface{}{}
for decoder.More() {
decoder.Decode(&data)
if filter(data) {
filteredData = append(filteredData, data)
}
}
return filteredData
}
package main
import (
"testing"
)
func BenchmarkMovieRead(b *testing.B) {
readJSON("movie.json", func(data map[string]interface{}) bool {
return data["year"].(float64) >= 2010
})
}
func BenchmarkMovieReadToken(b *testing.B) {
readJSONToken("movie.json", func(data map[string]interface{}) bool {
return data["year"].(float64) >= 2010
})
}
func BenchmarkQRead(b *testing.B) {
readJSON("question.json", func(data map[string]interface{}) bool {
return data["show_number"].(string) == "4680"
})
}
func BenchmarkQReadToken(b *testing.B) {
readJSONToken("question.json", func(data map[string]interface{}) bool {
return data["show_number"].(string) == "4680"
})
}
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"testing"
"time"
)
func makeData(n int) interface{} {
datas := []map[string]interface{}{}
for i := 0; i < n; i++ {
data := map[string]interface{}{
"Name": fmt.Sprintf("Person %v", i),
"Address": "Somewhere we don't really know",
"Description": "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.",
"Age": i + 10,
"PhoneNumber": i + 99,
"CreatedOn": time.Now(),
}
datas = append(datas, data)
}
return datas
}
func BenchmarkMarshalWriteBig(b *testing.B) {
for n := 0; n < b.N; n++ {
jsonString, _ := json.Marshal(makeData(1000000))
ioutil.WriteFile("big_marhsall.json", jsonString, os.ModePerm)
}
}
func BenchmarkEncodeWriteBig(b *testing.B) {
for n := 0; n < b.N; n++ {
file, _ := os.OpenFile("big_encode.json", os.O_CREATE, os.ModePerm)
defer file.Close()
encoder := json.NewEncoder(file)
encoder.Encode(makeData(1000000))
}
}
func BenchmarkMarshalWrite(b *testing.B) {
for n := 0; n < b.N; n++ {
jsonString, _ := json.Marshal(makeData(10000))
ioutil.WriteFile("small_marhsall.json", jsonString, os.ModePerm)
}
}
func BenchmarkEncodeWrite(b *testing.B) {
for n := 0; n < b.N; n++ {
file, _ := os.OpenFile("small_encode.json", os.O_CREATE, os.ModePerm)
defer file.Close()
encoder := json.NewEncoder(file)
encoder.Encode(makeData(10000))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment