https://github.com/sindbach/json-to-bson-go
A module to aid developers to generate Go BSON class maps
https://github.com/sindbach/json-to-bson-go
bson converter extended-json golang json mongodb
Last synced: 5 months ago
JSON representation
A module to aid developers to generate Go BSON class maps
- Host: GitHub
- URL: https://github.com/sindbach/json-to-bson-go
- Owner: sindbach
- License: apache-2.0
- Created: 2021-02-21T23:37:42.000Z (over 5 years ago)
- Default Branch: main
- Last Pushed: 2021-03-15T00:52:22.000Z (over 5 years ago)
- Last Synced: 2024-06-21T15:21:00.881Z (about 2 years ago)
- Topics: bson, converter, extended-json, golang, json, mongodb
- Language: Go
- Homepage:
- Size: 59.6 KB
- Stars: 12
- Watchers: 4
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
[](https://opensource.org/licenses/Apache-2.0)
# JSON to BSON for Golang
A module to aid developers to generate Go BSON class maps. The auto-generated code output utilises [go.mongodb.org/mongo-driver/bson](https://pkg.go.dev/go.mongodb.org/mongo-driver/bson), and is ideal to be used to read/write into [MongoDB](https://www.mongodb.com/).
This module is able to process both formats:
* [Standard JSON](https://www.json.org/json-en.html)
* [MongoDB Extended JSON v2](https://docs.mongodb.com/manual/reference/mongodb-extended-json/)
You can see the module in action on https://json-to-bson-map.netlify.app/
## Install
```
go get -u github.com/sindbach/json-to-bson-go
```
## Usage Example
```go
package main
import (
"fmt"
"github.com/sindbach/json-to-bson-go/convert"
"github.com/sindbach/json-to-bson-go/options"
)
func main() {
doc := `{"foo": "buildfest", "bar": {"$numberDecimal":"2021"} }`
opt := options.NewOptions()
result, _ := convert.Convert([]byte(doc), opt)
fmt.Println(result)
}
```
The output of the above code:
```go
package main
import "go.mongodb.org/mongo-driver/bson/primitive"
type Example struct {
Foo string `bson:"foo"`
Bar primitive.Decimal128 `bson:"bar"`
}
```
## Behaviour and Limitations
As of current latest version, these are the limitations:
| Case | Ex. input | Ex. output | Description
|----------------|------------------|-------------------| ---------------|
| Array of array consistent| `[[1, 2], [3, 4]]` | `[][]int32`
| Array of array inconsistent | `[[1, 2], "foo"]` | `[]interface{}`
| Array of array nest level | `[[[1, 2]]]` | `[][]interface{}` |
| Array of document | `[{"a": 1}]` | `[]A{}`
## Options
Available options
| Name | Syntax | Description
| ----------------------- | ---------------- | ---------------- |
| Struct Name | `opt.SetStructName(string)` | Specifies the name of the generated struct. The default value is "AutoGenerated".
| Minimize Integer Size | `opt.SetMinimizeIntegerSize(bool)` | Specifies how JSON numbers should be represented in Go types. If false, numbers in JSON input will always be represented as float64 in structs.If true, JSON numbers will be represented using either int32, int64, float32, or float64. For example, the JSON field "x: 100" would be converted to the struct field "X int32" while "x: 2^32 + 10" would be converted to "X int64" because (2^32 + 10) overflows int32. The default value is `false`
| Trucate Integers | `opt.SetTruncateIntegers(bool)` | Specifies whether or not integer fields in generated structs should have the "truncate" BSON struct tag. This tag enables non-integer data to be decoded into the integer field at the risk of loss of precision. For example, if this option is true and SetMinimizeIntegerSize is true, the JSON field "x: 1.0" would be converted to struct field "X int32 `bson:"x,truncate"`", which would allow data like "x: 5.4" to be decoded into the struct. This option is a no-op if SetMinimizeIntegerSize(false) is also called because in that case, all numeric JSON fields are converted to float64 and the "truncate" tag is not meaningful for that type. The default value is `false`.
## Need help?
If you get stuck, or have a question, please feel free to open an issue.
## License
This repo is covered under [Apache License v2](LICENSE).