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.

Related Whitepaper:

Functional Programming in Java: Harnessing the Power of Java 8 Lambda Expressions

Get ready to program in a whole new way!

Functional Programming in Java will help you quickly get on top of the new, essential Java 8 language features and the functional style that will change and improve your code. This short, targeted book will help you make the paradigm shift from the old imperative way to a less error-prone, more elegant, and concise coding style that’s also a breeze to parallelize. You’ll explore the syntax and semantics of lambda expressions, method and constructor references, and functional interfaces. You’ll design and write applications better using the new standards in Java 8 and the JDK.

Get it Now!  

One Response to "Java talking to iOS: Java objects to Apple plist serialization"

Leave a Reply


three − 3 =



Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use | Privacy Policy
All trademarks and registered trademarks appearing on Java Code Geeks are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries.
Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.
Do you want to know how to develop your skillset and become a ...
Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you two of our best selling eBooks for FREE!

Get ready to Rock!
You can download the complementary eBooks using the links below:
Close