jmcvetta / napping

Golang HTTP client library
286 stars 59 forks source link

Bigints like timestamps in JSON to float64 instead of int64 #14

Closed diegogub closed 10 years ago

diegogub commented 10 years ago

Hello, I'm using napping, but I have problems at the moment with bigints like timestamos and so. It's maped as float64 intead of int64. I've been looking for a solution and there are some workarounds in the json library. It would be cool to include them in napping. I will start to do some changes into the library and try to fix it. What do you think? Is there already a solution in napping and I'm missing it? Thanks ,

jmcvetta commented 10 years ago

Can you clarify your use case? Napping uses the standard encoding/json library for marshaling and unmarshaling. No new/different behavior is added.

I don't understand what data type you are seeing marshaled into a float. For example:

package main

import (
    "encoding/json"
    "math/big"
    "time"
)

func main() {
    type Foo struct {
        Timestamp time.Time
        Bigint    big.Int
    }
    f := Foo{
        Timestamp: time.Now(),
        Bigint:    *big.NewInt(int64(1234567890)),
    }
    b, _ := json.MarshalIndent(&f, "", "    ") // Make pretty JSON
    println(string(b))
}

Produces this output:

{
    "Timestamp": "2014-07-07T17:20:35.32708001-07:00",
    "Bigint": 1234567890
}
jmcvetta commented 10 years ago

Also tried unmarshaling a JSON message containing a large integer:

package main

import (
    "encoding/json"
    "github.com/kr/pretty"
    "log"
)

func main() {
    j := `{"id": 12000000000002539, "Name": "Some Name"}`
    type Msg struct {
        Id   int64
        Name string
    }
    m := new(Msg)
    err := json.Unmarshal([]byte(j), m)
    if err != nil {
        log.Fatalln(err)
    }
    pretty.Println(m)
}

Produces:

&main.Msg{Id:12000000000002539, Name:"Some Name"}

JSON containing integers too large to fit in int64 can be deserialized into big.Int.

FWIW:

$ go version
go version go1.3 linux/amd64
diegogub commented 10 years ago

Hello, I updated my golang version to 1.3 and now it's working :O , I was using older golang and timestamps were parsed as float64 :/ Thanks for the examples.

El 7/7/14 9:41 PM, Jason McVetta escribió:

Also tried unmarshaling a JSON message containing a large integer:

|package main

import ( "encoding/json" "github.com/kr/pretty" "log" )

func main() { j := {"id": 12000000000002539, "Name": "Some Name"} type Msg struct { Id int64 Name string } m := new(Msg) err := json.Unmarshal([]byte(j), m) if err != nil { log.Fatalln(err) } pretty.Println(m) } |

Produces:

&main.Msg{Id:12000000000002539, Name:"Some Name"}

JSON containing integers too large to fit in |int64| can be deserialized into |big.Int|.

FWIW:

$ go version go version go1.3 linux/amd64

— Reply to this email directly or view it on GitHub https://github.com/jmcvetta/napping/issues/14#issuecomment-48259252.

jmcvetta commented 10 years ago

Awesome!