示例#1
0
func datumToJson(datum *p.Datum) ([]byte, error) {
	switch datum.GetType() {
	case p.Datum_R_NULL:
		return json.Marshal(nil)
	case p.Datum_R_BOOL:
		return json.Marshal(datum.GetRBool())
	case p.Datum_R_NUM:
		return json.Marshal(datum.GetRNum())
	case p.Datum_R_STR:
		return json.Marshal(datum.GetRStr())
	case p.Datum_R_ARRAY:
		items := []string{}
		for _, d := range datum.GetRArray() {
			item, err := datumToJson(d)
			if err != nil {
				return nil, err
			}
			items = append(items, string(item))
		}
		return []byte("[" + strings.Join(items, ",") + "]"), nil
	case p.Datum_R_OBJECT:
		pairs := []string{}
		for _, assoc := range datum.GetRObject() {
			raw_key := assoc.GetKey()
			raw_val := assoc.GetVal()

			// convert to json form
			key, err := json.Marshal(raw_key)
			if err != nil {
				return nil, err
			}
			val, err := datumToJson(raw_val)
			if err != nil {
				return nil, err
			}

			pairs = append(pairs, string(key)+":"+string(val))
		}
		return []byte("{" + strings.Join(pairs, ",") + "}"), nil
	}
	panic("unknown datum type")
}
示例#2
0
文件: datum.go 项目: jfbus/rethinkgo
func isNullDatum(datum *p.Datum) bool {
	return datum.GetType() == p.Datum_R_NULL
}