package
0.0.0-20140208234550-8ce6181c2609
Repository: https://github.com/codegangsta/martini-contrib.git
Documentation: pkg.go.dev

# README

Encoder.

This is a simple wrapper to the json.Marshal, which adds ability to skip some fields of structure.
Unlike 'render' package it doesn't write anything, just returns marshalled data.
It's useful for things like passwords, statuses, activation codes, etc...

E.g.:

type Some struct {
	Login    string        `json:"login"`
	Password string        `json:"password,omitempty"  out:"false"`
}

Field 'Password' won't be exported.

Usage.

It's pretty straightforward:

m.Use(func(c martini.Context, w http.ResponseWriter) {
	c.MapTo(encoder.JsonEncoder{}, (*encoder.Encoder)(nil))
	w.Header().Set("Content-Type", "application/json; charset=utf-8")
})

Here is a ready to use example:

package main

import (
	"github.com/codegangsta/martini-contrib/encoder"
	"github.com/codegangsta/martini"
	"log"
	"net/http"
)

type Some struct {
	Login    string `json:"login"`
	Password string `json:"password" out:"false"`
}

func main() {
	m := martini.New()
	route := martini.NewRouter()

	// map json encoder
	m.Use(func(c martini.Context, w http.ResponseWriter) {
		c.MapTo(encoder.JsonEncoder{}, (*encoder.Encoder)(nil))
		w.Header().Set("Content-Type", "application/json; charset=utf-8")
	})

	route.Get("/test", func(enc encoder.Encoder) (int, []byte) {
		result := &Some{"awesome", "hidden"}
		return http.StatusOK, encoder.Must(enc.Encode(result))
	})

	m.Action(route.Handle)

	log.Println("Waiting for connections...")

	if err := http.ListenAndServe(":8000", m); err != nil {
		log.Fatal(err)
	}
}

# Functions

Because `panic`s are caught by martini's Recovery handler, it can be used to return server-side errors (500).

# Structs

No description provided by the author

# Interfaces

An Encoder implements an encoding format of values to be sent as response to requests on the API endpoints.