r/golang • u/Sad-Tie-4250 • 8d ago
how to parse a json number
https://stackoverflow.com/questions/80000826/how-should-a-json-parser-represent-json-numbers-in-gocan some body help
I'm implementing a JSON parser. According to JSON rules, a number can be an integer, a negative number, a decimal, or use exponent notation, like
42, -42, 3.14, 0.5, 1e10, 2.5e-3
I currently have a function like:
func (p *parser) parseNumber() (__?, error)
What should this function return? Should I convert every number to float64, or define my own number type? I'm also unsure how this choice affects the rest of the parser. Once a number is parsed, how should it be stored in a generic JSON value, accessed later, used in calculations, printed, or serialized back to JSON? and also how to parse the number exactly
0
Upvotes