From d09f37974b1bbb94d77bfcb2c3a4172c54095cbe Mon Sep 17 00:00:00 2001 From: Oleg Kovalov Date: Fri, 4 Aug 2023 14:07:58 +0200 Subject: [PATCH] Array of objects (#5) --- README.md | 12 ++---------- example_test.go | 27 +++++++++++++++++++++++++++ jsn.go | 3 +++ 3 files changed, 32 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index ebacbb5..f7bb074 100644 --- a/README.md +++ b/README.md @@ -2,8 +2,6 @@ [![build-img]][build-url] [![pkg-img]][pkg-url] -[![reportcard-img]][reportcard-url] -[![coverage-img]][coverage-url] [![version-img]][version-url] Go package to easily construct JSON without defined types. @@ -14,6 +12,8 @@ Go package to easily construct JSON without defined types. * Dependency-free. * Clean and tested code. +See [docs][pkg-url] for more details. + ## Install Go version 1.18+ @@ -50,10 +50,6 @@ fmt.Printf("%s\n", raw) See examples: [example_test.go](example_test.go). -## Documentation - -See [these docs][pkg-url] for more details. - ## License [MIT License](LICENSE). @@ -62,9 +58,5 @@ See [these docs][pkg-url] for more details. [build-url]: https://github.com/cristalhq/jsn/actions [pkg-img]: https://pkg.go.dev/badge/cristalhq/jsn [pkg-url]: https://pkg.go.dev/github.com/cristalhq/jsn -[reportcard-img]: https://goreportcard.com/badge/cristalhq/jsn -[reportcard-url]: https://goreportcard.com/report/cristalhq/jsn -[coverage-img]: https://codecov.io/gh/cristalhq/jsn/branch/main/graph/badge.svg -[coverage-url]: https://codecov.io/gh/cristalhq/jsn [version-img]: https://img.shields.io/github/v/release/cristalhq/jsn [version-url]: https://github.com/cristalhq/jsn/releases diff --git a/example_test.go b/example_test.go index 673e706..4831f67 100644 --- a/example_test.go +++ b/example_test.go @@ -31,6 +31,33 @@ func Example() { //} } +func ExampleAO() { + j := jsn.AO{ + {"a": 1}, + {"b": 2, "c": 3}, + {"d": jsn.O{"1": "2"}}, + } + + raw, _ := json.MarshalIndent(j, "", " ") + fmt.Printf("%s\n", raw) + + // Output: + // [ + // { + // "a": 1 + // }, + // { + // "b": 2, + // "c": 3 + // }, + // { + // "d": { + // "1": "2" + // } + // } + // ] +} + func ExampleNumber() { j := jsn.O{ "x": 123456.00000000000000000000000000000000000000001, diff --git a/jsn.go b/jsn.go index 934d7b2..b760e73 100644 --- a/jsn.go +++ b/jsn.go @@ -6,6 +6,9 @@ type A = []any // O represents JSON object. type O = map[string]any +// AO represents JSON array of JSON objects. +type AO = []O + // N represents JSON number. type N string