jason

package module
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Oct 22, 2023 License: MIT Imports: 2 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(r *http.Request, dst any) error

Read reads JSON data from an HTTP request and decodes it into a provided destination variable. It ensures that the JSON data structure matches the destination variable's structure.

Parameters:

r: The HTTP request containing the JSON data in the request body. dst: A destination variable where the JSON data will be decoded.

Returns:

An error if there are issues during reading or decoding; otherwise, it returns nil on success.

The function uses a JSON decoder to read and decode the JSON data from the request body. It disallows unknown fields to ensure the data structure compatibility

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.

Example usage:

params := WriteParams{
    Data:    someData,
    Status:  http.StatusOK,
    Headers: http.Header{
        "Content-Type": []string{"application/json"},
        "Custom-Header": []string{"value"},
    },
}
err := Write(w, params)
if err != nil {
    // Handle the error
}

Types

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