<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    風(fēng)人園

    弱水三千,只取一瓢,便能解渴;佛法無邊,奉行一法,便能得益。
    隨筆 - 99, 文章 - 181, 評論 - 56, 引用 - 0
    數(shù)據(jù)加載中……

    svenson, 一個用于生成和解析JSON數(shù)據(jù)格式的Java5開源類庫 (ZT)


    轉(zhuǎn)載自 http://m.tkk7.com/xmatthew/archive/2008/12/29/248998.html

    svenson是一個用于生成和解析JSON數(shù)據(jù)格式的Java5開源類庫。它能夠?qū)SON數(shù)據(jù)集轉(zhuǎn)換成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 官方網(wǎng)址:http://code.google.com/p/svenson/

    posted on 2009-04-15 16:48 風(fēng)人園 閱讀(762) 評論(0)  編輯  收藏 所屬分類: Open Source

    主站蜘蛛池模板: 亚洲 综合 国产 欧洲 丝袜| 亚洲综合在线观看视频| 国产永久免费高清在线| 亚洲自偷自拍另类图片二区| 四虎在线视频免费观看| 中国极品美軳免费观看| 久久亚洲最大成人网4438| 亚洲国产精品成人| 18禁黄网站禁片免费观看不卡| 337P日本欧洲亚洲大胆艺术图| 亚洲精品少妇30p| 免费毛片在线播放| 97视频免费观看2区| 青青视频免费在线| 91亚洲视频在线观看| 国产亚洲一区区二区在线| 男人的好看免费观看在线视频| a色毛片免费视频| 羞羞的视频在线免费观看| 亚洲国产成人久久99精品| 不卡精品国产_亚洲人成在线| 国产精品无码免费播放| 久久久久成人片免费观看蜜芽 | 免费观看一区二区三区| 亚洲AV性色在线观看| 精品亚洲国产成AV人片传媒| 亚洲成人高清在线| 免费高清在线影片一区| 24小时在线免费视频| a级毛片在线免费| 特级毛片A级毛片免费播放| 亚洲资源最新版在线观看| 图图资源网亚洲综合网站| 亚洲视频在线精品| 日韩一区二区免费视频| 亚洲人成电影网站免费| 久久久精品2019免费观看| 三上悠亚电影全集免费| 一级午夜免费视频| 黄页网站在线免费观看| 亚洲成aⅴ人片久青草影院按摩|