Int64 encoded as string
Closed this issue · 8 comments
I can see the #16 was closed like this:
@MangelMaxime wrote:
I don't see any problem with using string to represent int64 in JSON.
Our data provider requires us to send:
{
"command": "getTickPrices",
"arguments": {
"timestamp": 1637589093796
}
}
1637589093796
is out of range for int
, so how am I to create a valid JSON request? They won't accept "1637589093796"
. This default behavior is a trouble, how to work it around?
I was hoping for float
to the rescue, but well:
> let x = 1637589093796.;;
val x : float = 1.637589094e+12
> Encode.float x |> Encode.toString 0;;
val it : string = "1637589093796.0"
The data provider is not happy with this .0
as well. It must be an integer, not a string or decimal or float.
Hello @witoldsz,
you can write your own Encode.int64
encoder which use number instead of string to represents the type in the JSON.
module Thoth.Json.Net.Extra
open Thoth.Json.Net
module Encode =
let int64 // ....
Then in another file you can do
open Thoth.Json.Net
open Thoth.Json.Net.Extra
Encode.int64 // <- Here you will have your version because it was open after
This works only if you use manual coders because current version of Thoth.Json.Net doesn't allow yet to replace "native" coders in the Auto API.
Thanks, this is better than what I've came up with:
Encode.object [ "timestamp", JsonValue.FromObject (ts - 10_000L) ]
Would the JsonValue.FromObject
work in Thoth.Net
Thoth.Json
too?
For Auto API, one should introduce some tag like type MyInt64 of int64
and decode it instead.
The only thing missing right now is to mention it in docs, so there are no surprises.
Would the JsonValue.FromObject work in Thoth.Net too?
Are you speaking about Thoth.Json or Thoth.Json.Net here?
For Thoth.Json, you will probably be able to just do box value
as it is done for int
Just make sure to test the maximal and minimal values to be sure that it works for all of them as expected.
I think it might be better to actually name things, so I would rather introduce some auxiliary type like TimestampMillis
and make encoder for that very type.
You can add them by yourself, Thoth.Json is extensible for this reason. Or create a package which extends Thoth.Json if you need to have them in several of your project.
I don't want to saturate the core library with them right now. Perhaps, I will revisit my position in the future but for me right now Thoth.Json works as expected. I can't add all the types that everyone need because it will increase the bundle size and the maintenace cost of the library.
I already regret having said yes to some of the features added to Thoth.Json so I am really careful now :)
Sure, but the docs should mention the issue and describe solution. I can prepare something, maybe together with thoth-org/Thoth.Json#124 to make this great lib a better experience for newcomers.
The documentation has been completely rewritten and now explain how the numbers are represented and why.
https://thoth-org.github.io/Thoth.Json/documentation/manual/json-representation.html#numbers
For Auto API, one should introduce some tag like type MyInt64 of int64 and decode it instead.
This is not true, you can override the default for primitives to in the auto API.
https://thoth-org.github.io/Thoth.Json/documentation/auto/json-representation.html#primitives
I am closing this issue because for me this is normal and there are work around if needed. Thank you for taking the time to discuss it.