String-formatted date to JavaBean

Help
2008-06-20
2013-04-29
  • Raymon Wang
    Raymon Wang
    2008-06-20

    Hi, ALl:

    When converting form json-formated string to JavaBean, I met the
    problem about java.util.Date converting.

    the following is the Java file that I tested with:

        ========================================

            package json;

            import java.util.Date;

            import net.sf.json.JSONObject;

            public class Person {
                private Date birthday;

                public Date getBirthday() {
                    return birthday;
                }

                public void setBirthday(Date birthday) {
                    this.birthday = birthday;
                }

                public static Person  getInstance(String jsonVale) {
                    return
    (Person)JSONObject.toBean(JSONObject.fromObject(jsonVale),Person.class);
                }

                public static void main(String[] args) {
                    String personJson = "{birthday:\"06/28/2008
    17:00:00\"}";

                    Person p = getInstance(personJson);

                    System.out.println("Person's brithday: "+
    p.getBirthday());
                }
            }

    =====================================================
    Running the class would report the following message:

       2008-6-19 13:57:39 net.sf.json.JSONObject morphPropertyValue
       warning: Can  not transform property 'birthday' from
    java.lang.String into java.util.Date. Will register a default
    Morpher
    2008-6-19 13:57:39 net.sf.ezmorph.bean.BeanMorpher morph
    Info: Property 'java.util.Date.class' has no write method. SKIPPED.
    2008-6-19 13:57:39 net.sf.ezmorph.bean.BeanMorpher morph
    warning: Property 'java.lang.String.date' does not exist. SKIPPED.
    2008-6-19 13:57:39 net.sf.ezmorph.bean.BeanMorpher morph
    Info: Property 'java.util.Date.day' has no write method. SKIPPED.
    2008-6-19 13:57:39 net.sf.ezmorph.bean.BeanMorpher morph
    warning: Property 'java.lang.String.hours' does not exist. SKIPPED.
    2008-6-19 13:57:39 net.sf.ezmorph.bean.BeanMorpher morph
    warning: Property 'java.lang.String.minutes' does not exist.
    SKIPPED.
    2008-6-19 13:57:39 net.sf.ezmorph.bean.BeanMorpher morph
    warning: Property 'java.lang.String.month' does not exist. SKIPPED.
    2008-6-19 13:57:39 net.sf.ezmorph.bean.BeanMorpher morph
    warning: Property 'java.lang.String.seconds' does not exist.
    SKIPPED.
    2008-6-19 13:57:39 net.sf.ezmorph.bean.BeanMorpher morph
    warning: Property 'java.lang.String.time' does not exist. SKIPPED.
    2008-6-19 13:57:39 net.sf.ezmorph.bean.BeanMorpher morph
    Info: Property 'java.util.Date.timezoneOffset' has no write method.
    SKIPPED.
    2008-6-19 13:57:39 net.sf.ezmorph.bean.BeanMorpher morph
    warning: Property 'java.lang.String.year' does not exist. SKIPPED.
    Person's brithday: Thu Jun 19 13:57:39 CST 2008

    As the result shows,"Thu Jun 19 13:57:39 CST 2008 " is not equal to
    "06/28/2008 17:00:00". I know it may be related to JSONConfig, however
    I dont solve it.

    Would you please help me with that?

    Thanks in advance!

     
    • Raymon Wang
      Raymon Wang
      2008-06-20

      public static Person getInstance(String jsonVale) { 
              return  (Person)JSONObject.toBean(JSONObject.fromObject(jsonVale),Person.class); 
      }

      changed to:

      public static Person getInstance(String jsonVale) {
          JSONUtils.getMorpherRegistry().registerMorpher(new DateMorpher(new String[] {"MM/dd/yyyy HH:mm:ss"}) );
          return (Person)JSONObject.toBean(JSONObject.fromObject(jsonVale),Person.class);
      }

      i.e:
         add a sentence of "JSONUtils.getMorpherRegistry().registerMorpher(new DateMorpher(new String[] {"MM/dd/yyyy HH:mm:ss"}) );" to configure the Morpher.

      :),:) :),:) :),:),:):),:)

       
    • aalmiray
      aalmiray
      2008-06-20

      You need to register a DateMorpher with that specific format, assuming you will handle dates in that format only. If not you may supply DateMorpher with more formats.

      import net.sf.ezmoprh.object.DateMorpher

      ...
      JSONUtils.getMorpherRegistry().registerMorpher(new DateMorpher(new String[]{"dd/MM/yyyy HH:mm:ss"}));
      Person p = getInstance(personJson);
      ...