lossless

package module
v0.0.0-...-e0cd1ca Latest Latest
Warning

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

Go to latest
Published: Dec 4, 2018 License: MIT Imports: 5 Imported by: 1

README

json-lossless

json-lossless is a Go library that populates structs from JSON and allows serialization back to JSON without losing fields that are not explicitly defined in the struct.

json-lossless builds off of bit.ly's excellent go-simplejson package.

json-lossless is experimental and probably doesn't work in a lot of cases. Pull requests very welcome.

API

Full API docs are on GoDoc: http://godoc.org/github.com/joeshaw/json-lossless

To get started, embed a lossless.JSON inside your struct:

type Person struct {
        lossless.JSON `json:"-"`

	Name      string `json:"name"`
	Age       int    `json:"age"`
	Address   string
	CreatedAt time.Time
}

Define MarshalJSON and UnmarshalJSON methods on the type to implement the json.Marshaler and json.Unmarshaler interfaces, deferring the work to the lossless.JSON embed:

func (p *Person) UnmarshalJSON(data []byte) error {
	return p.JSON.UnmarshalJSON(p, data)
}

func (p Person) MarshalJSON() ([]byte, error) {
	return p.JSON.MarshalJSON(p)
}

Given JSON like this:

{"name": "Jack Wolfington",
 "age": 42,
 "address": "123 Fake St.",
 "CreatedAt": "2013-09-16T10:44:40.295451647-00:00",
 "Extra": {"foo": "bar"}}

When you decode into a struct, the Extra field will be kept around, even though it's not accessible from your struct.

var p Person
if err := json.Unmarshal(data, &p); err != nil {
        panic(err)
}

data, err := json.Marshal(p)
if err != nil {
        panic(err)
}

// "Extra" is still set in the marshaled JSON:
if bytes.Index(data, "Extra") == -1 {
        panic("Extra not in data!")
}

fmt.Println(string(data))

You can also set arbitrary key/values on your struct by calling Set():

p.Set("Extra", "AgeString", "forty-two")

When serialized, Extra will look like this:

{ ...
  "Extra": {"foo": "bar", "AgeString": "forty-two"}}

Known issues

json-lossless doesn't attempt to decode arrays or simple values. For those, just use json/encoding directly.

The omitempty setting on json tag is not handled. In fact, no parsing of the tags are done at all.

The lossless.JSON value needs to be tagged with json:"-" or it will be marshaled to JSON.

Documentation

Overview

json-lossless populates structs from JSON and allows serialization back to JSON without losing fields that are not explicitly defined in the struct.

Source code: https://github.com/joeshaw/json-lossless

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type JSON

type JSON struct {
	// contains filtered or unexported fields
}

The JSON type contains the state of the decoded data. Embed this type in your type and implement MarshalJSON and UnmarshalJSON methods to add lossless encoding and decoding.

Example:

type Person struct {
    lossless.JSON `json:"-"`

    Name    string
    Age     int
    Address string
}

func (p *Person) UnmarshalJSON(data []byte) error {
    return p.JSON.UnmarshalJSON(p, data)
}

func (p Person) MarshalJSON() ([]byte, error) {
    return p.JSON.MarshalJSON(p)
}

func (*JSON) MarshalJSON

func (js *JSON) MarshalJSON(src interface{}) ([]byte, error)

Marshals the given source into JSON data. Users should call this from their type's MarshalJSON method.

Example:

func (p Person) MarshalJSON() ([]byte, error) {
    return p.JSON.MarshalJSON(p)
}

func (*JSON) Set

func (js *JSON) Set(args ...interface{}) error

Sets a JSON value not represented in the struct type. The argument list is a set of strings referring to the JSON path, with the value to be set as the last value.

Example:

// This sets {"Phone": {"Mobile": "614-555-1212"}} in the JSON
p.Set("Phone", "Mobile", "614-555-1212")

func (*JSON) UnmarshalJSON

func (js *JSON) UnmarshalJSON(dest interface{}, data []byte) error

Unmarshals JSON data into the given destination. Users should call this from their type's UnmarshalJSON method.

Example:

func (p *Person) UnmarshalJSON(data []byte) error {
    return p.JSON.UnmarshalJSON(p, data)
}

Jump to

Keyboard shortcuts

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