Java talking to iOS: Java objects to Apple plist serialization
java-plist-serializer
java-plist-serializer is an open source project hosted on Github that helps to develop communication between Java application and iOS application.
- does not depend on any XML related library
- customizable output thanks to @PlistIgnore, @PlistAlias and Naming Strategies
- thread safe – can be used as a singleton in project
- extensible – handlers for additional objects can be easily added
- Spring Framework integration provided by PlistView
Usage
The heart of library is PlistSerializerImpl. In order to serialize obejcts to plist you have to create instance of PlistSerializerImpl and call one of serialization methods. For example:
Input classes:
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; }
Objects of those classes are created and plistSerializer.toXmlPlist
method is called
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
variable will contain:
<?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 Framework Integration
In order to return plist as a response of Spring MVC controller you can use PlistView that extends AbstractView.
There are several ways to configure Spring MVC. The easiest to understand example of usage of 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); } }
More detailed documentation can be found on project’s github page
Conclusion
Feel free to fork, extend. If you will find any issue please report them on github.
Reference: Java talking to iOS: Java objects to Apple plist serialization from our JCG partner Maciej Walkowiak at the Software Development Journey blog.
Thanks! I added binary plist support to this. https://github.com/uddhav/java-plist-serializer