Java与iOS对话:Java对象与Apple plist序列化

我很高兴地宣布我的第一个开源项目java-plist-serializer可以帮助您将Java(尤其是基于Spring的应用程序)与iOS应用程序集成。  
背景
我正在将Java Webapp作为后端并且客户端是iOS设备的项目。 最近,我收到了创建Web服务的任务,该服务返回plistiOS中使用的Property List数据格式)作为响应。 为什么选择plist而不选择JSON或经典XML? 如果您必须针对iOS <5.0进行开发,则没有本机类可反序列化JSON。 核心iOS库支持“属性列表”格式,因此反序列化为NSDictionary超级高效。
很少有plist –与Java有关的库,但是每个库都需要做大量的手工工作,并将Java对象逐步重写为Apple NS *类的Java等效类。 我认为没有人喜欢这种任务。 那就是为什么我开发了一个库,用于以类似于XStream XML序列化的方式将Java对象序列化为Plist。  

java-plist-serializer

java-plist-serializer是托管在Github上的开源项目,有助于开发Java应用程序和iOS应用程序之间的通信。

用法

库的核心是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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值