jason

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Feb 20, 2024 License: MIT Imports: 10 Imported by: 0

README

jason

Go Reference Go Report Card

This package is a product of this article by Alex Edwards and jsoniter

Installation 📥

go get github.com/micahasowata/jason@latest

Quick Start 💨

package main

import (
	"log/slog"
	"net/http"

	"github.com/micahasowata/jason"
)

func main() {
	j := jason.New(100, true, true)

	http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
		var input struct {
			Name string `json:"name"`
		}

		err := j.Read(w, r, &input)
		if err != nil {
			e, ok := err.(*jason.Err)
			if !ok {
				http.Error(w, err.Error(), http.StatusBadRequest)
				return
			}
			http.Error(w, e.Error(), e.Code)
			return
		}

		err = j.Write(w, http.StatusOK, jason.Envelope{"data": input}, nil)
		if err != nil {
			e, ok := err.(*jason.Err)
			if !ok {
				http.Error(w, err.Error(), http.StatusBadRequest)
				return
			}
			http.Error(w, e.Error(), e.Code)
			return
		}
	})

	slog.Info("server started")
	http.ListenAndServe(":8000", nil)
}

Contribution 🗳️

If you run into any issues while using this library or you notice any area where it can be improved feel free to create a pull request and be sure that it would not be ignored

Happy Hacking ⚡⚡⚡

Documentation

Index

Constants

View Source
const (
	ContentType     = "Content-Type"
	ContentTypeJSON = "application/json"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Envelope

type Envelope map[string]any

Envelope wraps JSON data for marshalling purposes, guarding against subtle JSON vulnerabilities. For more information, refer to: https://haacked.com/archive/2008/11/20/anatomy-of-a-subtle-json-vulnerability.aspx/

type Err added in v1.0.0

type Err struct {
	// Code is the recommended error code for error handling. It represents an HTTP status code,
	// providing guidance on how to handle the error, such as returning it to the client or logging it.
	Code int `json:"code"`

	// Msg contains the error message, providing detailed information about the encountered error.
	Msg string `json:"msg"`
}

Err represents an error with an associated code and message

func (*Err) Error added in v1.0.0

func (e *Err) Error() string

Error implements the error interface for Err, returning its message.

type Jason

type Jason struct {
	// contains filtered or unexported fields
}

Jason represents a configuration for handling JSON requests and responses

func New

func New(maxBodySize int64, indentResponse bool, disallowUnknownFields bool) *Jason

New creates a new Jason instance with the specified configuration parameters.

func (*Jason) Read

func (j *Jason) Read(w http.ResponseWriter, r *http.Request, dst interface{}) error

Read reads JSON data from the request body and decodes it into the provided destination interface. It checks the content type of the request and ensures that the destination is a pointer. Additionally, it sets limits on the request body size and handles various decoding errors.

func (*Jason) Write

func (j *Jason) Write(w http.ResponseWriter, status int, data Envelope, headers http.Header) error

Write writes the JSON representation of the provided Envelope data to the given http.ResponseWriter. It sets the HTTP status code, headers, and writes the JSON data. If indentation is enabled, it marshals data with indentation. Otherwise, it marshals data without indentation. The provided headers are set to the response writer before writing the data.

Jump to

Keyboard shortcuts

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