Win a copy of Node.js Design Patterns: Design and implement production-grade Node.js applications using proven patterns and techniques this week in the Server-Side JavaScript and NodeJS forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

Preserving int64 values without rounding when parsing struct/json in Go

 
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I have been struggling with an issue with the usage of int64 inside a struct. When parsing into Json in Go and from the 17th digits onwards I have noticed that it starts round the int64 up. For example: 2882795323592627689 => 2882795323592627700

My understanding is that json "numbers" get treated as floating points and therefore they are limited to 53 digits of integer precision. I cannot change my original struct and from the client point of view I still need to have a int64 and it is imperative to have it the same as it identifies a specific request. Can you help with this? This is my current implementation:

Function to convert []bytes to int64:



This, in turns, is used inside a function that construct the struct mapping and does the Json unmarshalling:




I have consulted various resources, but with no joy as my main issue seems to be to be able to treat "in a special way" only the int64 in the struct:

https://stackoverflow.com/questions/16946306/preserve-int64-values-when-parsing-json-in-go
https://github.com/golang/go/issues/5562
https://play.golang.org/p/gHEj8WHuTx

Any idea, can you help?

Thank you
 
Geane Norm
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Geane Norm wrote:Hi,
I have been struggling with an issue with the usage of int64 inside a struct. When parsing into Json in Go and from the 17th digits onwards I have noticed that it starts round the int64 up. For example: 2882795323592627689 => 2882795323592627700

My understanding is that json "numbers" get treated as floating points and therefore they are limited to 53 digits of integer precision. I cannot change my original struct and from the client point of view I still need to have a int64 and it is imperative to have it the same as it identifies a specific request. Can you help with this? This is my current implementation:

Function to convert []bytes to int64:



This, in turns, is used inside a function that construct the struct mapping and does the Json unmarshalling:




I have consulted various resources, but with no joy as my main issue seems to be to be able to treat "in a special way" only the int64 in the struct:

https://stackoverflow.com/questions/16946306/preserve-int64-values-when-parsing-json-in-go
https://github.com/golang/go/issues/5562
https://play.golang.org/p/gHEj8WHuTx

Any idea, can you help?

Thank you



I have found this: https://github.com/square/go-jose/issues/351

Is this even an issue that can be fixed or I will always face rounding up for in64 bigger than 17 digits? The issue I have is that this needs being rather generic for a multiple of fields one of which unfortunately is int64 and exceeds 17 digits, therefore the situation is complicated by the usage of the generic. I have attempted at using my own field in a custom struct, but I cannot make it generic enough
 
Marshal
Posts: 22461
121
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This issue doesn't appear if you use encoding/json to parse to a struct that has an int64 property. For instance:

Output:

The map contains a floating point number which has the issue you see. The struct does not.
 
Geane Norm
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rob Spoor wrote:This issue doesn't appear if you use encoding/json to parse to a struct that has an int64 property. For instance:

Output:

The map contains a floating point number which has the issue you see. The struct does not.



Thank you for your reply, appreciated. Yes, indeed, when unmarshalling with a struct containing the int64 value does work, however the issue is that I am dealing with several models and my utils needs being generic (and it is in fact shared across severall apis) and therefore I cannot pass a specific struct. I indeed need to use in the map. Is there any other way this can be solved? Thank you
 
Rob Spoor
Marshal
Posts: 22461
121
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm not familiar enough with Go's JSON parsing capabilities to help you any further, I'm afraid.
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic