parser

package
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: May 26, 2026 License: MIT Imports: 3 Imported by: 1 module, 5 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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

func InitParser(opt Options) Parser

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

Jump to

Keyboard shortcuts

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