qson

package
v0.0.0-...-2d8e61d Latest Latest
Warning

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

Go to latest
Published: Feb 1, 2021 License: Apache-2.0, MIT Imports: 5 Imported by: 0

README

qson

This is copy from https://github.com/joncalhoun/qson As author says he is not acrivelly maintains the repo and not plan to do that.

Usage

You can either turn a URL query param into a JSON byte array, or unmarshal that directly into a Go object.

Transforming the URL query param into a JSON byte array:

import "github.com/joncalhoun/qson"

func main() {
  b, err := qson.ToJSON("bar%5Bone%5D%5Btwo%5D=2&bar[one][red]=112")
  if err != nil {
    panic(err)
  }
  fmt.Println(string(b))
  // Should output: {"bar":{"one":{"red":112,"two":2}}}
}

Or unmarshalling directly into a Go object using JSON struct tags:

import "github.com/joncalhoun/qson"

type unmarshalT struct {
	A string     `json:"a"`
	B unmarshalB `json:"b"`
}
type unmarshalB struct {
	C int `json:"c"`
}

func main() {
  var out unmarshalT
  query := "a=xyz&b[c]=456"
  err := Unmarshal(&out, query)
  if err != nil {
  	t.Error(err)
  }
  // out should equal
  //   unmarshalT{
	// 	  A: "xyz",
	// 	  B: unmarshalB{
	// 	  	C: 456,
	// 	  },
	//   }
}

To get a query string like in the two previous examples you can use the RawQuery field on the net/url.URL type.

Documentation

Overview

Package qson implmenets decoding of URL query params into JSON and Go values (using JSON struct tags).

See https://golang.org/pkg/encoding/json/ for more details on JSON struct tags.

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	// ErrInvalidParam is returned when invalid data is provided to the ToJSON or Unmarshal function.
	// Specifically, this will be returned when there is no equals sign present in the URL query parameter.
	ErrInvalidParam error = errors.New("qson: invalid url query param provided")
)

Functions

func ToJSON

func ToJSON(query string) ([]byte, error)

ToJSON will turn a query string like:

cat=1&bar%5Bone%5D%5Btwo%5D=2&bar[one][red]=112

Into a JSON object with all the data merged as nicely as possible. Eg the example above would output:

{"bar":{"one":{"two":2,"red":112}}}
Example
b, err := ToJSON("a=xyz&b[c]=456")
if err != nil {
	panic(err)
}
fmt.Printf(string(b))
Output:

{"a":"xyz","b":{"c":456}}

func Unmarshal

func Unmarshal(dst interface{}, query string) error

Unmarshal will take a dest along with URL query params and attempt to first turn the query params into JSON and then unmarshal those into the dest variable

BUG(joncalhoun): If a URL query param value is something like 123 but is expected to be parsed into a string this will currently result in an error because the JSON transformation will assume this is intended to be an int. This should only affect the Unmarshal function and could likely be fixed, but someone will need to submit a PR if they want that fixed.

Example
type Ex struct {
	A string `json:"a"`
	B struct {
		C int `json:"c"`
	} `json:"b"`
}
var ex Ex
if err := Unmarshal(&ex, "a=xyz&b[c]=456"); err != nil {
	panic(err)
}
fmt.Printf("%+v\n", ex)
Output:

{A:xyz B:{C:456}}

Types

This section is empty.

Notes

Bugs

  • If a URL query param value is something like 123 but is expected to be parsed into a string this will currently result in an error because the JSON transformation will assume this is intended to be an int. This should only affect the Unmarshal function and could likely be fixed, but someone will need to submit a PR if they want that fixed.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL