jason

package module
v0.1.3 Latest Latest
Warning

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

Go to latest
Published: Oct 26, 2023 License: MIT Imports: 4 Imported by: 0

README

Jason

Jason is a wrapper package to ease reading and writing JSON for applications.

It was initially built for me but the license says it can be used by anyone.

So I would be making updates for it to be usable for everyone wanting to build APIs with Go

go get github.com/ma477/jason@v0.1.0

Usage

package main

import (
    "github.com/ma477/jason"
    "net/http"
)

func home(w http.ResponseWriter, r *http.Request) {
    // Set the data type for holding the data to be recieved
    var input struct {
        Name string `json:"name"`
    }

    // Read the data with the Read function.
    err := jason.Read(r, &input)
    if err != nil {
        // Handle it in your preferred manner
    }

    // Write the data back to the client
    p := WriteParams{
        Data: input,
        Status: http.StatusOK,
        Headers: nil,
    }

    err := jason.Write(w, p)
    if err != nil {
        // Handle it in your preferred manner
    }
}

func main() {
    http.HandleFunc("/", home)

    err := http.ListenAndServe(:8000, nil)
    if err != nil {
        // Handle it in your preferred manner
    }
}

Happy using 🚀🚀🚀🚀

Documentation

Overview

Package jason wraps around encoding/json to make it easier to read and write JSON in Go web APIs

Package jason wraps around encoding/json to make it easier to read and write JSON in Go web APIs

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Read

func Read(w http.ResponseWriter, r *http.Request, params ReadParams) error

func Write

func Write(w http.ResponseWriter, params WriteParams) error

Write sends an HTTP response using the provided http.ResponseWriter and WriteParams. It serializes the data in WriteParams.Data to JSON format, sets the HTTP status code, and adds any custom HTTP headers specified in WriteParams.Headers to the response.

Parameters:

  • w: An http.ResponseWriter to write the HTTP response to.
  • params: A WriteParams struct containing data, status code, and headers.

Returns:

  • error: An error if there was an issue serializing the data or writing the response.

Types

type ReadParams added in v0.1.3

type ReadParams struct {
	Dst  any
	Size int64
}

type WriteParams

type WriteParams struct {
	Data    any
	Status  int
	Headers http.Header
}

WriteParams is to store the data , status and headers for the response that would be sent to the client

Jump to

Keyboard shortcuts

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