Discover Packages
github.com/ecshreve/jcgo
internal
object
package
Version:
v0.1.2
Opens a new window with list of versions in this module.
Published: Jul 25, 2020
License: MIT
Opens a new window with license information.
Imports: 4
Opens a new window with list of imports.
Imported by: 0
Opens a new window with list of known importers.
README
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
Expand ▾
Collapse ▴
Documentation
¶
Package object provides implementations of the Object interface for the
possible JSON input value types.
ArrayObj implements the Object interface for a JSON array.
NewArrayObj returns a ArrayObj for the given input slice.
Parse returns the 2d slice of strings for the given ArrayObj.
BoolObj implements the Object interface for a bool value.
NewBoolObj returns a BoolObj for the given input bool.
Parse returns the 2d slice of strings for the given BoolObj.
ByPrefix implements the sort.Interface on the Prefix field.
MapObj implements the Object interface for a JSON map.
NewMapObj returns a MapObj for the given input map.
Parse returns the 2d slice of strings for the given MapObj.
NumberObj implements the Object interface for a numeric value.
NewNumberObj returns a NumberObj for the given input float.
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".
Object is representation of a JSON object.
FromInterface returns the Object for the given input interface and returns an
error if the interface is of an invalid type.
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.
NewPrefix returns a pointer to a Prefix for the given string.
StringObj implements the Object interface for a string value.
NewStringObj returns a StringObj for the given input string.
Parse returns the 2d slice of strings for the given StringObj.
Source Files
¶
Click to show internal directories.
Click to hide internal directories.