diff options
author | Vas Sudanagunta <vas@commonkarma.org> | 2018-02-02 01:35:26 -0500 |
---|---|---|
committer | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2018-02-02 09:14:37 +0100 |
commit | 4402c077754991df19c3bbab0c4a671dcfdc192c (patch) | |
tree | d4b9363344abf887af6cf222d7e6205abb8f3fe9 | |
parent | 4743de0d3c7564fc06972074e903d5502d204353 (diff) |
Fix JSON array-based data file handling regression
This bug was introduced in Hugo 0.35.
Fixes #4361
-rw-r--r-- | hugolib/site.go | 4 | ||||
-rw-r--r-- | parser/frontmatter.go | 18 |
2 files changed, 19 insertions, 3 deletions
diff --git a/hugolib/site.go b/hugolib/site.go index 0e5f034a4..044866cca 100644 --- a/hugolib/site.go +++ b/hugolib/site.go @@ -804,7 +804,7 @@ func (s *Site) handleDataFile(r source.ReadableFile) error { data, err := s.readData(r) if err != nil { - s.Log.WARN.Printf("Failed to read data from %s: %s", filepath.Join(r.Path(), r.LogicalName()), err) + s.Log.ERROR.Printf("Failed to read data from %s: %s", filepath.Join(r.Path(), r.LogicalName()), err) return nil } @@ -846,7 +846,7 @@ func (s *Site) readData(f source.ReadableFile) (interface{}, error) { case "yaml", "yml": return parser.HandleYAMLMetaData(content) case "json": - return parser.HandleJSONMetaData(content) + return parser.HandleJSONData(content) case "toml": return parser.HandleTOMLMetaData(content) default: diff --git a/parser/frontmatter.go b/parser/frontmatter.go index 7560a734a..e9552a859 100644 --- a/parser/frontmatter.go +++ b/parser/frontmatter.go @@ -207,6 +207,22 @@ func HandleYAMLMetaData(datum []byte) (map[string]interface{}, error) { // HandleJSONMetaData unmarshals JSON-encoded datum and returns a Go interface // representing the encoded data structure. func HandleJSONMetaData(datum []byte) (map[string]interface{}, error) { + m := make(map[string]interface{}) + + if datum == nil { + // Package json returns on error on nil input. + // Return an empty map to be consistent with our other supported + // formats. + return m, nil + } + + err := json.Unmarshal(datum, &m) + return m, err +} + +// HandleJSONData unmarshals JSON-encoded datum and returns a Go interface +// representing the encoded data structure. +func HandleJSONData(datum []byte) (interface{}, error) { if datum == nil { // Package json returns on error on nil input. // Return an empty map to be consistent with our other supported @@ -214,7 +230,7 @@ func HandleJSONMetaData(datum []byte) (map[string]interface{}, error) { return make(map[string]interface{}), nil } - var f map[string]interface{} + var f interface{} err := json.Unmarshal(datum, &f) return f, err } |