martini-contrib / render

Martini middleware/handler for easily rendering serialized JSON, XML, and HTML template responses.
MIT License
245 stars 57 forks source link

json encoding remove fields #36

Closed ventayol closed 10 years ago

ventayol commented 10 years ago

I'm using the render to send back json entities. It's working fine, but I would like to remove some fields from the response. For example, I don't want to send the user password back to the client.

Is there any option to do this?

cryptix commented 10 years ago

To ignore a field you just define it like this:

Field int `json:"-"`

and let encoding/json deal with the rest. It's documented here.

ventayol commented 10 years ago

Thanks @cryptix

This solved the problem I had but it's not the best option for my project.

I think I'm going to use omitempty so I can keep using binding and his required validation. So I'll set the field to nil after I query it.