Fix JSON array-based data file handling regression

This bug was introduced in Hugo 0.35.

Fixes #4361
This commit is contained in:
Vas Sudanagunta 2018-02-02 01:35:26 -05:00 committed by Bjørn Erik Pedersen
parent 4743de0d3c
commit 4402c07775
2 changed files with 19 additions and 3 deletions

View file

@ -804,7 +804,7 @@ func (s *Site) handleDataFile(r source.ReadableFile) error {
data, err := s.readData(r) data, err := s.readData(r)
if err != nil { 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 return nil
} }
@ -846,7 +846,7 @@ func (s *Site) readData(f source.ReadableFile) (interface{}, error) {
case "yaml", "yml": case "yaml", "yml":
return parser.HandleYAMLMetaData(content) return parser.HandleYAMLMetaData(content)
case "json": case "json":
return parser.HandleJSONMetaData(content) return parser.HandleJSONData(content)
case "toml": case "toml":
return parser.HandleTOMLMetaData(content) return parser.HandleTOMLMetaData(content)
default: default:

View file

@ -207,6 +207,22 @@ func HandleYAMLMetaData(datum []byte) (map[string]interface{}, error) {
// HandleJSONMetaData unmarshals JSON-encoded datum and returns a Go interface // HandleJSONMetaData unmarshals JSON-encoded datum and returns a Go interface
// representing the encoded data structure. // representing the encoded data structure.
func HandleJSONMetaData(datum []byte) (map[string]interface{}, error) { 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 { if datum == nil {
// Package json returns on error on nil input. // Package json returns on error on nil input.
// Return an empty map to be consistent with our other supported // 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 return make(map[string]interface{}), nil
} }
var f map[string]interface{} var f interface{}
err := json.Unmarshal(datum, &f) err := json.Unmarshal(datum, &f)
return f, err return f, err
} }