瀏覽代碼

codec: fix TermWhitespace handling

We should only add ' ' or '\n' if TermWhitespace=true.
Ugorji Nwoke 6 年之前
父節點
當前提交
08f7b401ae
共有 3 個文件被更改,包括 17 次插入6 次删除
  1. 2 1
      codec/codec_test.go
  2. 3 1
      codec/helper_test.go
  3. 12 4
      codec/json.go

+ 2 - 1
codec/codec_test.go

@@ -1460,7 +1460,8 @@ func doTestJsonLargeInteger(t *testing.T, v interface{}, ias uint8) {
 	// then decode, and validate that the correct value was decoded.
 	fnStrChk := func() {
 		// check that output started with ", and ended with " true
-		if !(len(b) >= 7 && b[0] == '"' && string(b[len(b)-7:]) == `" true `) {
+		// if !(len(b) >= 7 && b[0] == '"' && string(b[len(b)-7:]) == `" true `) {
+		if !(len(b) >= 5 && b[0] == '"' && string(b[len(b)-5:]) == `"true`) {
 			logT(t, "Expecting a JSON string, got: '%s'", b)
 			failT(t)
 		}

+ 3 - 1
codec/helper_test.go

@@ -13,9 +13,11 @@ import (
 
 // --- these functions are used by both benchmarks and tests
 
+var errDeepEqualNotMatch = errors.New("Not Match")
+
 func deepEqual(v1, v2 interface{}) (err error) {
 	if !reflect.DeepEqual(v1, v2) {
-		err = errors.New("Not Match")
+		err = errDeepEqualNotMatch
 	}
 	return
 }

+ 12 - 4
codec/json.go

@@ -585,11 +585,19 @@ func (e *jsonEncDriver) quoteStr(s string) {
 }
 
 func (e *jsonEncDriver) atEndOfEncode() {
-	if e.c == 0 { // scalar written
-		e.w.writen1(' ')
-	} else if e.h.TermWhitespace {
-		e.w.writen1('\n')
+	// if e.c == 0 { // scalar written, output space
+	// 	e.w.writen1(' ')
+	// } else if e.h.TermWhitespace { // container written, output new-line
+	// 	e.w.writen1('\n')
+	// }
+	if e.h.TermWhitespace {
+		if e.c == 0 { // scalar written, output space
+			e.w.writen1(' ')
+		} else { // container written, output new-line
+			e.w.writen1('\n')
+		}
 	}
+
 	// e.c = 0
 }