mirror of
https://github.com/json-iterator/go.git
synced 2024-11-27 08:30:57 +02:00
demonstrate how to customize float encoding
This commit is contained in:
parent
1df353727b
commit
3f1fcaff87
@ -34,9 +34,9 @@ func (stream *Stream) WriteFloat32(val float32) {
|
|||||||
for p := precision - 1; p > 0 && fval < POW10[p]; p-- {
|
for p := precision - 1; p > 0 && fval < POW10[p]; p-- {
|
||||||
stream.writeByte('0')
|
stream.writeByte('0')
|
||||||
}
|
}
|
||||||
stream.WriteUint64(fval);
|
stream.WriteUint64(fval)
|
||||||
for stream.buf[stream.n - 1] == '0' {
|
for stream.buf[stream.n - 1] == '0' {
|
||||||
stream.n--;
|
stream.n--
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -64,8 +64,8 @@ func (stream *Stream) WriteFloat64(val float64) {
|
|||||||
for p := precision - 1; p > 0 && fval < POW10[p]; p-- {
|
for p := precision - 1; p > 0 && fval < POW10[p]; p-- {
|
||||||
stream.writeByte('0')
|
stream.writeByte('0')
|
||||||
}
|
}
|
||||||
stream.WriteUint64(fval);
|
stream.WriteUint64(fval)
|
||||||
for stream.buf[stream.n - 1] == '0' {
|
for stream.buf[stream.n - 1] == '0' {
|
||||||
stream.n--;
|
stream.n--
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -4,6 +4,8 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"testing"
|
"testing"
|
||||||
"github.com/json-iterator/go/require"
|
"github.com/json-iterator/go/require"
|
||||||
|
"unsafe"
|
||||||
|
"strconv"
|
||||||
)
|
)
|
||||||
|
|
||||||
func Test_bind_api_demo(t *testing.T) {
|
func Test_bind_api_demo(t *testing.T) {
|
||||||
@ -22,3 +24,38 @@ func Test_iterator_api_demo(t *testing.T) {
|
|||||||
}
|
}
|
||||||
fmt.Println(total)
|
fmt.Println(total)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type DocumentMatch struct {
|
||||||
|
Index string `json:"index,omitempty"`
|
||||||
|
ID string `json:"id"`
|
||||||
|
Score float64 `json:"score"`
|
||||||
|
Sort []string `json:"sort,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type DocumentMatchCollection []*DocumentMatch
|
||||||
|
|
||||||
|
type SearchResult struct {
|
||||||
|
Hits DocumentMatchCollection `json:"hits"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func Test2(t *testing.T) {
|
||||||
|
RegisterTypeEncoder("float64", func(ptr unsafe.Pointer, stream *Stream) {
|
||||||
|
t := *((*float64)(ptr))
|
||||||
|
stream.WriteRaw(strconv.FormatFloat(t, 'E', -1, 64))
|
||||||
|
})
|
||||||
|
hits := []byte(`{"hits":[{"index":"geo","id":"firehouse_grill_brewery","score":3.584608106366055e-07,
|
||||||
|
"sort":[" \u0001@\t\u0007\u0013;a\u001b}W"]},
|
||||||
|
{"index":"geo","id":"jack_s_brewing","score":2.3332790568885077e-07,
|
||||||
|
"sort":[" \u0001@\u0013{w?.\"0\u0010"]},
|
||||||
|
{"index":"geo","id":"brewpub_on_the_green","score":2.3332790568885077e-07,
|
||||||
|
"sort":[" \u0001@\u0014\u0017+\u00137QZG"]}]}`)
|
||||||
|
var h SearchResult
|
||||||
|
err := Unmarshal(hits, &h)
|
||||||
|
fmt.Printf("SR %+v \n", h.Hits[0])
|
||||||
|
b, err := Marshal(h.Hits[0])
|
||||||
|
if err != nil {
|
||||||
|
fmt.Printf("error marshalling search res: %v", err)
|
||||||
|
//return
|
||||||
|
}
|
||||||
|
fmt.Printf("SR %s \n", string(b))
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user