Menu

Using ValueProcessor AND BeanProcessor

Jeff Lowe
2008-04-13
2013-04-29
  • Jeff Lowe

    Jeff Lowe - 2008-04-13

    I have a model that I'm trying to serialize.  The model is contained in a Map.  The model contains multiple instances of ClassA that are referenced in two different parts of the model.

    The first part is where ClassX references ClassA. In this case I want to serialize ClassX and have a JsonValueProcessor convert ClassA to a single value (the ID).

    The second part is where the model contains a List of ClassA.  In this case I want to serialize the List, and have a JsonBeanProcessor convert each instance of ClassA into a JSONObject containing the name and ID attributes.

    Is it possible to configure the serializer to handle both these scenarios within a single serialize operation?

    Thanks,
    -Jeff

     
    • aalmiray

      aalmiray - 2008-04-14

      Jeff,

      I believe JsonConfig.registerJsonValueProcessor* might expose what you are looking for. You may register a ValueBeanProcessor in 4 different ways:
      1 beanClass + propertytype = matches all properties of type propertyType on instances of that class only*
      2 beanClass + key = matches the property whose name is key on instances of that class only*
      3 propertyType = matches all properties of type propertyType of all instances regardless of bean class
      4 key = matches all properties with name key of all instances regardless of bean class

      * can be changed if you register a JsonValueProcessorMatcher

      So assuming that

      ClassX {
         ClassA classA
         List<ClassA> listOfClassA
      }

      you may register your processors like

      jsonConfig.registerJsonValueProcessor( ClassX.class, ClassA.class, JsonValueProcessorForClassAProperty )
      jsonConfig.registerJsonValueProcessor( ClassX.class, "listOfClassA", JsonValueProcessorForListOfA )

      Cheers,
      Andres

       

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.