将数据从一种格式转换成另一种格式总是一个很困难的任务 。将 Atom 这样丰富和强大的数据格式序列化为 JSON 这类简单的基本格式,一定会面临许多问题,本文描述的技术只是其中的一种 。
忽略所有扩展,而且不要将它们包含在 JSON 序列化中 序列化已知扩展,忽略其他所有扩展 序列化所有扩展 第一种选择显然是最简单的选择,但是限制了 JSON 表示的整体效用(并不一定是件坏事) 。第二种选择允许对已知扩展的 JSON 序列化进行优化和简化 。
- 概述JSON定义法的相关使用技巧和说明
- 详细阐述JSON结构的格式说明特点
- 使用JSON数值结构说明介绍使用
- 大致讲述JSON异步的使用说明介绍
- 读者详细说明JSON 的使用与维护工作
将 Atom 序列化为 JSON 需要考虑的最后一个问题是,如何处理扩展 。有 3 种可能的选择:但是仍然限制了序列化的效用 。第三种选择显著增加了序列化的总体复杂性,但是能够确保原始 Atom 文档的所有信息都能够通过 JSON 表示出来 。
首先讨论如何优化已知扩展的输出 。Atom Threading Extension (RFC 4685) 提供了一种方法,可以表示某个条目是另一个条目的响应 。主题扩展规范明确指定了 in-reply-to 元素的属性和含义,并表示多个 in-reply-to 元素可以出现在同一个条目中 。了解了这一点,就可以对 in-reply-to 元素的 JSON 表示进行优化,如清单 26 和 27 所示 。
- <entry> ... <thr:in-reply-to ref="tag:example.org,2007:/foo/entries/2" />
- <thr:in-reply-to ref="tag:example.org,2007:/foo/entries/3" /> ...entry>
文档转换
现在,可以获取任何 Atom 文档并将其转换为有用的JSON 序列化 。清单 30 和 31 提供了转换过程的完整演示 。原始 Atom 文档包含相对 IRI、语言上下文、扩展、多种文本和内容类型等 。使用从 Internet 上获得的任何 XML 和 JSON 转换器运行此文档,产生的序列化都不可避免地遇到数据丢失和/或可用性问题 。
- xml version="1.0" encoding="utf-8" ?> <a:feed xmlns:a="http://www.w3.org/2005/Atom"
- xmlns:thr="http://purl.org/syndication/thread/1.0"
- xmlns="http://www.w3.org/1999/xhtml"
- xmlns:foo="http://example.org/unknown-markup"
- xml:lang="en-US" xml:base="http://example.org/foo"
- dir="ltr"> <a:id>tag:example.org,2007:/fooa:id>
- <a:title>Example Feeda:title> <a:subtitle type="html">This is an example feed]]>a:subtitle>
- <a:rights type="xhtml"> <div> <p>Copyright © James M Snellp>
- div> a:rights> <a:author xmlns="http://www.w3.org/2005/Atom">
- <name>James M Snellname> <email>jasnell@example.orgemail>
- <uri>/'jasnelluri> a:author> <a:updated>2007-10-14T12:12:12Za:updated>
- <a:link rel="self" href="" /> <a:link href="/blog" /> <a:link rel="alternate"
- type="application/json" href="/blog;json" /> <a:entry xml:base="entries/1">
- <a:id>tag:example.org,2007:/foo/entries/1a:id> <a:title type="text">Entry Number Onea:title>
- <a:summary type="xhtml"> <div> <p>This is the first entry. You can read it <a href="">herea>p>
- div> a:summary> <a:rights type="html"> <p>Copyright © James M Snellp> a:rights>
- <a:updated>2007-10-14T12:12:12Za:updated> <a:link href="" />
- <a:link rel="alternate" type="application/json" href="1;json" />
- <a:link rel="replies" type="application/atom+xml"
- href="1;replies" thr:count="10" /> <a:content type="xhtml">
- <div> <p>This is the content of the first entry. It contains a picture.p>
- <img src="/images/foo.jpg" /> div> a:content>
- <thr:in-reply-to ref="tag:example.org,2007:/foo/entries/2" />
- <a:category scheme="http://example.org/categories/" term="foo"
- label="test" xml:lang="en-US" /> <a:category scheme="http://example.org/categories/" term="bar"
- label="essai" xml:lang="fr" /> <foo:a><foo:b><foo:c d="e">ffoo:c>foo:b>foo:a>
- a:entry> <a:entry xml:base="entries/2" xml:lang="fr"> <a:id>tag:example.org,2007:/foo/entries/2a:id>
- <a:title type="text">La première entréea:title> <a:summary type="xhtml"> <div>
- <p>Il sagit de la première entrée. Vous pouvez lire <a href="">est icia>p>
- div> a:summary> <a:rights type="html"> <p>Copyright © James M Snellp>
- a:rights> <a:updated>2007-10-14T12:12:11Za:updated> <a:link href="" />
- <a:link rel="alternate" type="application/json" href="2;json" /> <a:link rel="replies" type="application/atom+xml"
- href="2;replies" thr:count="10" /> <a:content type="xhtml"> <div>
- <p>Ceci est le contenu de la première entrée. Il contient une image.p>
- <img src="/images/foo.jpg" /> div> a:content>
- <a:category scheme="http://example.org/categories/" term="foo"
- label="test" xml:lang="en-US" /> <a:category scheme="http://example.org/categories/" term="bar"
- label="essai" xml:lang="fr" /> <foo:a><foo:b><foo:c d="e">ffoo:c>foo:b>foo:a> a:entry> a:feed>
本文描述的从 Atom 到 JSON 的序列化技术能够生成一个容易理解、易于使用而且能够避免丢失重要上下文数据的 JSON 表示 。清单 31. 对清单 30 中完整的 Atom 提要文档进行 JSON 序列化
使用 Abdera JSON Writer
本文介绍的技术已经作为 Apache Abdera 项目的一部分实现了 。清单 32 中的代码演示了 Abdera JSON Writer 的使用 。如果想要尝试 Atom 到 JSON 的转换,请访问 Abdera wiki,获取关于如何下载最新开发映像的信息 。
结束语
将数据从一种格式转换成另一种格式总是一个很困难的任务 。将 Atom 这样丰富和强大的数据格式序列化为 JSON 这类简单的基本格式,一定会面临许多问题 。尽管已经有许多出色的尝试,在出现标准的转换方法之前,应用程序开发人员需要处理多种质量参差不齐的不兼容序列化方法,本文描述的技术只是其中的一种 。