json - is there a good way to serialize only select columns of a list of BossRecords?

89 views
Skip to first unread message

Neil Pahl

unread,
Jun 17, 2014, 5:16:51 AM6/17/14
to chica...@googlegroups.com
I like the feature of serializing the BossRecord automatically when returning a json via the web controller:

{json, Data::proplist()}

Return Data as a JSON object to the client. Performs appropriate serialization if the values in Data contain a BossRecord or a list of BossRecords.


However, I usually have fields containing information I don't want to share with the client. I think it would be pretty convenient if there was a feature that would serialize only the fields which you ask for? something like:

{json, Data::proplist(), [Key::atom()]}


In the meantime, anyone have a good method of getting a list of proplists(of only chosen fields) from a list of BossRecords? or of serializing select fields of your BossRecords?

chan sisowath

unread,
Jun 17, 2014, 6:00:05 AM6/17/14
to chica...@googlegroups.com
hi,

i do like this

-module(toto, [Id, Name, Private, NotPrivate]).

-export([public/0]).

public() ->
    Attrs = [id, name, not_private],
    [{X,THIS:X()}|| X <- Attrs].


in your controller:
============

   instead of Record:attributes()
   you call Record:public()

if you have a list of record,

[X:public() || X <- ListRecord].
   

regards.
chan





--
You received this message because you are subscribed to the Google Groups "ChicagoBoss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to chicagoboss...@googlegroups.com.
Visit this group at http://groups.google.com/group/chicagoboss.
To view this discussion on the web visit https://groups.google.com/d/msgid/chicagoboss/675a6988-8762-4fac-b9a0-a204b685b52e%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

can2nac

unread,
Jun 17, 2014, 11:19:42 AM6/17/14
to chica...@googlegroups.com
nice!

Neil Pahl

unread,
Jun 17, 2014, 12:14:05 PM6/17/14
to chica...@googlegroups.com
That sure is an elegent solution - just what I was looking for. Thanks for sharing!

Neil Pahl

unread,
Jun 18, 2014, 11:56:01 PM6/18/14
to chica...@googlegroups.com
regarding mihawk's suggestion:

it seems that for the controller code, this gives error:
{json, [X:public() || X <- ListRecord]}

but something like this will serialize properly:
{json, [{X:id(),X:public()}||X<-ListRecord] }

Cheers,

On Tuesday, June 17, 2014 6:00:05 PM UTC+8, mihawk wrote:

Phang Mulianto

unread,
Nov 27, 2016, 11:32:34 AM11/27/16
to ChicagoBoss
Hi ,

I encounter this for my json api, and got problem with Date time data.  The data is {{2016,11,15},{13,50,56}}

and when returned to json, its error. can some body help?

Why the {json, [{X:id(),X:public()}||X<-ListRecord] }  give error when there is datetime data inside the list records field.

Thanks

Mulianto
Reply all
Reply to author
Forward
0 new messages