cesanta / ubjson

2 stars 1 forks source link

Decoding of top level value types #1

Closed mkmik closed 8 years ago

mkmik commented 8 years ago
func testFloat64() {
  v := float64(3.14)
  b, err := ubjson.Marshal(&v)
  if err != nil {
    log.Fatal(err)
  }

  var v2 float64
  if err := ubjson.Unmarshal(b, &v2); err != nil {
    log.Fatal(err)
  }
  if v2 != v {
    log.Fatalf("want: %v got: %v", v, v2)
  }
}
I0911 12:37:19.340474    4877 scanner.go:32] "D" -> float64
I0911 12:37:19.340945    4877 scanner.go:32] "@" -> continue
I0911 12:37:19.340953    4877 scanner.go:32] "\t" -> continue
I0911 12:37:19.340958    4877 scanner.go:32] "\x1e" -> continue
I0911 12:37:19.340961    4877 scanner.go:32] "\xb8" -> continue
I0911 12:37:19.340964    4877 scanner.go:32] "Q" -> continue
I0911 12:37:19.340967    4877 scanner.go:32] "\xeb" -> continue
I0911 12:37:19.340970    4877 scanner.go:32] "\x85" -> continue
I0911 12:37:19.340973    4877 scanner.go:32] "\x1f" -> endPayload
2015/09/11 12:37:19 unexpected end of JSON input
mkmik commented 8 years ago

sorry, it has nothing to do with float64, but rather with the fact that it's a top level element.

This passes:

func testFloat64() {
  v := []float64{3.14}
  b, err := ubjson.Marshal(&v)
  if err != nil {
    log.Fatal(err)
  }

  var v2 []float64
  if err := ubjson.Unmarshal(b, &v2); err != nil {
    log.Fatal(err)
  }

  if len(v2) != len(v) && v2[0] != v[0] {
    log.Fatalf("want: %v got: %v", v, v2)
  }

  fmt.Println("got", v2)
}
imax9000 commented 8 years ago

Ugh, that's the case for which original parser inserted a space at the end of byte stream.

imax9000 commented 8 years ago

Should be ok after https://github.com/cesanta/ubjson/commit/cc6295523fd32b6a1ae2fdc16c67ece8d6ba4dfd

mkmik commented 8 years ago

works thanks