Menu

JsonView documentation

Help
Alfredo
2008-09-30
2013-04-29
  • Alfredo

    Alfredo - 2008-09-30

    Andrés,

    I am trying to use the JsonView class but can't find any documentation. I guess I should override the renderMergedOutputModel(..) method, but I don't know what exactly do. Could you please provide a small code snippet?

    Thanks

     
    • Alfredo

      Alfredo - 2008-09-30

      After a while, now is working. The Spring configuration I have is as follows:

      The lms-views.xml file:
              <bean id="jsonView" class="net.sf.json.spring.web.servlet.view.JsonView" />

      The lms-servlet.xml
          <bean id="myViewResolver" class="org.springframework.web.servlet.view.XmlViewResolver">
              <property name="location" value="classpath:lms-views.xml" />
              <property name="order" value="0" />
          </bean>

          <bean id="jsonController" class="com.myapp.JsonController">
              <property name="catalogService" ref="catalogService" />
          </bean>

      The jsonController looks like this:
          protected ModelAndView handleRequestInternal(HttpServletRequest request,
                  HttpServletResponse response) throws Exception {
             
              Map model = new HashMap();
              model.put("paises", catalogService.getPaises());
             
              return new ModelAndView("jsonView", model);
          }

      Please let me know if this is the right configuration.

       
      • aalmiray

        aalmiray - 2008-09-30

        Yes, that would be the most common way. Only if you need to do some pre-processing of your data before output that you'll be concerned with the other protected methods from JsonView.

        Glad you got it working :)

        Cheers,
        Andres

         
    • Alfredo

      Alfredo - 2008-10-02

      I wonder how I could exclude nested properties, for example I have this:

      MainObject.propertya.propertyb

      What would I do in order to exclude from propertya down the hierarchy?

      I realized that is not possible to use dot notation  (as shown below) in the "excludedeProperties" bean  property.

      <bean id="jsonView" class="net.sf.json.spring.web.servlet.view.JsonView">
      <property name="excludedProperties" value="propertya.propertyb" /> 
      </bean>

      What would be the option to exclude nested properties?

       
    • aalmiray

      aalmiray - 2008-10-02

      Alfredo,

      There are several ways to do it :) The easiest one is registering a String[] of exclusions via excludedProperties, but it has a drawback, say you write something like

      <bean id="jsonView" class="net.sf.json.spring.web.servlet.view.JsonView">
      <property name="excludedProperties" value="propertyb,propertyc" /> 
      </bean>

      then for any bean property (not just MainObject) that matches that list will be excluded.

      There are other alternatives as I previously mentioned, you can find a description of them at the following link
      http://jroller.com/aalmiray/entry/json_lib_hibernate_tips_and

      You'll have to create an instance of JsonConfig, set the desired configuration props/flags and wire it up into your views.

      Cheers,
      Andres

       
      • Alfredo

        Alfredo - 2008-10-03

        Andrés,

        Thanks so much for your help, is highly appreciated. Now only one problem remains.

        I have two JsonBeanProcessors corresponding to two of my domain classes and a JsonBeanProcessorMatcher as you mention in your blog post.

        My domain hierarchy looks like this --> User.Company.Category <--
        The JsonBeanProcessors correspond to User and Company classes. The Company's JsonBeanProcessor does not include the category property, but I am still getting the Hibernate's LazyInitializationException regarding to Company's category property. Why?

        If in User's JsonBeanProcessor I comment out the code as follows, the Json string is generated fine.

        ...
        User user = (User) bean;
        return new JSONObject()
        .element("name", user.getName())
        //.element("company", user.getCompany()) <-- Commented out
        .element("mail", user.getEmail());
        ...

        Why doesn't Json-lib take into account the Company's JsonBeanProcessor?

         
        • aalmiray

          aalmiray - 2008-10-03

          Alfredo,

          Could you post the code of your JsonBeanProcessorMatcher? thanks!

          Andres

           
          • Alfredo

            Alfredo - 2008-10-03

            Yes, of course, here it is:

                private class MyJsonBeanProcessorMatcher extends JsonBeanProcessorMatcher {

                    @SuppressWarnings("unchecked")
                    @Override
                    public Object getMatch(Class target, Set set) {
                        for (Object match : set) {
                            if (((Class) match).isAssignableFrom(target)) {
                                return match;
                            }
                        }
                        return null;
                    }
                   
                }

             
          • Alfredo

            Alfredo - 2008-10-03

            Andés,

            Te problem occurs when the method JSONObject.element() is called from UserJsonBeanProcessor, see the implementation:

               public JSONObject element( String key, Object value ) {
                  return element( key, value, new JsonConfig() );
               }
              
            Is created a new JsonConfig object, with an empty beanProcessorMap.

             
    • Alfredo

      Alfredo - 2008-10-03

      Now is working fine, I haven't seen the method:

      public JSONObject element( String key, Object value, JsonConfig jsonConfig )

      Thanks a lot and congratulations for such great framework!!

       

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.