Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/lucassscaravelli/ej
Write and read JSON from different sources in one line
https://github.com/lucassscaravelli/ej
go golang json
Last synced: about 2 months ago
JSON representation
Write and read JSON from different sources in one line
- Host: GitHub
- URL: https://github.com/lucassscaravelli/ej
- Owner: lucassscaravelli
- License: mit
- Created: 2020-01-04T17:39:35.000Z (almost 5 years ago)
- Default Branch: master
- Last Pushed: 2020-04-07T00:36:07.000Z (over 4 years ago)
- Last Synced: 2024-07-31T20:52:04.016Z (5 months ago)
- Topics: go, golang, json
- Language: Go
- Homepage:
- Size: 10.7 KB
- Stars: 10
- Watchers: 2
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.MD
- License: LICENSE
Awesome Lists containing this project
- awesome-go - ej - Write and read JSON from different sources succinctly. (JSON / Search and Analytic Databases)
- awesome-go-extra - ej - 01-04T17:39:35Z|2020-04-07T00:36:07Z| (JSON / Advanced Console UIs)
README
# Easy Json (EJ)
[![Go Report Card](https://goreportcard.com/badge/github.com/lucassscaravelli/ej)](https://goreportcard.com/report/github.com/lucassscaravelli/ej)
[![Build Status](https://travis-ci.org/lucassscaravelli/ej.svg?branch=master)](https://travis-ci.org/lucassscaravelli/ej)
[![Coverage Status](https://coveralls.io/repos/github/lucassscaravelli/ej/badge.svg?branch=master)](https://coveralls.io/github/lucassscaravelli/ej?branch=master)
[![GoDoc](https://godoc.org/github.com/lucassscaravelli/ej?status.svg)](https://godoc.org/github.com/lucassscaravelli/ej)
[![Mentioned in Awesome Go](https://awesome.re/mentioned-badge.svg)](https://github.com/avelino/awesome-go)Package ```ej``` implements a JSON handler to write and read json succinctly from different sources like files and http requests.
## Install
```
go get -u github.com/lucassscaravelli/ej
```## Examples
### File
```go
package maintype exData struct {
Hello int
World []string
}func main() {
dataWrite := &exData{
Hello: 1,
World: []string{"h", "e", "l", "l", "o"},
}var dataRead exData
// marshal the content of "dataWrite" to JSON and write in "ex.json" file
if err := ej.JSON(from.File("ex.json")).Write(&dataWrite); err != nil {
log.Fatal(err)
}
// read the data of "ex.json" file and unmarshal the JSON to "dataRead" content
if err := ej.JSON(from.File("ex.json")).ParseToData(&dataRead); err != nil {
log.Fatal(err)
}
// equal content
fmt.Printf("dataWrite: %+v\n", dataWrite)
fmt.Printf("dataRead: %+v\n", dataRead)
}```
### HTTP Request
```go
package mainimport (
"log"
"net/http""github.com/lucassscaravelil/ej"
"github.com/lucassscaravelil/ej/from"
)type requestPayload struct {
NumberToFind int
Numbers []int
}type responseErrorPayload struct {
StatusCode int
ErrorTxt string
}type responsePayload struct {
Found bool
Number int
}func writeError(jsonHandler *ej.EJ, status int, err error) {
jsonHandler.Write(&responseErrorPayload{
StatusCode: status,
ErrorTxt: err.Error(),
})
}func main() {
http.HandleFunc("/bar", func(w http.ResponseWriter, r *http.Request) {
var bodyData requestPayload
jsonHandler := ej.JSON(from.Request(w, r))
if err := jsonHandler.ParseToData(&bodyData); err != nil {
writeError(jsonHandler, http.StatusBadRequest, err)
return
}found := false
foundNumber := 0for _, number := range bodyData.Numbers {
if number == bodyData.NumberToFind {
found = true
foundNumber = number
break
}
}jsonHandler.Write(&responsePayload{
Found: found,
Number: foundNumber,
})return
})log.Fatal(http.ListenAndServe(":8080", nil))
}
```### HTTP Response
``` go
package mainimport (
"log"
"net/http""github.com/lucassscaravelil/ej"
"github.com/lucassscaravelil/ej/from"
)type testData struct {
Count int
Txt string
}func main() {
var responseData testData
response, err := http.Get("http:///any")
if err != nil {
log.Fatal(err)
}if err := ej.JSON(Response(response)).ParseToData(&responseData); err != nil {
log.Fatal(err)
}fmt.Printf("dataRead: %+v\n", responseData)
}
```