Documentation
¶
Index ¶
- Variables
- func NewLineReader(r io.Reader) *lineReader
- func ToRFC8259(src []byte) []byte
- func Unmarshal(data []byte, v any) error
- func UnmarshalJSONC(data []byte, v any) error
- func UnmarshalRead(r io.Reader, v any) error
- func UnmarshalerWithLocation[T any](r *lineReader, hooks ...DecodeHook) *json.Unmarshalers
- type DecodeHook
- type Location
- type ObjectLocation
- type TokenType
Constants ¶
This section is empty.
Variables ¶
var SetLocationHook = DecodeHook{ After: func(_ *jsontext.Decoder, target any, location types.Location) { if loc, ok := target.(ObjectLocation); ok { loc.SetLocation(location) } }, }
Functions ¶
func NewLineReader ¶
NewLineReader creates a new line reader.
func ToRFC8259 ¶
ToRFC8259 converts JSONC (JSON with Comments) to valid JSON following RFC8259. It strips out comments and trailing commas while maintaining the exact character offsets as the input. This ensures that any JSON parser locations will map directly back to the original source file positions.
Both line numbers and character positions are preserved in the output. Comments and trailing commas are replaced with spaces without changing line counts.
Comments can be either: - Single-line: starting with // and continuing to the end of the line - Multi-line: starting with /* and ending with */
Trailing commas are allowed in JSONC but not in standard JSON, so they are replaced with spaces to maintain character offsets.
func UnmarshalJSONC ¶
UnmarshalJSONC parses JSONC (JSON with Comments) data into the specified value. It first converts JSONC to standard JSON following RFC8259 and then unmarshals it. This is a convenience function that combines ToRFC8259 and Unmarshal.
The parser preserves line number information, which is essential for reporting errors at their correct locations in the original file.
Usage example:
type Config struct {
Name string `json:"name"`
Version string `json:"version"`
xjson.Location // Embed Location to get line number info
}
var config Config
if err := xjson.UnmarshalJSONC(data, &config); err != nil {
return err
}
func UnmarshalerWithLocation ¶
func UnmarshalerWithLocation[T any](r *lineReader, hooks ...DecodeHook) *json.Unmarshalers
UnmarshalerWithLocation returns a json.Unmarshalers that captures the source code location (start and end lines) of each decoded object and optionally invokes hooks after decoding.
By default, the SetLocationHook is used to record source code locations, unless other hooks are explicitly provided.
To use UnmarshalerWithLocation for primitive types, you must implement the json.UnmarshalerFrom interface for those objects. cf. https://pkg.go.dev/github.com/go-json-experiment/json#UnmarshalerFrom
The returned unmarshalers resolve decoder offsets to lines through r and keep decoding state, so they must not be used concurrently or with another reader. Pass them to the decoder as is: the nested decode relies on the decoder options carrying them.
Types ¶
type DecodeHook ¶
type Location ¶
Location is wrap of types.Location. This struct is required when you need to detect location of your object from json file.
func (*Location) SetLocation ¶
type ObjectLocation ¶
ObjectLocation is required when you need to save Location for your struct.