Skip to content
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

JSON number interpreted as float and integer #13241

Closed
Infiziert90 opened this issue Jan 10, 2025 · 2 comments
Closed

JSON number interpreted as float and integer #13241

Infiziert90 opened this issue Jan 10, 2025 · 2 comments

Comments

@Infiziert90
Copy link

What version of Hugo are you using (hugo version)?

0.140.2

Does this issue reproduce with the latest release?

yes

Issue

Having some weirdness going on with a json number, that is interpreted as float and also as integer.
I would understand if it is either float or integer, because of Golangs json parser, but both at the same time?

{{ $data := dict }}
{{ $json := resources.Get (.Param "data") }}
{{ with $json | transform.Unmarshal }}
    {{ $data = . }}
{{ end }}
    
    ...........
    
{{ range $category := $data }}
  ...........
  {{ range .Coffers }}
  <button id="{{ $category.Territory }}-{{ .CofferId }}-tab" class="tablinks" onclick="openTab(event, {{ $category.Territory }}, {{ .CofferId }})">{{ .CofferName }}-{{ .CofferId }}</button>
  {{ end }}
{{ end }}
  

Result:

<button id="763-2.009532e+06-tab" class="tablinks" onclick="openTab(event,  763 ,  2009532 )">Bronze-2.009532e+06</button>
@jmooring
Copy link
Member

jmooring commented Jan 10, 2025

1) Go unmarshals JSON numbers to float64:
https://pkg.go.dev/encoding/json#Unmarshal

2) The default representation of a float64 in Go depends on precision (default precision is 6):

package main

import "fmt"

func main() {
	s := []float64{123456, 1234567}
	fmt.Println(s)
}

The above outputs:

[123456 1.234567e+06]

3) Then why don't we see "2.009532e+06" everywhere in the rendered HTML?

Because Go's html/template package is doing some contextual magic when in a JavaScript context (e.g., inside an onclick attribute). I haven't figured out why Go does this. See https://go.dev/play/p/jK2WWmXVfd3, then change to text/template and test again.

In your case, just cast to string:

id="{{ .CofferId | string }}"

Or to int:

id="{{ .CofferId | int }}"

@Infiziert90
Copy link
Author

Thanks for the quick response, the 3) explains why it happens.
Gonna close this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy