Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/rvflash/flat
Flat provides methods to handle JSON, XML or YAML data as a map[string]interface
https://github.com/rvflash/flat
golang json marshalling xml yaml
Last synced: 16 days ago
JSON representation
Flat provides methods to handle JSON, XML or YAML data as a map[string]interface
- Host: GitHub
- URL: https://github.com/rvflash/flat
- Owner: rvflash
- License: mit
- Created: 2021-05-19T20:28:50.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2023-03-17T22:43:48.000Z (almost 2 years ago)
- Last Synced: 2024-11-30T05:09:55.212Z (3 months ago)
- Topics: golang, json, marshalling, xml, yaml
- Language: Go
- Homepage:
- Size: 40 KB
- Stars: 2
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Flat
[![GoDoc](https://godoc.org/github.com/rvflash/flat?status.svg)](https://godoc.org/github.com/rvflash/flat)
[![Build Status](https://github.com/rvflash/flat/workflows/build/badge.svg)](https://github.com/rvflash/flat/actions?workflow=build)
[![Code Coverage](https://codecov.io/gh/rvflash/flat/branch/main/graph/badge.svg)](https://codecov.io/gh/rvflash/flat)
[![Go Report Card](https://goreportcard.com/badge/github.com/rvflash/flat?)](https://goreportcard.com/report/github.com/rvflash/flat)The package `flat` provides methods to handle JSON, XML or YAML data as a `map[string]interface{}`,
useful to manipulate unknown structures or to flatten them into a single dimension.### Installation
To install it, you need to install Go and set your Go workspace first.
Then, download and install it:```bash
$ go get -u github.com/rvflash/flat
```
Import it in your code:```go
import "github.com/rvflash/flat"
```### Prerequisite
`flat` uses the Go modules that required Go 1.11 or later.
### XML Samples (see the example tests)
> Errors are ignored just for the demo.
#### Marshal
```go
var (
d = map[string]interface{}{
"languages": map[string]interface{}{
"en": "English",
"fr": "French",
},
}
res = bytes.Buffer{}
attrs = []xml.Attr{
{Name: xml.Name{Local: "xmlns:xsi"}, Value: "http://www.w3.org/2001/XMLSchema-instance"},
}
_ = flat.New(
d,
flat.XMLName("custom"),
flat.XMLNS("http://schemas.xmlsoap.org/soap/envelope/"),
flat.XMLAttributes(attrs),
).XMLEncode(&res)
)
fmt.Println(res.String())
// Output:
// EnglishFrench
```#### Unmarshal
```go
var (
d = flat.D{}
_ = xml.Unmarshal([]byte(`EnglishFrench`), &d)
)
fmt.Printf("%#v", d.Flatten())
// Output:
// map[string]interface {}{"languages_en":"English", "languages_fr":"French"}
```