فهرست منبع

#126 fix space in case map key is sorted

Tao Wen 8 سال پیش
والد
کامیت
845d8438db
2فایلهای تغییر یافته به همراه11 افزوده شده و 3 حذف شده
  1. 8 3
      feature_reflect_map.go
  2. 3 0
      jsoniter_adapter_test.go

+ 8 - 3
feature_reflect_map.go

@@ -101,9 +101,10 @@ func (encoder *mapEncoder) Encode(ptr unsafe.Pointer, stream *Stream) {
 			stream.WriteMore()
 		}
 		encodeMapKey(key, stream)
-		stream.writeByte(':')
 		if stream.indention > 0 {
-			stream.writeByte(' ')
+			stream.writeTwoBytes(byte(':'), byte(' '))
+		} else {
+			stream.writeByte(':')
 		}
 		val := realVal.MapIndex(key).Interface()
 		encoder.elemEncoder.EncodeInterface(val, stream)
@@ -185,7 +186,11 @@ func (encoder *sortKeysMapEncoder) Encode(ptr unsafe.Pointer, stream *Stream) {
 			stream.WriteMore()
 		}
 		stream.WriteVal(key.s) // might need html escape, so can not WriteString directly
-		stream.writeByte(':')
+		if stream.indention > 0 {
+			stream.writeTwoBytes(byte(':'), byte(' '))
+		} else {
+			stream.writeByte(':')
+		}
 		val := realVal.MapIndex(key.v).Interface()
 		encoder.elemEncoder.EncodeInterface(val, stream)
 	}

+ 3 - 0
jsoniter_adapter_test.go

@@ -78,4 +78,7 @@ func Test_marshal_indent_map(t *testing.T) {
 	output, err = MarshalIndent(obj, "", "  ")
 	should.Nil(err)
 	should.Equal("{\n  \"1\": 2\n}", string(output))
+	output, err = ConfigCompatibleWithStandardLibrary.MarshalIndent(obj, "", "  ")
+	should.Nil(err)
+	should.Equal("{\n  \"1\": 2\n}", string(output))
 }