// ToJson returns a SearchResult marshalled to JSON bytes.
// XXX: This is terrible but gained several hundred requests/sec over json.Marshall.
ToJson returns a SearchResult marshalled to JSON bytes.
XXX: This is terrible but gained several hundred requests/sec over json.Marshall.
// ToJson returns a SearchResult marshalled to JSON bytes . // XXX : This is terrible but gained several hundred requests / sec over json . Marshall .
func (r SearchResult) ToJson() ([]byte, error) {
	buf := new(bytes.Buffer)
	buf.WriteString(fmt.Sprintf(`{"query":{"lat":%v,"lng":%v},"locations":[`, r.Query.Lat, r.Query.Lng))
	totalLocations := len(r.Locations)
	for x, location := range r.Locations {
		total := len(location.Crimes)
		buf.WriteString(fmt.Sprintf(`{"point":{"lat":%v,"lng":%v},`, location.Point.Lat, location.Point.Lng))
		buf.WriteString(`"crimes":[`)
		line := `{"id":%v,"date":"%v","time":"%v","type":"%v"}`
		for i, crime := range location.Crimes {
			isLast := i == total-1
			buf.WriteString(fmt.Sprintf(line, crime.Id, crime.Date, crime.Time, crime.Type))
			if (total > 1) && !isLast {
				buf.WriteString(",")
			}
		}
		buf.WriteString("]}")
		isLast :=  x == totalLocations-1
		if (totalLocations > 1) && !isLast {
			buf.WriteString(",")
		}	
	}
	buf.WriteString("]}")
	return buf.Bytes(), nil
}
func ( r SearchResult ) ToJson ( ) ( [ ] byte , error ) { buf := new ( bytes . Buffer ) buf . WriteString ( fmt . Sprintf ( `{"query":{"lat":%v,"lng":%v},"locations":[` , r . Query . Lat , r . Query . Lng ) ) totalLocations := len ( r . Locations ) for x , location := range r . Locations { total := len ( location . Crimes ) buf . WriteString ( fmt . Sprintf ( `{"point":{"lat":%v,"lng":%v},` , location . Point . Lat , location . Point . Lng ) ) buf . WriteString ( `"crimes":[` ) line := `{"id":%v,"date":"%v","time":"%v","type":"%v"}` for i , crime := range location . Crimes { isLast := i == total - 1 buf . WriteString ( fmt . Sprintf ( line , crime . Id , crime . Date , crime . Time , crime . Type ) ) if ( total > 1 ) && ! isLast { buf . WriteString ( " " ) } } buf . WriteString ( " " ) isLast := x == totalLocations - 1 if ( totalLocations > 1 ) && ! isLast { buf . WriteString ( " " ) } } buf . WriteString ( " " ) return buf . Bytes ( ) , nil }
ToJson returns a SearchResult marshalled to JSON bytes.
ToJson returns a SearchResult marshalled to JSON bytes .