Explorar el Código

Merge branch 'master' of https://github.com/json-iterator/go

Tao Wen hace 8 años
padre
commit
4ae426c4b7
Se han modificado 3 ficheros con 57 adiciones y 6 borrados
  1. 5 5
      fuzzy_mode_convert_table.md
  2. 42 0
      jsoniter_encode_interface_test.go
  3. 10 1
      jsoniter_string_test.go

+ 5 - 5
fuzzy_mode_convert_table.md

@@ -1,7 +1,7 @@
 | json type \ dest type | bool | int | uint | float |string|
 | --- | --- | --- | --- |--|--|
-| number | positive => true <br/> negative => true <br/> zero => false| 23.2 => 23 <br/> -32.1 => -32| 12.1 => 12 <br/> -12.1 => 0|as normal||
-| string | empty string => false <br/> string "0" => false <br/> other strings => true | "123.32" => 123 <br/> "-123.4" => -123 <br/> "123.23xxxw" => 123 <br/>  "abcde12" => 0 <br/> "-32.1" => -32| 13.2 => 13 <br/> -1.1 => 0 |12.1 => 12.1 <br/> -12.3 => -12.3<br/> 12.4xxa => 12.4 <br/> +1.1e2 =>110 ||
-| bool | true => true <br/> false => false| true => 1 <br/> false => 0 | true => 1 <br/> false => 0 |true => 1 <br/>false => 0||
-| object | true | 0 | 0 |0||
-| array | empty array => false <br/> nonempty array => true| [] => 0 <br/> [1,2] => 1 | [] => 0 <br/> [1,2] => 1 |[] => 0<br/>[1,2] => 1||
+| number | positive => true <br/> negative => true <br/> zero => false| 23.2 => 23 <br/> -32.1 => -32| 12.1 => 12 <br/> -12.1 => 0|as normal|same as origin|
+| string | empty string => false <br/> string "0" => false <br/> other strings => true | "123.32" => 123 <br/> "-123.4" => -123 <br/> "123.23xxxw" => 123 <br/>  "abcde12" => 0 <br/> "-32.1" => -32| 13.2 => 13 <br/> -1.1 => 0 |12.1 => 12.1 <br/> -12.3 => -12.3<br/> 12.4xxa => 12.4 <br/> +1.1e2 =>110 |same as origin|
+| bool | true => true <br/> false => false| true => 1 <br/> false => 0 | true => 1 <br/> false => 0 |true => 1 <br/>false => 0|true => "true" <br/> false => "false"|
+| object | true | 0 | 0 |0|originnal json|
+| array | empty array => false <br/> nonempty array => true| [] => 0 <br/> [1,2] => 1 | [] => 0 <br/> [1,2] => 1 |[] => 0<br/>[1,2] => 1|original json|

+ 42 - 0
jsoniter_encode_interface_test.go

@@ -0,0 +1,42 @@
+package jsoniter
+
+import (
+	"encoding/json"
+	"testing"
+
+	"github.com/stretchr/testify/require"
+)
+
+func Test_encode_interface(t *testing.T) {
+	should := require.New(t)
+	var a interface{}
+	a = int8(10)
+	str, err := MarshalToString(a)
+	should.Nil(err)
+	should.Equal(str, "10")
+	a = float32(3)
+	str, err = MarshalToString(a)
+	should.Nil(err)
+	should.Equal(str, "3")
+	a = map[string]interface{}{"abc": 1}
+	str, err = MarshalToString(a)
+	should.Nil(err)
+	should.Equal(str, `{"abc":1}`)
+	a = uintptr(1)
+	str, err = MarshalToString(a)
+	should.Nil(err)
+	should.Equal(str, "1")
+	a = uint(1)
+	str, err = MarshalToString(a)
+	should.Nil(err)
+	should.Equal(str, "1")
+	a = uint8(1)
+	str, err = MarshalToString(a)
+	should.Nil(err)
+	should.Equal(str, "1")
+	a = json.RawMessage("abc")
+	MarshalToString(a)
+	str, err = MarshalToString(a)
+	should.Nil(err)
+	should.Equal(str, "abc")
+}

+ 10 - 1
jsoniter_string_test.go

@@ -94,7 +94,16 @@ func Test_read_normal_string(t *testing.T) {
 func Test_read_exotic_string(t *testing.T) {
 	cases := map[string]string{
 		`"hel\"lo"`:      `hel"lo`,
-		`"hel\nlo"`:      "hel\nlo",
+		`"hel\\\/lo"`:    `hel\/lo`,
+		`"hel\\blo"`:     `hel\blo`,
+		`"hel\\\blo"`:    "hel\\\blo",
+		`"hel\\nlo"`:     `hel\nlo`,
+		`"hel\\\nlo"`:    "hel\\\nlo",
+		`"hel\\tlo"`:     `hel\tlo`,
+		`"hel\\flo"`:     `hel\flo`,
+		`"hel\\\flo"`:    "hel\\\flo",
+		`"hel\\\rlo"`:    "hel\\\rlo",
+		`"hel\\\tlo"`:    "hel\\\tlo",
 		`"\u4e2d\u6587"`: "中文",
 		`"\ud83d\udc4a"`: "\xf0\x9f\x91\x8a", // surrogate
 	}