https://github.com/kpsaurus/gin-json-renderer
A custom JSON renderer for Gin framework
https://github.com/kpsaurus/gin-json-renderer
framework gin go golang
Last synced: 3 months ago
JSON representation
A custom JSON renderer for Gin framework
- Host: GitHub
- URL: https://github.com/kpsaurus/gin-json-renderer
- Owner: kpsaurus
- License: mit
- Created: 2025-01-07T17:10:08.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2025-01-07T17:11:23.000Z (over 1 year ago)
- Last Synced: 2025-06-09T13:41:38.742Z (10 months ago)
- Topics: framework, gin, go, golang
- Language: Go
- Homepage:
- Size: 6.84 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# JSON renderer for Gin framework
`ginjsonrenderer` is a custom JSON renderer for the [Gin framework](https://github.com/gin-gonic/gin). Heavily inspired by Django REST framework's JSON renderer, it provides a consistent and structured way to return JSON responses, including support for pagination, custom data, and error handling.
## Features
- **Standardized JSON Response Structure**: Ensure consistent API responses across your application.
- **Pagination Support**: Easily include pagination metadata in your responses.
- **Customizable**: Define HTTP status codes, data payloads, and error details.
- **Seamless Integration**: Built specifically for the Gin framework.
## Installation
Install the package using `go get`:
```bash
go get github.com/yourusername/ginjsonrenderer
```
## Usage
```bash
import "github.com/kpsaurus/ginjsonrenderer"
```
## Basic Usage
Example: Basic JSON Response
Here's a basic example of how to use ginjsonrenderer to return a JSON response:
```go
package main
import (
"net/http"
"github.com/gin-gonic/gin"
"github.com/kpsaurus/ginjsonrenderer"
)
func main() {
r := gin.Default()
r.GET("/example", func(c *gin.Context) {
data := gin.H{"message": "Hello, World!"}
ginjsonrenderer.JSON(c, http.StatusOK, data, nil)
})
r.Run(":8080")
}
```
Example: JSON Response with Pagination
You can include pagination details in your response by passing a Pagination struct:
```go
r.GET("/paginated-example", func(c *gin.Context) {
pagination := &ginjsonrenderer.Pagination{
Offset: 0,
Limit: 10,
Total: 2,
}
data := gin.H{"items": []string{"item1", "item2"}}
ginjsonrenderer.JSON(c, http.StatusOK, data, nil, pagination)
})
```
Response Format
The response structure will look like this:
```json
{
"status": 200,
"message": "OK",
"pagination": {
"offset": 0,
"limit": 10,
"total": 2
},
"data": {
"items": ["item1", "item2"]
},
"errors": null
}
```
## Function Reference
```go
func JSON(c *gin.Context, statusCode int, data interface{}, errors interface{}, pagination ...*Pagination)
```
- c: Gin's context.
- statusCode: HTTP status code (e.g., http.StatusOK).
- data: The data payload to include in the response.
- errors: Error details, if any (can be nil if no errors).
- pagination: (Optional) Pagination metadata.
## Contributing
Contributions are welcome! Feel free to open issues or submit pull requests for improvements or bug fixes.
## License
This project is licensed under the MIT License.