parser

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: May 17, 2026 License: MIT Imports: 12 Imported by: 5

README

parser — JSON & CSV parsing with schema validation

import "github.com/downsized-devs/sdk-go/parser"

Stability: Stable — see STABILITY.md

JSON parsing via json-iterator/go (configurable presets), CSV via gocsv, and optional JSON-Schema validation via gojsonschema.

Features

  • JsonInterface with Marshal/Unmarshal variants (5 modes: standard, default, fastest, custom, etc.)
  • JSON Schema validation against a registered schema set
  • CsvInterface for typed CSV reads
  • Single InitParser factory returns both

Installation

go get github.com/downsized-devs/sdk-go/parser

Quick Start

p := parser.InitParser(log, parser.Options{
    Json: parser.JsonOptions{ Config: parser.DefaultConfig },
})
jp := p.JSONParser()

raw, _ := jp.Marshal(map[string]any{"hello": "world"})
var out map[string]any
_ = jp.Unmarshal(raw, &out)

API Reference

Symbol Signature
InitParser func InitParser(log logger.Interface, opts Options) Parser
Parser.JSONParser () JsonInterface
Parser.CSVParser () CsvInterface
JsonInterface.Marshal (any) ([]byte, error)
JsonInterface.Unmarshal ([]byte, any) error
JsonInterface.ValidateAgainstSchema (name string, data []byte) error
CsvInterface.Marshal / Unmarshal typed conversions

Configuration

Option Purpose
Json.Config Preset: vanillaCompatible, defaultConfig, fastestConfig, customConfig.
Json.Schema Map of schema name → file or URL path.
Csv CSV-specific options.

Examples

JSON Schema validation
p := parser.InitParser(log, parser.Options{
    Json: parser.JsonOptions{
        Schema: map[string]string{ "user": "file://schemas/user.json" },
    },
})
if err := p.JSONParser().ValidateAgainstSchema("user", raw); err != nil {
    return errors.WrapWithCode(err, codes.CodeJSONSchemaInvalid, "user payload")
}

Error Handling

Validation/parse errors are wrapped with codes parser codes.

Dependencies

  • Internal: codes, errors, logger
  • External: github.com/json-iterator/go, github.com/xeipuuv/gojsonschema, github.com/gocarina/gocsv

Testing

go test ./parser/...

JSON path is covered; CSV path currently has no tests.

Contributing

See CONTRIBUTING.md.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CsvInterface

type CsvInterface interface {
	SetOptionsRead(opt CsvOptions)
	SetOptionsWrite(opt CsvOptions)
	Marshal(orig interface{}) ([]byte, error)
	MarshalWithoutHeaders(orig interface{}) ([]byte, error)
	Unmarshal(blob []byte, dest interface{}) error
}

type CsvOptions

type CsvOptions struct {
	Separator  rune
	LazyQuotes bool
}

type Interface added in v1.0.0

type Interface interface {
	JsonParser() JsonInterface
	CsvParser() CsvInterface
}

func Init added in v1.0.0

func Init(log logger.Interface, opt Options) Interface

type JsonInterface

type JsonInterface interface {
	// Marshal go structs into bytes
	Marshal(orig interface{}) ([]byte, error)
	// Marshal go structs into bytes and validates returned bytes
	MarshalWithSchemaValidation(sch string, orig interface{}) ([]byte, error)
	// Unmarshal bytes into go structs
	Unmarshal(blob []byte, dest interface{}) error
	// Validates input bytes and Unmarshal
	UnmarshalWithSchemaValidation(sch string, blob []byte, dest interface{}) error
}

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
	// Schema contains schema definitions with key as schema name and value as source path
	// schema sources can be file or URL. Schema definition will be iniatialized during
	// JSON parser object initialization.
	Schema map[string]string
}

type Options

type Options struct {
	JsonOptions JsonOptions
	CsvOptions  CsvOptions
}

Jump to

Keyboard shortcuts

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