Skip to content

Latest commit

 

History

History
169 lines (135 loc) · 5.33 KB

README.md

File metadata and controls

169 lines (135 loc) · 5.33 KB

conjson

Build Status Coverage Status Go Report Card GoDoc Latest Stable Version

conjson - (conventional, consistent, conformative) JSON

A simple, functional, no-tags-required mechanism to handle and transform JSON representations of values, consistently.

Project Status

This project is currently in "pre-release". While the code is heavily tested, the API may change. Vendor or "lock" this dependency if you plan on using it.

History and Motivation

This project was originally born from a 3+ year old (at the time of creation) "Gist".

Both that Gist, and eventually this larger project, were inspired by a desire to more easily work with APIs that accepted/returned JSON that had "snake_case"-style object keys.

Basically, I wanted a way to Marshal and Unmarshal Go structures without having to add "tags" to each and every field of each and every structure. That Gist solved that problem for me, and now this library can do the same but with more power and flexibility.

Examples

Marshal a Go structure into "conventional" style JSON

model := exampleModel{
	Title:         "Example Title",
	Description:   "This is a description.",
	ImageURL:      "https://example.com/image.png",
	ReferredByURL: "https://example.com/referrer/index.html",
	IsActive:      true,
	CreatedAt:     inceptionTime,
	UpdatedAt:     packageTime,
}

marshaler := conjson.NewMarshaler(model, transform.ConventionalKeys())

encoded, _ := json.MarshalIndent(marshaler, marshalPrefix, marshalIndent)

fmt.Println(string(encoded))
// Output:
// {
//     "title": "Example Title",
//     "description": "This is a description.",
//     "image_url": "https://example.com/image.png",
//     "referred_by_url": "https://example.com/referrer/index.html",
//     "is_active": true,
//     "created_at": "2015-11-17T20:43:31-05:00",
//     "updated_at": "2018-12-24T13:21:15-07:00"
// }

Unmarshal "conventional" style JSON into a Go structure

sampleJSON := `
{
	"title": "Example Title",
	"description": "This is a description.",
	"image_url": "https://example.com/image.png",
	"referred_by_url": "https://example.com/referrer/index.html",
	"is_active": true,
	"created_at": "2015-11-17T20:43:31-05:00",
	"updated_at": "2018-12-24T13:21:15-07:00"
}
`

var model exampleModel

json.Unmarshal(
	[]byte(sampleJSON),
	conjson.NewUnmarshaler(&model, transform.ConventionalKeys()),
)

// Print the "raw" model JSON to show result
rawJSON, _ := json.MarshalIndent(model, marshalPrefix, marshalIndent)
fmt.Println(string(rawJSON))
// Output:
// {
//     "Title": "Example Title",
//     "Description": "This is a description.",
//     "ImageURL": "https://example.com/image.png",
//     "ReferredByURL": "https://example.com/referrer/index.html",
//     "IsActive": true,
//     "CreatedAt": "2015-11-17T20:43:31-05:00",
//     "UpdatedAt": "2018-12-24T13:21:15-07:00"
// }

Encode a Go structure into "camelCase" style JSON

model := exampleModel{
	Title:         "Example Title",
	Description:   "This is a description.",
	ImageURL:      "https://example.com/image.png",
	ReferredByURL: "https://example.com/referrer/index.html",
	IsActive:      true,
	CreatedAt:     inceptionTime,
	UpdatedAt:     packageTime,
}

jsonEncoder := json.NewEncoder(os.Stdout)
jsonEncoder.SetIndent(marshalPrefix, marshalIndent)

conjson.NewEncoder(jsonEncoder, transform.CamelCaseKeys(false)).Encode(model)

// Output:
// {
//     "title": "Example Title",
//     "description": "This is a description.",
//     "imageURL": "https://example.com/image.png",
//     "referredByURL": "https://example.com/referrer/index.html",
//     "isActive": true,
//     "createdAt": "2015-11-17T20:43:31-05:00",
//     "updatedAt": "2018-12-24T13:21:15-07:00"
// }

Decode JSON with atypical keys into a Go structure

sampleJSON := `
{
	"$title--": "Example Title",
	"$description--": "This is a description.",
	"$image_url--": "https://example.com/image.png",
	"$referred_by_url--": "https://example.com/referrer/index.html",
	"$is_active--": true,
	"created_at--": "2015-11-17T20:43:31-05:00",
	"updated_at--": "2018-12-24T13:21:15-07:00"
}
`

var model exampleModel

decoder := conjson.NewDecoder(
	json.NewDecoder(bytes.NewBufferString(sampleJSON)),
	transform.ConventionalKeys(),
	transform.ValidIdentifierKeys(),
)

decoder.Decode(&model)

// Print the "raw" model JSON to show result
rawJSON, _ := json.MarshalIndent(model, marshalPrefix, marshalIndent)
fmt.Println(string(rawJSON))
// Output:
// {
//     "Title": "Example Title",
//     "Description": "This is a description.",
//     "ImageURL": "https://example.com/image.png",
//     "ReferredByURL": "https://example.com/referrer/index.html",
//     "IsActive": true,
//     "CreatedAt": "2015-11-17T20:43:31-05:00",
//     "UpdatedAt": "2018-12-24T13:21:15-07:00"
// }