You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
As I'm writing a webservice, I want my data to be available through JSON.
During the process of deciding how will I do that, I found a nice-to-have issue for tripod.
Implementing the attributes method so people can use ActiveModel::Serializers::JSON (or any othe ActiveModel Serializer) would be nice.
Here are some details:
The default json serialization of a Tripod object is not the best ever (but understandable):
It uses predicates URIs as vars names, non-multivalued data are in arrays anyways, simple values will be in an object with an "@value" variable, etc.
Steps to produce such an output are below.
Using the default respond_with(tripodObject) gives another output, which is much more detailed so even less readable (but should be way more from_json-able).
In my case, I have to re-write as_json in my model anyway, but to help someone else it might be usefull to implement the "attributes" method.
This allows models to include ActiveModel::Serializers::JSON, which offers nice options and output for the as_json method.
Please read this stack overflow answer for more details.
To produce default json output:
Simply add something like this in your controller:
defshow@p=# get your tripod object hererespond_todo |format|
format.html# default show.html.rbformat.json{renderjson: @p}endend
And then access /controller/id.json.
The detailed output can be obtained by:
defshow@p=# get your tripod object hererespond_todo |format|
format.html# show.html.rbformat.json{respond_with(@p)}endend
I hope that this is clear enough, do not hesitate to ask a question if needed.
The text was updated successfully, but these errors were encountered:
Hello,
As I'm writing a webservice, I want my data to be available through JSON.
During the process of deciding how will I do that, I found a nice-to-have issue for tripod.
Implementing the attributes method so people can use ActiveModel::Serializers::JSON (or any othe ActiveModel Serializer) would be nice.
Here are some details:
The default json serialization of a Tripod object is not the best ever (but understandable):
It uses predicates URIs as vars names, non-multivalued data are in arrays anyways, simple values will be in an object with an "@value" variable, etc.
Steps to produce such an output are below.
Using the default respond_with(tripodObject) gives another output, which is much more detailed so even less readable (but should be way more from_json-able).
In my case, I have to re-write as_json in my model anyway, but to help someone else it might be usefull to implement the "attributes" method.
This allows models to include ActiveModel::Serializers::JSON, which offers nice options and output for the as_json method.
Please read this stack overflow answer for more details.
To produce default json output:
Simply add something like this in your controller:
And then access /controller/id.json.
The detailed output can be obtained by:
I hope that this is clear enough, do not hesitate to ask a question if needed.
The text was updated successfully, but these errors were encountered: