func Kson(x interface{}) string { b, err := kson.Marshal(x) if err != nil { panic(err) } return string(b) }
func TestMarshalMap(t *testing.T) { data := map[string]interface{}{ "bool": true, "uint": 1024, "int": -1, "slice": []interface{}{ true, 1024, "hello", -1}, "array": [3]string{ "one", "two", "three"}, "map": map[string]string{ "a": "a_1", "b": "b_1", }, "string": "hello", } b, err := kson.Marshal(data) t.Log(string(b)) if err != nil { t.Error(err) } }
func TestMarshalConfig(t *testing.T) { config := defaultConfig b, err := kson.Marshal(config) t.Log(string(b)) if err != nil { t.Error(err) } }
func BenchmarkMarshal(b *testing.B) { config := defaultConfig for i := 0; i < b.N; i++ { _, err := kson.Marshal(config) if err != nil { b.Error(err) } } }
func TestMarshalStruct(t *testing.T) { var t1 T1 t1.T1_map = make(map[string]*T2, 2) t1.T1_map["a"] = &T2{} t1.T1_map["b"] = &T2{} b, err := kson.Marshal(t1) t.Log(string(b)) if err != nil { t.Error(err) } }
func testMarshalSimpleType(t *testing.T, expect string, a interface{}) { b, err := kson.Marshal(a) if err != nil { t.Errorf("marshal fail: %v", a) return } s := string(b) t.Log(s) if s != expect { t.Errorf("Marshal fail: %v; expect %s; actual %s \n", reflect.TypeOf(a), expect, s) } }
func TestMarshalHash(t *testing.T) { hash := map[string]interface{}{ "int": 1024, "bool": true, "string": "string", "text": ` I'm not a great programmer, I'm a pretty good programmer with great habits. `, } b, err := kson.Marshal(hash) t.Log(string(b)) if err != nil { t.Error(err) } }
func TestMarshalList(t *testing.T) { list := []string{ "line one", `[line two]`, ` line three `, } b, err := kson.Marshal(list) t.Log(string(b)) if err != nil { t.Error(err) } }
func TestMarshalPoco(t *testing.T) { p := Poco{ Name: "value", Int: -1024, Float: 6.4, Bool: true, Date: "2012-12-21", String: `abcdefghijklmnopqrstuvwxyz/字符串 #:[]{}`, Quote: "[0,1,2,3,4,5,6,7,8,9]", Json: ` var father = { "Name": "John", "Age": 32, "Children": [ { "Name": "Richard", "Age": 7 }, { "Name": "Susan", "Age": 4 } ] }; `, Xml: ` <root> <!-- a node --> <text> I'll be back </text> </root> `, Empty: "", } b, err := kson.Marshal(p) t.Log(string(b)) if err != nil { t.Error(err) } }
func TestMarshalSlice(t *testing.T) { var data []interface{} = []interface{}{ true, 1024, -1, []interface{}{ true, 1024, "hello", -1}, [3]string{ "one", "two", "three"}, } b, err := kson.Marshal(data) t.Log(string(b)) if err != nil { t.Error(err) } }
func formatKson(ctx *wk.HttpContext, x interface{}) (wk.HttpResult, bool) { b, _ := kson.Marshal(x) return wk.Content(string(b), "text/plain"), true }