json-to-bson-go

command module
Version: v0.1.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 11, 2021 License: Apache-2.0 Imports: 0 Imported by: 0

README

License

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, and is ideal to be used to read/write into MongoDB.

This module is able to process both formats:

Install

go get -u github.com/sindbach/json-to-bson-go

Usage Example

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:

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.

Documentation

The Go Gopher

There is no documentation for this package.

Source Files

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
t or T : Toggle theme light dark auto
y or Y : Canonical URL