forked from hashicorp/packer
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
common/json: add Unmarshal with method with syntax errors
- Loading branch information
Showing
4 changed files
with
45 additions
and
28 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package json | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"fmt" | ||
) | ||
|
||
// Unmarshal is wrapper around json.Unmarshal that returns user-friendly | ||
// errors when there are syntax errors. | ||
func Unmarshal(data []byte, i interface{}) error { | ||
err := json.Unmarshal(data, i) | ||
if err != nil { | ||
syntaxErr, ok := err.(*json.SyntaxError) | ||
if !ok { | ||
return err | ||
} | ||
|
||
// We have a syntax error. Extract out the line number and friends. | ||
// https://groups.google.com/forum/#!topic/golang-nuts/fizimmXtVfc | ||
newline := []byte{'\x0a'} | ||
|
||
// Calculate the start/end position of the line where the error is | ||
start := bytes.LastIndex(data[:syntaxErr.Offset], newline) + 1 | ||
end := len(data) | ||
if idx := bytes.Index(data[start:], newline); idx >= 0 { | ||
end = start + idx | ||
} | ||
|
||
// Count the line number we're on plus the offset in the line | ||
line := bytes.Count(data[:start], newline) + 1 | ||
pos := int(syntaxErr.Offset) - start - 1 | ||
|
||
err = fmt.Errorf("Error in line %d, char %d: %s\n%s", | ||
line, pos, syntaxErr, data[start:end]) | ||
return err | ||
} | ||
|
||
return nil | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
package 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