https://github.com/enrichman/gofield
Keep light your JSON!
https://github.com/enrichman/gofield
go golang json
Last synced: 3 months ago
JSON representation
Keep light your JSON!
- Host: GitHub
- URL: https://github.com/enrichman/gofield
- Owner: enrichman
- License: mit
- Created: 2017-02-13T22:40:52.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2017-02-15T22:14:38.000Z (over 9 years ago)
- Last Synced: 2024-10-03T09:31:00.645Z (over 1 year ago)
- Topics: go, golang, json
- Language: Go
- Size: 69.3 KB
- Stars: 2
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
# Gofield [](https://travis-ci.org/enrichman/gofield)
Gofield will reduce your JSON serialized Go struct in a lighter object to keep your client happy!
You can use the "Facebook convention" in order to select the fields object: comma separated values, with curly brackets for the inner objects.
```go
fields := "foo,bar{inner_foo,inner_bar}"
lighterObj := gofield.Reduce(fatObj, fields)
```
i.e.:
if you have a JSON like
```json
{
"name": "Enrico",
"surname": "Candino",
"age": 27,
"city": "Rome",
"email":[
{
"name": "work",
"email": "mymail@work.it"
},
{
"name": "personal",
"email": "mymail@personal.it"
}
]
}
```
you can reduce it like this selecting the fields `name,email{name},city`:
```go
gofield.Reduce(fatObj, "name,email{name},city")
```
```json
{
"name": "Enrico",
"city": "Rome",
"email":[
{
"name": "work"
},
{
"name": "personal"
}
]
}
```
You can obviously nest as many levels as you want!
A sample server can be run on the `:8080` to test it.
```shell
go run cmd/gofield-server/main.go
```
Just send your JSON with a POST on the `/reduce` endpoint, with a query parameter `fields`. :)
```
POST /reduce?fields=foo,bar{inner_bar}
```
Note:
performance were not benchmarked, so do your homework before use it on production!
Issues and PR are welcome!
Developed By
--------
Enrico Candino - www.enricocandino.it
License
--------
The MIT License (MIT)
Copyright (c) 2017 Enrico Candino
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.