Пример #1
0
func parseJSONString(content []byte, structName string, w io.Writer) error {
	w.Write([]byte("package main"))

	rs := make(chan *parser.Struct)
	errs := make(chan error)
	done := make(chan bool)

	defer close(rs)
	defer close(errs)
	defer close(done)

	go parser.Parse(parser.ToCamelCase(structName), content, rs, errs, done)

	for {
		select {
		case err := <-errs:
			return err
		case <-done:
			return nil
		case s := <-rs:
			w.Write(newline)
			s.WriteTo(w)
		}
	}
}
Пример #2
0
func TestParse(t *testing.T) {
	data := []byte(`
{
    "name": "Toan",
    "age": 30,
    "active": true,
    "children": [{
        "name": "Hachi",
        "age": 3
    },
    {
        "name": "Yuri",
        "age": 2
    }],
	"group": ["family", "work"],
	"contact": {
		"phone": "9282882822",
		"email": "*****@*****.**"
	}
}
`)

	rs := make(chan *parser.Struct)
	errs := make(chan error)
	done := make(chan bool)

	go parser.Parse("Person", data, rs, errs, done)

	for {
		select {
		case <-done:
			close(rs)
			close(errs)
			close(done)
			return
		case s := <-rs:
			w := &stringWriter{}
			s.WriteTo(w)
			fmt.Println(w.Data)
		}
	}
}