Refuse a calc over digits(0) or a repeat of 0, name an option's wrong kind as JSON does, type the name scope kinds, and compile only from a jsonvalue.Value

This commit is contained in:
2026-10-08 00:02:07 +02:00
committed by Lillem4n
parent ba2ae41ffe
commit 97e72988b1
13 changed files with 106 additions and 55 deletions
+39
View File
@@ -0,0 +1,39 @@
// Package jsonvalue holds a decoded JSON value whose numbers keep the text they are written in.
package jsonvalue
import (
"bytes"
"encoding/json"
)
// Value is a JSON value that only Decode, String and Object make, so a template never compiles
// from a value encoding/json decoded with its numbers as float64.
type Value struct{ v any }
// Decode decodes one JSON value, keeping each number as the json.Number it is written as.
func Decode(b []byte) (Value, error) {
var v any
if !json.Valid(b) {
return Value{}, json.Unmarshal(b, &v)
}
dec := json.NewDecoder(bytes.NewReader(b))
dec.UseNumber()
err := dec.Decode(&v)
return Value{v}, err
}
// String is the JSON string s.
func String(s string) Value { return Value{s} }
// Object is the JSON object holding fields.
func Object(fields map[string]Value) Value {
m := make(map[string]any, len(fields))
for k, f := range fields {
m[k] = f.v
}
return Value{m}
}
// Any is the value as encoding/json decodes it: a map[string]any, []any, string, json.Number,
// bool or nil.
func (v Value) Any() any { return v.v }