jsondoc

package
v0.0.0-...-d31700d Latest Latest
Warning

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

Go to latest
Published: Mar 28, 2022 License: MIT Imports: 3 Imported by: 0

README

jsondoc

Package jsondoc makes a json like documentation for human reading, not for machine decoding.

It's source code is copied from the Golang 1.12.5 encoding/json package, and made these modifications:

  1. Remove the unwanted codes, retain only MarshalIndent.

  2. Refactor giant encode.go into encoder sub package; refactor scanner.go and indent.go into scanner sub package.

  3. Extract comment from struct field tags("comment" or "c") and output after the field.

func ExampleMarshalIndent_comments() {
	var strct = struct {
		A string `c:"注释A"`
		B string `c:"注释B"`
		C string
		D string `c:"<注释D>"`
	}{
		A: "1",
		B: "2",
		C: "3",
		D: "4",
	}
	b, err := MarshalIndent(strct, false, ``, `  `)
	fmt.Println(string(b), err)

	// Output:
	// {
	//   "A": "1",	 # 注释A
	//   "B": "2",	 # 注释B
	//   "C": "3",
	//   "D": "4"	 # <注释D>
	// } <nil>
}
  1. Empty slice or map is converted to a slice or map with one zero value element. Empty pointer is converted to a zero value of the pointed-to type. It stops after the first time if it makes a recursion.
func ExampleMarshalIndent_empty_slice() {
	type node struct {
		Name     string `c:"名称"`
		Children []node `c:"孩子"`
	}
	b, err := MarshalIndent(node{}, false, ``, `  `)
	fmt.Println(string(b), err)

	// Output:
	// {
	//   "Name": "",	 # 名称
	//   "Children": [	 # 孩子
	//     {
	//       "Name": "",	 # 名称
	//       "Children": null	 # 孩子
	//     }
	//   ]
	// } <nil>
}
func ExampleMarshalIndent_empty_map() {
	type node struct {
		Name     string       `c:"名称"`
		Children map[int]node `c:"孩子"`
	}
	b, err := MarshalIndent(node{}, false, ``, `  `)
	fmt.Println(string(b), err)

	// Output:
	// {
	//   "Name": "",	 # 名称
	//   "Children": {	 # 孩子
	//     "0": {
	//       "Name": "",	 # 名称
	//       "Children": null	 # 孩子
	//     }
	//   }
	// } <nil>
}

func ExampleMarshalIndent_nil_pointer() {
	type node struct {
		Name string `c:"名称"`
		Next *node  `c:"后一个"`
		Time *time.Time
	}
	b, err := MarshalIndent(node{}, false, ``, `  `)
	fmt.Println(string(b), err)

	// Output:
	// {
	//   "Name": "",	 # 名称
	//   "Next": {	 # 后一个
	//     "Name": "",	 # 名称
	//     "Next": null,	 # 后一个
	//     "Time": "0001-01-01T00:00:00Z"
	//   },
	//   "Time": "0001-01-01T00:00:00Z"
	// } <nil>
}

func ExampleMarshalIndent_nil_pointer_to_anonymous_field() {
	type node struct {
		Name string `c:"名称"`
		Next *node  `c:"后一个"`
		Time *time.Time
	}
	b, err := MarshalIndent(struct{ *node }{}, false, ``, `  `)
	fmt.Println(string(b), err)

	// Output:
	// {
	//   "Name": "",	 # 名称
	//   "Next": null,	 # 后一个
	//   "Time": "0001-01-01T00:00:00Z"
	// } <nil>
}

Documentation

Overview

Package json implements encoding and decoding of JSON as defined in RFC 7159. The mapping between JSON and Go values is described in the documentation for the Marshal and Unmarshal functions.

See "JSON and Go" for an introduction to this package: https://golang.org/doc/articles/json_and_go.html

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func MarshalIndent

func MarshalIndent(v interface{}, escapeHTML bool, prefix, indent string) ([]byte, error)

MarshalIndent is like json.Marshal but applies Indent to format the output. Each JSON element in the output will begin on a new line beginning with prefix followed by one or more copies of indent according to the indentation nesting.

Example (Comments)
var strct = struct {
	A string `c:"注释A"`
	B string `c:"注释B"`
	C string
	D string `c:"<注释D>"`
}{
	A: "1",
	B: "2",
	C: "3",
	D: "4",
}
b, err := MarshalIndent(strct, false, ``, `  `)
fmt.Println(string(b), err)
Output:

{
  "A": "1",	 # 注释A
  "B": "2",	 # 注释B
  "C": "3",
  "D": "4"	 # <注释D>
} <nil>
Example (Empty_map)
type node struct {
	Name     string       `c:"名称"`
	Children map[int]node `c:"孩子"`
}
b, err := MarshalIndent(node{}, false, ``, `  `)
fmt.Println(string(b), err)
Output:

{
  "Name": "",	 # 名称
  "Children": {	 # 孩子
    "0": {
      "Name": "",	 # 名称
      "Children": null	 # 孩子
    }
  }
} <nil>
Example (Empty_slice)
type node struct {
	Name     string `c:"名称"`
	Children []node `c:"孩子"`
}
b, err := MarshalIndent(node{}, false, ``, `  `)
fmt.Println(string(b), err)
Output:

{
  "Name": "",	 # 名称
  "Children": [	 # 孩子
    {
      "Name": "",	 # 名称
      "Children": null	 # 孩子
    }
  ]
} <nil>
Example (Nil_pointer)
type node struct {
	Name string `c:"名称"`
	Next *node  `c:"后一个"`
	Time *time.Time
}
b, err := MarshalIndent(node{}, false, ``, `  `)
fmt.Println(string(b), err)
Output:

{
  "Name": "",	 # 名称
  "*Next": {	 # 后一个
    "Name": "",	 # 名称
    "*Next": null,	 # 后一个
    "*Time": "0001-01-01T00:00:00Z"
  },
  "*Time": "0001-01-01T00:00:00Z"
} <nil>
Example (Nil_pointer_to_anonymous_field)
type node struct {
	Name string `c:"名称"`
	Next *node  `c:"后一个"`
	Time *time.Time
}
b, err := MarshalIndent(struct{ *node }{}, false, ``, `  `)
fmt.Println(string(b), err)
Output:

{
  "Name": "",	 # 名称
  "*Next": null,	 # 后一个
  "*Time": "0001-01-01T00:00:00Z"
} <nil>

Types

This section is empty.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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