Skip to content

Commit

Permalink
jsonutil: add JSON utility functions
Browse files Browse the repository at this point in the history
  • Loading branch information
mewmew committed Apr 28, 2017
1 parent e4ccb66 commit c19a6f9
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions jsonutil/jsonutil.go
@@ -0,0 +1,30 @@
// Package jsonutil implements JSON utility functions.
package jsonutil

import (
"bufio"
"encoding/json"
"io"
"os"

"github.com/pkg/errors"
)

// Parse parses the given JSON stream into v.
func Parse(r io.Reader, v interface{}) error {
br := bufio.NewReader(r)
dec := json.NewDecoder(br)
return dec.Decode(v)
}

// ParseFile parses the given JSON file into v.
func ParseFile(path string, v interface{}) error {
f, err := os.Open(path)
if err != nil {
return errors.WithStack(err)
}
defer f.Close()
br := bufio.NewReader(f)
dec := json.NewDecoder(br)
return dec.Decode(v)
}

0 comments on commit c19a6f9

Please sign in to comment.