6 Replies Latest reply on Oct 17, 2014 4:11 PM by bigcar

    Custom Mashaller

    bigcar

      Hi, All,

       

      I created my own custom mashaller, say AbsMarshaller to handle model class "Abc", and annotated with @ClientMarshaller and @ServerMarshaller.

       

      Anywhere I need to refer to this "AbsMarshaller"? Or Errai will scan all classes in the classpath?

       

      Thanks!

      Tao

        • 1. Re: Custom Mashaller
          csa

          Hi Tao,

           

          Errai will scan for it automatically but you do need an ErraiApp.properties file in that JAR or wherever your marshaller is on the classpath.

           

          Cheers,

          Christian

          • 2. Re: Custom Mashaller
            bigcar

            Thanks a lot, Christian!

             

            I do have an empty ErraiApp.properties file on my classpath. But My custom marshaller is not taking any effect.

             

            @ClientMarshaller(StoneColor.class)

            @ServerMarshaller(StoneColor.class)

            public class StoneColorMarshaller extends AbstractNullableMarshaller<StoneColor> {

                @Override

                public StoneColor[] getEmptyArray() {

                    return new StoneColor[0];

                }

             

             

                @Override

                public StoneColor doNotNullDemarshall(final EJValue o, final MarshallingSession ctx) {

                    if (o.isObject() != null) {

                         EJValue qualifiedValue = o.isObject().get(SerializationParts.QUALIFIED_VALUE);

                         if (!qualifiedValue.isNull() && qualifiedValue.isString() != null) {

                               return StoneColor.valueOf(qualifiedValue.isString().stringValue());

                         }

                     }

                     return null;

                }

             

                @Override

                public String doNotNullMarshall(final StoneColor o, final MarshallingSession ctx) {

                    return "{\"" + SerializationParts.ENCODED_TYPE + "\":\"" + StoneColor.class.getName() + "\"," + "\""

                             + SerializationParts.OBJECT_ID + "\":\"" + o.hashCode() + "\"," + "\""

                             + SerializationParts.QUALIFIED_VALUE + "\":\"" + o + "\"}";

                }

            }

             

            Anything went wrong?

             

            Thanks !

            Tao

            • 3. Re: Custom Mashaller
              csa

              That looks OK so far. Can you share you project layout/package structure? You can also check the contents of .errai/MarshallerFactoryImpl.java. If your custom marshaller is not referenced in there then Errai failed to find it.

              • 4. Re: Custom Mashaller
                bigcar

                Hi, Christian,

                 

                Thanks a lot for your reply!

                 

                Here is what I observed:

                1. If I run the "mvn clean package", then my StoneColorMarshaller.java is recognized in MarshallerFactoryImpl.java

                2. If I right click the project in eclipse, and run as Google web application, my StoneColorMarshaller is totally ignored.

                 

                Normally I don't run mvn command, most of my development work is done in eclipse.

                 

                Btw, the reason, I wanted to come up with custom Marshaller is the result of default Marshaller is too verbose. In my application, I will marshal domain objects to browser's local storage.

                 

                Thanks again!

                Tao

                • 5. Re: Custom Mashaller
                  csa

                  Hi,

                   

                  It seems your custom marshaller is not on the classpath when starting from Eclipse. If it's in a separate project/JAR make sure to add that to the classpath in your launch config.

                   

                  Did you take a look at Errai JPA? It allows you to persist objects into the browser's local storage using a minimal JSON representation.

                   

                  Cheers,

                  Christian

                  • 6. Re: Custom Mashaller
                    bigcar

                    Thanks again, Christian!

                     

                    I only have only one project, my project layout is like this:

                    layout.png

                    You can see my tentative "StoneColorMarshaller" is in a separate package under "shared".

                     

                    As for the JPA, I did look at the JPA, the reason I did go for JPA is my domain objects are structure rich and object graph is complex with circles etc. It seems JPA might not fit.

                     

                    Btw, StoneColorMarshaller is leaf object in domain.

                     

                    Thanks!

                    Tao