Documentation
¶
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type JSONInterface ¶
type JSONInterface interface {
Marshal(orig any) ([]byte, error)
Unmarshal(blob []byte, dest any) error
}
JSONInterface defines the contract for JSON marshaling and unmarshaling operations.
type JSONOptions ¶
type JSONOptions struct {
Config jsonConfig
IndentionStep int
MarshalFloatWith6Digits bool
EscapeHTML bool
SortMapKeys bool
UseNumber bool
DisallowUnknownFields bool
TagKey string
OnlyTaggedField bool
ValidateJSONRawMessage bool
ObjectFieldMustBeSimpleString bool
CaseSensitive bool
}
JSONOptions configures the JSON parser behavior including encoding settings.
type Options ¶
type Options struct {
JSONOptions JSONOptions
}
Options configures the parser with specific settings for each format.
type Parser ¶
type Parser interface {
JSONParser() JSONInterface
}
Parser provides access to different parsing implementations.
func InitParser ¶
InitParser creates a new Parser instance configured with the given options.
Example ¶
package main
import (
"fmt"
"github.com/alpardfm/go-toolkit/parser"
)
func main() {
// Initialize a parser with standard JSON configuration
p := parser.InitParser(parser.Options{
JSONOptions: parser.JSONOptions{},
})
// Marshal a struct to JSON
type User struct {
Name string `json:"name"`
Age int `json:"age"`
}
data, err := p.JSONParser().Marshal(User{Name: "Alice", Age: 30})
if err != nil {
fmt.Println("error:", err)
return
}
fmt.Println(string(data))
// Unmarshal JSON back to a struct
var user User
err = p.JSONParser().Unmarshal(data, &user)
if err != nil {
fmt.Println("error:", err)
return
}
fmt.Println(user.Name, user.Age)
}
Output: {"name":"Alice","age":30} Alice 30