tj

package module
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Aug 6, 2022 License: Apache-2.0 Imports: 0 Imported by: 0

README

Construct JSON without backing it up with Go structures

Go Reference

Use tj to produce well-formed schemaless JSON:

json.Marshal(tj.O{
    "hello": tj.A{
        "world",
        42,
        tj.O{
            "go": nil,
        },
    },
})

tj/json2go tool produces tj trees from existing JSON or YAML files.

Why whould I need that?

This package comes in handy for interacting with large amounts of external systems all demanding different JSON schemas. Creating structures, tagging them and filling them is overkill if the structures are used just once.

Why do I need this package to do so?

tj.O and tj.A are short and unobtrusive, this makes the resulting code readable.

Is it always a good idea to use this package?

If you find yourself using tj to describe JSONs with the same schema more than once then it's time to switch to Go struct-backed JSON generation.

Copyright Tectonic Networks, Inc.

Licensed under Apache 2.0 license.

Authors:

Documentation

Overview

Package tj provides shortcuts for constructing JSON without backing it up with Go structures.

Example
package main

import (
	"encoding/json"
	"fmt"

	"github.com/ridge/tj"
)

func main() {
	j := tj.O{
		"hello": tj.A{
			"world",
			42,
			tj.O{
				"go": nil,
			},
		},
	}
	bytes, _ := json.MarshalIndent(j, "", "  ")
	fmt.Printf("%s\n", bytes)
}
Output:
{
  "hello": [
    "world",
    42,
    {
      "go": null
    }
  ]
}

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type A

type A = []interface{}

A is an alias to make writing JSON array literals easier

type O

type O = map[string]interface{}

O is an alias to make writing JSON object literals easier

Directories

Path Synopsis
doc2go module

Jump to

Keyboard shortcuts

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