Force decimal formatting for ints on JSON output (#1840)

* Force decimal formatting for ints on JSON output

* update a test case
This commit is contained in:
John Kerl 2025-07-20 17:42:37 -04:00 committed by GitHub
parent fccdf215e6
commit 9445046bfe
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 12 additions and 2 deletions

View file

@ -13,6 +13,7 @@ import (
"encoding/json"
"fmt"
"io"
"strconv"
"github.com/johnkerl/miller/v6/pkg/colorizer"
"github.com/johnkerl/miller/v6/pkg/lib"
@ -406,7 +407,16 @@ func millerJSONEncodeString(input string) string {
// ----------------------------------------------------------------
func (mv *Mlrval) marshalJSONInt(outputIsStdout bool) (string, error) {
lib.InternalCodingErrorIf(mv.mvtype != MT_INT)
return colorizer.MaybeColorizeValue(mv.String(), outputIsStdout), nil
// Other formats would use mv.String(): for example, if the user used hex
// format, we would emit whatever they set. However, for JSON, we are
// required to disrespect the user's formatting, and only emit decimal.
// See also https://github.com/johnkerl/miller/issues/1761.
ival, ok := mv.GetIntValue()
if !ok {
panic("Internal coding error: int-typed mlrval denied int access")
}
s := strconv.FormatInt(ival, 10)
return colorizer.MaybeColorizeValue(s, outputIsStdout), nil
}
// ----------------------------------------------------------------

View file

@ -1,7 +1,7 @@
[
{
"hostname": "localhost",
"pid": 0x3039,
"pid": 12345,
"req": {
"id": 6789,
"method": "GET",