Documentation
¶
Index ¶
- func Decode(data string) (interface{}, error)
- func DecodeTyped(data string, v interface{}) error
- func DetectFormat(data string) string
- func Encode(v interface{}) (string, error)
- func Parse(data string, hint ...string) (interface{}, error)
- func ParseFile(filename string, data []byte, v interface{}) error
- func ParseTyped(data string, v interface{}, hint ...string) error
- func ProcessDecode(process *process.Process) interface{}
- func ProcessEncode(process *process.Process) interface{}
- func ProcessParse(process *process.Process) interface{}
- func ProcessRepair(process *process.Process) interface{}
- func ProcessValidate(process *process.Process) interface{}
- func ProcessValidateSchema(process *process.Process) interface{}
- func Repair(data string) (string, error)
- func TrimComments(data []byte) []byte
- func Validate(data interface{}, schema interface{}) error
- func ValidateSchema(schema interface{}) error
- type Validator
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func DecodeTyped ¶
DecodeTyped decodes JSON string to the given typed pointer
func DetectFormat ¶
DetectFormat attempts to detect the format of the data Returns: "yaml", "json", "jsonc", or "" if uncertain
func Parse ¶
Parse parses data in multiple formats (JSON, JSONC, Yao, YAML) with auto-repair Parameters:
- data: the data string to parse
- hint: optional format hint (e.g., "file.yaml", ".json", "yaml") If not provided, will attempt to auto-detect the format
func ParseFile ¶
ParseFile parses data based on file extension Supports: .json, .jsonc, .yao, .yaml, .yml
func ParseTyped ¶
ParseTyped parses data into a typed pointer with format detection
func ProcessDecode ¶
ProcessDecode json.Decode Standard JSON decoding Args: data string - JSON string to decode Returns: decoded value
func ProcessEncode ¶
ProcessEncode json.Encode Standard JSON encoding Args: value interface{} - The value to encode Returns: JSON string
func ProcessParse ¶
ProcessParse json.Parse Universal parser supporting JSON, JSONC, Yao, YAML with auto-repair Args:
- data string - The data string to parse
- hint string (optional) - Format hint (e.g., "file.yaml", ".json", "yaml")
Returns: parsed value
Usage:
// Parse JSON (with auto-repair if broken)
var data = Process("json.Parse", jsonString)
// Parse YAML
var data = Process("json.Parse", yamlString, "file.yaml")
// Parse Yao/JSONC (with comments)
var data = Process("json.Parse", yaoString, ".yao")
func ProcessRepair ¶
ProcessRepair json.Repair Repairs broken JSON Args: data string - Broken JSON string Returns: repaired JSON string
func ProcessValidate ¶
ProcessValidate json.Validate Validates data against a JSON Schema Args:
- data interface{} - The data to validate
- schema interface{} - The JSON Schema (map, string, or []byte)
Returns: nil if valid, error message string if invalid
Usage:
// Validate with schema map
var result = Process("json.Validate", data, schema)
if (result != null) {
log.Error("Validation failed: " + result)
}
// Validate with JSON string
var schemaStr = '{"type": "object", "properties": {"name": {"type": "string"}}}'
var result = Process("json.Validate", {"name": "John"}, schemaStr)
func ProcessValidateSchema ¶
ProcessValidateSchema json.ValidateSchema Validates a JSON Schema structure Args: schema interface{} - The JSON Schema to validate Returns: nil if valid, error message string if invalid
func TrimComments ¶
TrimComments removes comments from JSONC/Yao format data Supports both single-line (//) and multi-line (/* */) comments
func Validate ¶
func Validate(data interface{}, schema interface{}) error
Validate validates data against a JSON Schema (one-shot validation) Returns error if schema is invalid or data doesn't match the schema
Usage:
err := json.Validate(data, schemaMap)
if err != nil {
log.Printf("Validation failed: %v", err)
}
func ValidateSchema ¶
func ValidateSchema(schema interface{}) error
ValidateSchema validates a JSON Schema structure without compiling it Returns error if the schema is invalid
Types ¶
type Validator ¶
type Validator struct {
// contains filtered or unexported fields
}
Validator wraps a compiled JSON Schema for validation
func NewValidator ¶
NewValidator compiles a JSON Schema and returns a validator Returns error if the schema is invalid
Args:
- schema: can be map[string]interface{}, []byte, string, or any JSON-serializable type
Usage:
// From map
schemaMap := map[string]interface{}{
"type": "object",
"properties": map[string]interface{}{
"name": map[string]interface{}{"type": "string"},
},
"required": []string{"name"},
}
validator, err := json.NewValidator(schemaMap)
// From JSON string
validator, err := json.NewValidator(`{"type": "object", "properties": {...}}`)
// From JSON bytes
validator, err := json.NewValidator([]byte(`{"type": "object", ...}`))
func (*Validator) Validate ¶
Validate validates data against the compiled JSON Schema Returns nil if data is valid, error with validation details otherwise
Usage:
validator, _ := json.NewValidator(schemaMap)
data := map[string]interface{}{"name": "John"}
if err := validator.Validate(data); err != nil {
log.Printf("Validation failed: %v", err)
}