Enterprise Java

Java talking to iOS: Java objects to Apple plist serialization

I am happy to announce my first open source project java-plist-serializer that can help you with integrating Java and especially Spring based applications with iOS applications.  
Background
I am working on project that has Java webapp as a backend and clients are iOS devices. Recently I’ve received task to create Web Service that returns plistProperty List data format used in iOS – as a response. Why plist and not JSON or classic XML? If you have to develop for iOS < 5.0 – there are no native classes to deserialize JSON. Property List format is supported by core iOS libraries so deserialization to NSDictionary is super fast and efficient.
There are few plist – related libraries for Java but each of them requires to do a lot of manual work and rewrite Java objects step by step into Java equivalents of Apple NS* classes. I think nobody likes this kind of task. Thats why I have developed library for serializing Java objects into Plist in similar way to XStream XML 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.

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.

Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Uddhav Kambli
12 years ago

Thanks! I added binary plist support to this. https://github.com/uddhav/java-plist-serializer

Back to top button