-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add new lines to printed error messages * Added JSON encoder and decoder * Fixed error handling in jsondecode.go * Changed from json 'encode' to json 'escape' * README.md changes - added json escaper info
- Loading branch information
Showing
5 changed files
with
57 additions
and
2 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
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,24 @@ | ||
package pencode | ||
|
||
import "encoding/json" | ||
|
||
type JSONUnescaper struct{} | ||
|
||
type JSONInput struct { | ||
Input string `json:"input"` | ||
} | ||
|
||
func (u JSONUnescaper) Encode(input []byte) ([]byte, error) { | ||
inputJson := `{"input":"` + string(input) + `"}` | ||
|
||
var out JSONInput | ||
if err := json.Unmarshal([]byte(inputJson), &out); err != nil { | ||
return []byte{}, err | ||
} | ||
|
||
return []byte(out.Input), nil | ||
} | ||
|
||
func (u JSONUnescaper) HelpText() string { | ||
return "JSON unescape" | ||
} |
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,25 @@ | ||
package pencode | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
) | ||
|
||
type JSONEscaper struct{} | ||
|
||
func (u JSONEscaper) Encode(input []byte) ([]byte, error) { | ||
buf := &bytes.Buffer{} | ||
enc := json.NewEncoder(buf) | ||
enc.SetEscapeHTML(false) // prevents encoding of < and > characters | ||
err := enc.Encode(string(input)) | ||
if err != nil { | ||
return []byte{}, err | ||
} | ||
|
||
output := buf.Bytes() | ||
return output[1 : len(output)-2], nil | ||
} | ||
|
||
func (u JSONEscaper) HelpText() string { | ||
return "JSON escape" | ||
} |