-
Notifications
You must be signed in to change notification settings - Fork 213
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
int64 id value changed while decoding #202
Comments
Disregard the previous. |
@quetzyg I just upgraded to |
@gadelkareem it looks like you are decoding JSON not JSON API; not sure if this was intended or not. A JSON API representation would look something more like:
To further help I've tried to replicate what you are seeing with a couple tests: package main
import (
"encoding/json"
"fmt"
"strings"
"testing"
"github.com/google/jsonapi"
)
type model struct {
ID int64 `jsonapi:"primary,r" json:"id"`
}
const data = `{"id":"1616867757123793000"}`
func TestJSONAPI(t *testing.T) {
m := &model{}
if err := jsonapi.UnmarshalPayload(strings.NewReader(data), m); err != nil {
t.Fatal(err)
}
fmt.Printf("%#v", m)
}
func TestJSON(t *testing.T) {
m := &model{}
if err := json.Unmarshal([]byte(data), m); err != nil {
t.Fatal(err)
}
fmt.Printf("%#v", m)
} Running the tests via
If you are using JSON, is it possible you are setting the value before decoding? More verbose code/examples would help in debugging. |
@aren55555 I gave a wrong example, sorry for that. Here is the correct request I am sending
The debug output for that ID is |
@gadelkareem just a heads up that you pasted in your Authorization token into the cURL, you may want to redact it! I've managed to replicate this with a unit test: package main
import (
"strings"
"testing"
"github.com/google/jsonapi"
)
type model struct {
ID int64 `jsonapi:"primary,notifications"`
ReadReceipt bool `jsonapi:"attr,read_receipt"`
}
const data = `{
"data": {
"type": "notifications",
"id": "1616867757123793000",
"attributes": {
"read_receipt": true
}
}
}`
func TestJSONAPI(t *testing.T) {
m := &model{}
if err := jsonapi.UnmarshalPayload(strings.NewReader(data), m); err != nil {
t.Fatal(err)
}
if got, want := m.ID, int64(1616867757123793000); got != want {
t.Fatalf("got %v, want %v", got, want)
}
} Running
My gut feeling hypothesis is that this likely has something to do with the string => int64 parsing. I'll have to look more deeply into this. |
Thanks @aren55555 |
From what I can tell, this happens because the current approach to JSON decoding transforms any numeric value into a if err := json.NewDecoder(in).Decode(payload); err != nil {
return err
} In order to preserve the literal value when unmarshalling, we should tell the decoder to save the value as string with decoder := json.NewDecoder(in)
decoder.UseNumber()
if err := decoder.Decode(payload); err != nil {
return err
} The Using the JSON decoder with default settings we get:
Hint: See the differences in Telling the JSON decoder to preserve numbers we get:
@aren55555 I'm able to help out, if you want. |
@quetzyg if you want to take a swipe at it and submit a well tested PR I can take a look at this earliest on Sunday (not at a computer today) or later during the work week. LMK as I plan to fix either this or the rfc3339 thing this weekend. |
I'll take a stab at this in the next few hours. |
@quetzyg I encountered this Curious if you were able to make any progress with |
I've made some progress, but haven't finished yet. I can open a draft PR with what I have done so far, if you want? |
Mind checking the PR that fixes this issue @aren55555 ? |
Using an id of
1616867757123793000
, the jsonapi decoded the value into1616867757123792896
. Any idea what's wrong?ex:
json
after decode:
The text was updated successfully, but these errors were encountered: