Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
Clear All
new posts

    JSON as DataSource using Gson

    I'm using Isomorphic SmartClient/SmartGWT Framework (v9.0p_2013-09-17/Pro Deployment 2013-09-17). This is browser independent.

    I'm having a little trouble using a JSON datasource. I think I'm close though - hopefully I'm just missing something easy. The way I have it working right now, as a DataSource (not a RestDataSource), I get the data but the Collections of Strings in the JSON are not parsed as arrays, but rather as a single string. If I change to a RestDataSource, and all other settings stay the same, I get the error "transformResponse(): JSON response text does not appear to be in standard response format."

    Details:

    I am outputting the json on the server side with Gson. I have a simple
    bean with some strings and 2 collections (permissions and roles). An
    example JSON output from the server looks like this:
    Code:
    {
        "username": "me",
        "permissions": [
            "login",
            "someperm"
        ],
        "roles": [
            "admin"
        ],
        "app": "appname"
    }
    and the Content-Type is set to application/json

    This is just what Gson outputs and I verified it on a bunch of places on the net that it's valid JSON and http://jsonviewer.stack.hu/ even interpreted the permissions as multiple, not a single string of "login, someperm".

    Here is the code I used to set it up in a SmartGWT widget:
    Code:
            DataSource changedDS = new DataSource();  
            changedDS.setDataFormat(DSDataFormat.JSON);  
            changedDS.setDataURL(myurl);                
            OperationBinding getOperation = new OperationBinding();
            getOperation.setOperationType(DSOperationType.FETCH);        
            changedDS.setOperationBindings(getOperation);   
            changedDS.setUseStrictJSON(false);
            DetailViewer viewer = new DetailViewer();
            viewer.setDataSource(changedDS);
            viewer.setFields(new DetailViewerField("permissions"));      
            viewer.setAutoFetchData(true);
    This works, except the detail viewer shows a single permission of "login,someperm" instead of two permissions. I also tried this on a ListGrid and it worked in the same way: it only shows one permission "login,someperm" instead of two.

    I then changed all instances of "DataSource" in the code above and replaced it with "RestDataSource" and that gave me:
    "WARN:RestDataSource:isc_RestDataSource_2:RestDataSouce
    transformResponse(): JSON response text does not appear to be in standard response format."

    Any ideas what I'm doing wrong? Feels like I'm close - maybe just missing a setting somewhere?

    Thanks,
    Brian

    #2
    Bump. Anyone? Maybe I wasn't clear and should restate the problem?

    Comment


      #3
      Ok, let's go at this from a different direction... how can I intercept the DetailViewer's rendering so that I can change the String that it tries to display from "value1, value2, value3" so that the commas are replaced with an HTML linebreak? ie... I want to get to the data when it's fetched, and intercept it and change it before it's displayed in the DetailViewer. (See attached screenshot of how it looks right now)

      I'm setting up the viewer with code like:
      viewer.setFields(new DetailViewerField("permissions"));

      then after setting up the datasource with
      viewer.setDataSource(ds);

      I just call
      viewer.fetchData()

      Where in this process can I intercept the data/comma-separated String and modify it?

      Thanks!
      Attached Files

      Comment


        #4
        Ok, I figured this out with some google-fu and this webpage:
        http://devscratch.blogspot.com/2010/02/using-detailviewer-with-collection.html

        Basically, I just had to implement my own DetailFormatter and split the values up myself and seprate them with an HTML BR.

        Some code:
        Code:
        public class CollectionDetailFormatter implements DetailFormatter {
            @Override
            public String format(Object value, Record record, DetailViewerField field) {
                if (value != null && !value.toString().trim().isEmpty()) {
                    String commaString = value.toString();
                    String[] pieces = commaString.split(",");
                    StringBuilder sb = new StringBuilder();
                    for (String one : pieces) {
                        sb.append(one.trim()).append("<br/>");
                    }
                    return sb.toString();            
                }
                else return "";
            }
        }
        then to use it I did this:
        Code:
                DetailViewerField permField = new DetailViewerField(KEY_PERM);
                permField.setDetailFormatter(new CollectionDetailFormatter());                
                viewer.setFields(permField);

        Comment

        Working...
        X