object

package
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Jul 25, 2020 License: MIT Imports: 4 Imported by: 0

README

JSON

JSON (JavaScript Object Notation) is a lightweight data-interchange format.

My Summary of the JSON Specification

JSON is built on two structures:

  • object: collection of name/value pairs (in go this is a map)
  • array: an ordered list of values (in go we use slices to access arrays)
  • structures can be nested arbitrarily

JSON and Golang

General
  • encode and decode using the Marshal and Unmarshal functions
  • JSON objects only support string keys
  • complex types can't be encoded
  • cyclic data structures can't be encoded
  • pointers are encoded as the values they point to, or null if the pointer is nil
Encoding / Decoding
  • JSON can be decoded into and encoded from defined go structs
  • Unmarshal will only decode fields it can match to the go type
  • can use json tags in the struct definition to aid in decoding
  • if the structure is unknown, can decode into the interface{} type
    • this lets us decode arbitrary json into a go structure

Data Types

Object
  • unordered set of name/value pairs
  • begins with { and ends with }
  • each name is followed by a :
  • name/value pairs are separated by ,
Array
  • ordered collection of values
  • begins with [ and ends with ]
  • values are separated by ,
Value

values can be nested, and can be any of the following:

  • a string in double quotes
  • a number
  • a boolean (true or false)
  • null
  • an object
  • an array
String
  • zero or more unicode chars wrapped in double quotes
  • uses backslash escapes
Number
  • can be positive or negative, indicated by a leading -
  • can be a float, using either decimal or exponent notation
  • octal and hex formats are not supported
Whitespace
  • spaces, tabs, linefeeds, cariage returns
  • can be inserted between any pair of tokens

Documentation

Overview

Package object provides implementations of the Object interface for the possible JSON input value types.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ArrayObj added in v0.1.1

type ArrayObj struct {
	*Prefix
	Val []Object
}

ArrayObj implements the Object interface for a JSON array.

func NewArrayObj added in v0.1.1

func NewArrayObj(prefix string, input []interface{}) (*ArrayObj, error)

NewArrayObj returns a ArrayObj for the given input slice.

func (ArrayObj) Parse added in v0.1.1

func (o ArrayObj) Parse() ([][]string, error)

Parse returns the 2d slice of strings for the given ArrayObj.

type BoolObj

type BoolObj struct {
	*Prefix
	Val bool
}

BoolObj implements the Object interface for a bool value.

func NewBoolObj

func NewBoolObj(prefix string, input bool) *BoolObj

NewBoolObj returns a BoolObj for the given input bool.

func (BoolObj) Parse

func (o BoolObj) Parse() ([][]string, error)

Parse returns the 2d slice of strings for the given BoolObj.

type ByPrefix

type ByPrefix []Object

ByPrefix implements the sort.Interface on the Prefix field.

func (ByPrefix) Len

func (objs ByPrefix) Len() int

func (ByPrefix) Less

func (objs ByPrefix) Less(i, j int) bool

func (ByPrefix) Swap

func (objs ByPrefix) Swap(i, j int)

type MapObj

type MapObj struct {
	*Prefix
	SortedKeys []string
	Val        map[string]Object
}

MapObj implements the Object interface for a JSON map.

func NewMapObj

func NewMapObj(prefix string, input map[string]interface{}) (*MapObj, error)

NewMapObj returns a MapObj for the given input map.

func (MapObj) Parse

func (o MapObj) Parse() ([][]string, error)

Parse returns the 2d slice of strings for the given MapObj.

type NumberObj added in v0.1.1

type NumberObj struct {
	*Prefix
	Val float64
}

NumberObj implements the Object interface for a numeric value.

func NewNumberObj added in v0.1.1

func NewNumberObj(prefix string, input float64) *NumberObj

NewNumberObj returns a NumberObj for the given input float.

func (NumberObj) Parse added in v0.1.1

func (o NumberObj) Parse() ([][]string, error)

Parse returns the 2d slice of strings for the given FloatObj.

We do a check to see if the value is actually an integer, and treat if so. This check is necessary because the results of unmarshalling a JSON byte array into a map[string]interface{} treats all numeric values as floats. We want the string representation that we eventually write to a CSV file to have numeric values appear accurately if they're integers. i.e. "345" not "345.0".

type Object

type Object interface {
	Parse() ([][]string, error)
	// contains filtered or unexported methods
}

Object is representation of a JSON object.

func FromInterface

func FromInterface(prefix string, input interface{}) (Object, error)

FromInterface returns the Object for the given input interface and returns an error if the interface is of an invalid type.

type Prefix added in v0.1.1

type Prefix string

Prefix is just a string, we redefine it so we can implement the getPrefix method of the Object interface once rather than for each new Object type.

func NewPrefix added in v0.1.1

func NewPrefix(p string) *Prefix

NewPrefix returns a pointer to a Prefix for the given string.

type StringObj

type StringObj struct {
	*Prefix
	Val string
}

StringObj implements the Object interface for a string value.

func NewStringObj

func NewStringObj(prefix string, input string) *StringObj

NewStringObj returns a StringObj for the given input string.

func (StringObj) Parse

func (o StringObj) Parse() ([][]string, error)

Parse returns the 2d slice of strings for the given StringObj.

Jump to

Keyboard shortcuts

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