java-plist-serializer
java-plist-serializer是托管在Github上的開源項目,有助于開發Java應用程序和iOS應用程序之間的通信。
- 不依賴于任何與XML相關的庫
- @PlistIgnore , @PlistAlias和命名策略可定制的輸出
- 線程安全–可以在項目中用作單例
- 可擴展–可以輕松添加其他對象的處理程序
- PlistView提供的Spring Framework集成
用法
庫的核心是PlistSerializerImpl 。 為了將對象序列化為plist,您必須創建PlistSerializerImpl的實例并調用序列化方法之一。 例如:
輸入類別:
public class Post {private String title;private Integer views = 0;private List<Comment> comments = new ArrayList<Comment>();private Author author;public Post(Author author, String title, Integer views) {this.title = title;this.views = views;this.author = author;}
}public class Comment {private String content;private String author;public Comment(String author, String content) {this.content = content;this.author = author;}
}public class Author {private String name;
}
創建這些類的對象,并plistSerializer.toXmlPlist
方法
Post post = new Post(new Author("jason bourne"), "java-plist-serializer introduction", 9);
post.addComment(new Comment("maciejwalkowiak", "first comment"));
post.addComment(new Comment("john doe", "second comment"));PlistSerializerImpl plistSerializer = new PlistSerializerImpl();
String xml = plistSerializer.toXmlPlist(post);
xml
變量將包含:
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0"><dict><key>author</key><dict><key>name</key><string>jason bourne</string></dict><key>comments</key><array><dict><key>author</key><string>maciejwalkowiak</string><key>content</key><string>first comment</string></dict><dict><key>author</key><string>john doe</string><key>content</key><string>second comment</string></dict></array><key>title</key><string>java-plist-serializer introduction</string><key>views</key><integer>9</integer></dict>
</plist>
Spring框架集成
為了返回plist作為Spring MVC控制器的響應,您可以使用擴展AbstractView的 PlistView 。
有幾種方法可以配置Spring MVC。 最容易理解的PlistView用法示例:
@Controller
public class BlogController {@RequestMapping(value = "/loadBlogPost", method = RequestMethod.GET)public ModelAndView loadBlogPost() {Post post = new Post(new Author("jason bourne"), "java-plist-serializer introduction", 9);post.addComment(new Comment("maciejwalkowiak", "first comment"));post.addComment(new Comment("john doe", "second comment"));ModelMap model = new ModelMap();model.addAttribute("RESULT", notification);return new ModelAndView(new PlistView(), model);}
}
更詳細的文檔可以在項目的github頁面上找到
結論
隨意叉,延伸。 如果您發現任何問題,請在github上報告。
參考: Java與iOS的對話:來自Java 伙伴JCG合作伙伴 Maciej Walkowiak(來自Software Development Journey博客)的Java對象與Apple plist序列化 。
翻譯自: https://www.javacodegeeks.com/2012/07/java-talking-to-ios-java-objects-to.html