mirror of
https://github.com/netbirdio/easyjson.git
synced 2026-05-22 18:44:42 -07:00
Merge branch 'master' into camel_case_functions
This commit is contained in:
+7
-2
@@ -10,8 +10,9 @@ import (
|
||||
|
||||
// Writer is a JSON writer.
|
||||
type Writer struct {
|
||||
Error error
|
||||
Buffer buffer.Buffer
|
||||
EscapeLtGt bool
|
||||
Error error
|
||||
Buffer buffer.Buffer
|
||||
}
|
||||
|
||||
// Size returns the size of the data that was written out.
|
||||
@@ -224,6 +225,10 @@ func (w *Writer) String(s string) {
|
||||
escape = '\\'
|
||||
case '"':
|
||||
escape = '"'
|
||||
case '<', '>':
|
||||
if !w.EscapeLtGt {
|
||||
continue
|
||||
}
|
||||
default:
|
||||
if c >= 0x20 {
|
||||
// no escaping is required
|
||||
|
||||
@@ -7,6 +7,7 @@ import (
|
||||
"encoding/json"
|
||||
|
||||
"github.com/mailru/easyjson"
|
||||
"github.com/mailru/easyjson/jwriter"
|
||||
)
|
||||
|
||||
type testType interface {
|
||||
@@ -103,3 +104,25 @@ func TestParseNull(t *testing.T) {
|
||||
t.Errorf("Unmarshal() = %+v; want %+v", got, want)
|
||||
}
|
||||
}
|
||||
|
||||
var testCasesEncodeLtGt = []struct {
|
||||
Writer *jwriter.Writer
|
||||
Encoded string
|
||||
}{
|
||||
{&jwriter.Writer{
|
||||
EscapeLtGt: false,
|
||||
}, encodeLtGtFalseWantString},
|
||||
{&jwriter.Writer{
|
||||
EscapeLtGt: true,
|
||||
}, encodeLtGtTrueWantString},
|
||||
}
|
||||
|
||||
func TestEncodeLtGt(t *testing.T) {
|
||||
for i, test := range testCasesEncodeLtGt {
|
||||
test.Writer.String(encodeLtGtString)
|
||||
got := string(test.Writer.Buffer.BuildBytes())
|
||||
if got != test.Encoded {
|
||||
t.Errorf("[%d] Encoded() = %+v; want %+v", i, got, test.Encoded)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -440,3 +440,7 @@ type RequiredOptionalStruct struct {
|
||||
FirstName string `json:"first_name,required"`
|
||||
Lastname string `json:"last_name"`
|
||||
}
|
||||
|
||||
var encodeLtGtString = `Username <user@example.com>`
|
||||
var encodeLtGtFalseWantString = `"Username <user@example.com>"`
|
||||
var encodeLtGtTrueWantString = `"Username \u003cuser@example.com\u003e"`
|
||||
|
||||
Reference in New Issue
Block a user