posts - 156,  comments - 601,  trackbacks - 0
          svenson是一個用于生成和解析JSON數據格式的Java5開源類庫。它能夠將JSON數據集轉換成Java對象或相反的操作。

          下面是一些示例:

          Svenson offers you the possibility to generate JSON datasets from your java classes being a nested graph of:

          • POJOs
          • Java Beans
          • Arrays
          • Collections
          • Maps

          In the most simple case you can just do a

          import org.svenson.JSON;



          ...







          String json = JSON.defaultJSON().forValue( yourObjects );

          you can also generate JSON while ignoring certain properties:

          import org.svenson.JSON;



          ...







          String json = JSON.defaultJSON().forValue( yourObjects, Arrays.asList("foo","bar"));

          This would generate JSON for "yourObjects" while ignoring all properties named "foo" and "bar" in all beans.

          @JSONProperty

          The org.svenson.JSONProperty annotation allows you to define another JSON property name for your properties, make JSON ignore your properties completely or ignore them if they are null.

          import org.svenson.JSON;



          import org.svenson.JSONProperty;







          public class MyBean



          {



             
          private String foo,bar,baz;

             
          @JSONProperty("notFoo")
             
          public String getFoo()
             
          {
               
          return foo;
             
          }

             
          @JSONProperty(ignoreIfNull = true)
             
          public String getBar()
             
          {
                 
          return foo;
             
          }

             
          @JSONProperty(ignore = true)
             
          public String getBaz()
             
          {
                 
          return foo;
             
          }

          }

          Here the foo property would be generated as "notFoo" property in JSON, the "bar" property would be ignored if it is null, and the "baz" property would be ignored completely.

          JSONable

          If your classes implement the org.svenson.JSONable, they can provide their own JSON representation.

          import org.svenson.JSON;



          import org.svenson.JSONable;







          public class MyClass implements JSONable



          {



             
          private String member;



             
          ...



             
          public String toJSON()



             
          {



                   
          return JSON.defaultJSON().quote(this.member);



             
          }



          }

          Here MyClass would just generate one of its members as JSON string. As you can see, you can use the JSON class to help you implementing toJSON().

          JSONifier

          A JSONifier is a class that can convert another class into JSON. It needs to implement org.svenson.JSONifier.

          import org.svenson.JSON;



          import org.svenson.JSONifier;







          public class MyJSONifier implements JSONifier



          {



             
          public String toJSON(Object o)



             
          {



                   
          MyClass myClass = (MyClass)o;



                   
          return JSON.defaultJSON().quote(myClass.getMember());



             
          }



          }

          This would be a simple JSONifier that turns instances of MyClass into JSON by replacing them with one of its members. To use this JSONifier you have to register it in your own JSON instance.

          import org.svenson.JSON;



             
          ...



             
          // assume myClassInstance is an instance of MyClass



              JSON json
          = new JSON();



              json
          .registerJSONifier(MyClass.class, new MyJSONifier());







             
          String output = json.forValue( myClassInstance);

          svenson 官方網址:http://code.google.com/p/svenson/

          Good Luck!
          Yours Matthew!
          posted on 2008-12-29 21:35 x.matthew 閱讀(3122) 評論(0)  編輯  收藏 所屬分類: 最新開源動態Best Practise(JDK API)
          主站蜘蛛池模板: 马关县| 龙口市| 黄浦区| 布尔津县| 东源县| 汉沽区| 嵊州市| 武隆县| 湾仔区| 肥城市| 比如县| 凭祥市| 芦溪县| 保德县| 连江县| 东山县| 永安市| 金秀| 武定县| 上思县| 长汀县| 环江| 田林县| 巴林右旗| 石柱| 上栗县| 玉树县| 胶南市| 古浪县| 庐江县| 松潘县| 屯留县| 响水县| 六盘水市| 邢台市| 晋城| 阿拉善右旗| 沧州市| 长海县| 沧源| 中超|