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 ¶
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
}