https://github.com/knetic/marsalo
Convenience library for unmarshalling HTTP request/response bodies from XML or JSON
https://github.com/knetic/marsalo
Last synced: 8 months ago
JSON representation
Convenience library for unmarshalling HTTP request/response bodies from XML or JSON
- Host: GitHub
- URL: https://github.com/knetic/marsalo
- Owner: Knetic
- Created: 2016-01-22T00:48:45.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2017-05-16T00:43:17.000Z (over 8 years ago)
- Last Synced: 2025-01-07T09:42:58.673Z (9 months ago)
- Language: Go
- Homepage:
- Size: 8.79 KB
- Stars: 0
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
#marsalo
[](https://travis-ci.org/Knetic/marsalo)
[](https://godoc.org/github.com/Knetic/marsalo)Marsalo is a small library that makes one-liner unmarshalling of http responses easy. The library isn't complicated, it's just a convenience library for code that tends to get written over and over.
Marsalo is Esperanto for "marshal".
## Usage
Often you'll have an HTTP request or response whose body will need to be unmarshalled into a real object model. Generally, you don't care if the request body is XML or JSON, you just want the parsed content thereof. This library encapsulates that logic of unmarshalling a request or response body from the appropriate transport type.
Assume you have the following object model (just as an example):
type SomeObjectModel struct {
name string `json:"name" xml:"name"`
location string `json:"location" xml:"location"`
entries int `json:"entries" xml:"entries"`
}And let's say you write a client to an HTTP API which may return either XML or JSON (or maybe you can pick, whatever), but the object represented by the response is the same.
func HandleResponse(response *http.Response) {
var structuredResponse SomeObjectModel
err := marsalo.UnmarshalResponse(response, &structuredResponse)
if(err != nil) {
// some parsing error
return
}
}// Otherwise, structuredResponse now contains the unmarshalled XML or JSON body of [response]
`marsalo` works similarly for writing a webserver which needs to unmarshal the request from a client:
func HandleRequest(request *http.Request) {
var structuredResponse SomeObjectModel
err := marsalo.UnmarshalRequest(request, &structuredResponse)
if(err != nil) {
// some parsing error
return
}
}