All Projects → Rican7 → conjson

Rican7 / conjson

Licence: MIT license
(conventional, consistent, conformative) JSON - A simple, functional, no-tags-required mechanism to handle and transform JSON representations of values, consistently.

Programming Languages

go
31211 projects - #10 most used programming language
Makefile
30231 projects

Projects that are alternatives of or similar to conjson

fefe
Validate, sanitize and transform values with proper TypeScript types and zero dependencies.
Stars: ✭ 34 (-27.66%)
Mutual labels:  functional, transform
fastener
Functional Zipper for manipulating JSON
Stars: ✭ 54 (+14.89%)
Mutual labels:  functional, transform
parse-commit-message
(!! moved to tunnckoCore/opensource !! try `parse-commit-message@canary`) Parse, stringify or validate a commit messages that follows Conventional Commits Specification
Stars: ✭ 31 (-34.04%)
Mutual labels:  conventional
spatialmath-matlab
Create, manipulate and convert representations of position and orientation in 2D or 3D using Python
Stars: ✭ 142 (+202.13%)
Mutual labels:  transform
stream
Go Stream, like Java 8 Stream.
Stars: ✭ 60 (+27.66%)
Mutual labels:  functional
react-native-less-transformer
Use Less to style your React Native apps.
Stars: ✭ 26 (-44.68%)
Mutual labels:  transform
zio-http4s-example
For anyone who's struggling to put an http4s server together with ZIO
Stars: ✭ 19 (-59.57%)
Mutual labels:  functional
apropos
Fast strong typed 'Either' data structure for typescript and flow
Stars: ✭ 20 (-57.45%)
Mutual labels:  functional
potassium
A framework for writing robot software with functional programming in Scala
Stars: ✭ 16 (-65.96%)
Mutual labels:  functional
treecko
A collection of functional and immutable helpers for working with tree data structures.
Stars: ✭ 31 (-34.04%)
Mutual labels:  functional
rudash
Rudash - Lodash for Ruby Apps
Stars: ✭ 27 (-42.55%)
Mutual labels:  functional
wrangler
Wrangler Transform: A DMD system for transforming Big Data
Stars: ✭ 63 (+34.04%)
Mutual labels:  transform
snap
Snap Programming Language
Stars: ✭ 20 (-57.45%)
Mutual labels:  functional
func-dependency-injection-go
Dependency injection example using higher order functions
Stars: ✭ 26 (-44.68%)
Mutual labels:  functional
salt
The compilation target that functional programmers always wanted.
Stars: ✭ 62 (+31.91%)
Mutual labels:  functional
TypeInferencer
Algorithm W and Algorithm M in F#
Stars: ✭ 33 (-29.79%)
Mutual labels:  functional
Latte-lang
100% Java compatibility and Functional Programming.
Stars: ✭ 128 (+172.34%)
Mutual labels:  functional
vallang
Generic immutable recursive data representation API targeted at source code models and more.
Stars: ✭ 28 (-40.43%)
Mutual labels:  functional
iterum
Handling iterables like lazy arrays.
Stars: ✭ 28 (-40.43%)
Mutual labels:  functional
Functional-Light-JS-Zh
《Functional-Light-JS》中文翻译
Stars: ✭ 14 (-70.21%)
Mutual labels:  functional

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"
// }
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].