Help us shape the future of Go! Take the 2026 Go Developer Survey

jsonx

package
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 3, 2026 License: Apache-2.0 Imports: 3 Imported by: 0

Documentation

Overview

Package jsonx 提供独立于传输协议的 JSON 首值解码,保留解码期间观察到的底层读取错误。

以下示例需导入 fmt、strings 和 github.com/bpcoder16/pixiu/jsonx:

var value struct {
	Count int `json:"count"`
}
reader := strings.NewReader(`{"count":42}`)
if err := jsonx.DecodeOne(reader, &value); err != nil {
	return err
}
fmt.Println(value.Count)

DecodeOne 不关闭输入,不设置大小限制或超时;文件、HTTP 响应体等资源由调用方管理。 首值解析成功后不检查尾部或额外确认 EOF,接受后续存在其他 JSON 或非法内容。 解码器可能预读后续字节;连续解码多个值时应持有同一个 json.Decoder。

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func DecodeOne

func DecodeOne(reader io.Reader, target any) error

DecodeOne 解码首个 JSON 值到 target,不检查尾部或额外确认 EOF。 reader 必须非 nil;target 的类型与未知字段处理沿用 encoding/json 的默认规则。 解码期间观察到的读取错误不会因已解析出完整值而被忽略,可通过错误链检查。 返回错误时 target 可能已被赋值,不应作为成功结果使用。 本函数不关闭 reader,也不设置大小限制或超时;解码器可能预读后续内容, 需要连续解码多个值时应由调用方持有同一个 json.Decoder。

Example
package main

import (
	"fmt"
	"strings"

	"github.com/bpcoder16/pixiu/jsonx"
)

func main() {
	var value struct {
		Count int `json:"count"`
	}
	reader := strings.NewReader(`{"count":42}`)
	if err := jsonx.DecodeOne(reader, &value); err != nil {
		fmt.Println(err)
		return
	}
	fmt.Println(value.Count)
}
Output:
42

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL