コード例 #1
0
ファイル: basic_test.go プロジェクト: jango2015/wk
func Kson(x interface{}) string {
	b, err := kson.Marshal(x)
	if err != nil {
		panic(err)
	}
	return string(b)
}
コード例 #2
0
ファイル: encoder_test.go プロジェクト: sdming/kiss
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)
	}

}
コード例 #3
0
ファイル: encoder_test.go プロジェクト: sdming/kiss
func TestMarshalConfig(t *testing.T) {
	config := defaultConfig
	b, err := kson.Marshal(config)
	t.Log(string(b))
	if err != nil {
		t.Error(err)
	}
}
コード例 #4
0
ファイル: kson_test.go プロジェクト: sdming/kiss
func BenchmarkMarshal(b *testing.B) {

	config := defaultConfig

	for i := 0; i < b.N; i++ {
		_, err := kson.Marshal(config)
		if err != nil {
			b.Error(err)
		}
	}

}
コード例 #5
0
ファイル: encoder_test.go プロジェクト: sdming/kiss
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)
	}

}
コード例 #6
0
ファイル: encoder_test.go プロジェクト: sdming/kiss
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)
	}
}
コード例 #7
0
ファイル: encoder_test.go プロジェクト: sdming/kiss
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)
	}

}
コード例 #8
0
ファイル: encoder_test.go プロジェクト: sdming/kiss
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)
	}

}
コード例 #9
0
ファイル: encoder_test.go プロジェクト: sdming/kiss
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)
	}

}
コード例 #10
0
ファイル: encoder_test.go プロジェクト: sdming/kiss
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)
	}

}
コード例 #11
0
ファイル: data.go プロジェクト: jango2015/wk
func formatKson(ctx *wk.HttpContext, x interface{}) (wk.HttpResult, bool) {
	b, _ := kson.Marshal(x)
	return wk.Content(string(b), "text/plain"), true
}