詳細(xì)闡述JSON 序列化的進(jìn)行轉(zhuǎn)換介紹
將數(shù)據(jù)從一種格式轉(zhuǎn)換成另一種格式總是一個(gè)很困難的任務(wù)。將 Atom 這樣豐富和強(qiáng)大的數(shù)據(jù)格式序列化為 JSON 這類簡(jiǎn)單的基本格式,一定會(huì)面臨許多問題,本文描述的技術(shù)只是其中的一種。
忽略所有擴(kuò)展,而且不要將它們包含在 JSON 序列化中 序列化已知擴(kuò)展,忽略其他所有擴(kuò)展 序列化所有擴(kuò)展 第一種選擇顯然是最簡(jiǎn)單的選擇,但是限制了 JSON 表示的整體效用(并不一定是件壞事)。第二種選擇允許對(duì)已知擴(kuò)展的 JSON 序列化進(jìn)行優(yōu)化和簡(jiǎn)化。#t#
將 Atom 序列化為 JSON 需要考慮的最后一個(gè)問題是,如何處理擴(kuò)展。有 3 種可能的選擇:但是仍然限制了序列化的效用。第三種選擇顯著增加了序列化的總體復(fù)雜性,但是能夠確保原始 Atom 文檔的所有信息都能夠通過(guò) JSON 表示出來(lái)。
首先討論如何優(yōu)化已知擴(kuò)展的輸出。Atom Threading Extension (RFC 4685) 提供了一種方法,可以表示某個(gè)條目是另一個(gè)條目的響應(yīng)。主題擴(kuò)展規(guī)范明確指定了 in-reply-to 元素的屬性和含義,并表示多個(gè) in-reply-to 元素可以出現(xiàn)在同一個(gè)條目中。了解了這一點(diǎn),就可以對(duì) in-reply-to 元素的 JSON 表示進(jìn)行優(yōu)化,如清單 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>
文檔轉(zhuǎn)換
現(xiàn)在,可以獲取任何 Atom 文檔并將其轉(zhuǎn)換為有用的JSON 序列化。清單 30 和 31 提供了轉(zhuǎn)換過(guò)程的完整演示。原始 Atom 文檔包含相對(duì) IRI、語(yǔ)言上下文、擴(kuò)展、多種文本和內(nèi)容類型等。使用從 Internet 上獲得的任何 XML 和 JSON 轉(zhuǎn)換器運(yùn)行此文檔,產(chǎn)生的序列化都不可避免地遇到數(shù)據(jù)丟失和/或可用性問題。
- <?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:/foo</a:id>
- <a:title>Example Feed</a:title> <a:subtitle type="html"><![CDATA[<p>This is an example feed</p>]]></a:subtitle>
- <a:rights type="xhtml"> <div> <p>Copyright © James M Snell</p>
- </div> </a:rights> <a:author xmlns="http://www.w3.org/2005/Atom">
- <name>James M Snell</name> <email>jasnell@example.org</email>
- <uri>/~jasnell</uri> </a:author> <a:updated>2007-10-14T12:12:12Z</a: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/1</a:id> <a:title type="text">Entry Number One</a:title>
- <a:summary type="xhtml"> <div> <p>This is the first entry. You can read it <a href="">here</a></p>
- </div> </a:summary> <a:rights type="html"> <p>Copyright © James M Snell</p> </a:rights>
- <a:updated>2007-10-14T12:12:12Z</a: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">f</foo:c></foo:b></foo:a>
- </a:entry> <a:entry xml:base="entries/2" xml:lang="fr"> <a:id>tag:example.org,2007:/foo/entries/2</a:id>
- <a:title type="text">La première entrée</a:title> <a:summary type="xhtml"> <div>
- <p>Il s''agit de la première entrée. Vous pouvez lire <a href="">est ici</a></p>
- </div> </a:summary> <a:rights type="html"> <p>Copyright © James M Snell</p>
- </a:rights> <a:updated>2007-10-14T12:12:11Z</a: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">f</foo:c></foo:b></foo:a> </a:entry> </a:feed>
本文描述的從 Atom 到 JSON 的序列化技術(shù)能夠生成一個(gè)容易理解、易于使用而且能夠避免丟失重要上下文數(shù)據(jù)的 JSON 表示。清單 31. 對(duì)清單 30 中完整的 Atom 提要文檔進(jìn)行 JSON 序列化
使用 Abdera JSON Writer
本文介紹的技術(shù)已經(jīng)作為 Apache Abdera 項(xiàng)目的一部分實(shí)現(xiàn)了。清單 32 中的代碼演示了 Abdera JSON Writer 的使用。如果想要嘗試 Atom 到 JSON 的轉(zhuǎn)換,請(qǐng)?jiān)L問 Abdera wiki,獲取關(guān)于如何下載最新開發(fā)映像的信息。
結(jié)束語(yǔ)
將數(shù)據(jù)從一種格式轉(zhuǎn)換成另一種格式總是一個(gè)很困難的任務(wù)。將 Atom 這樣豐富和強(qiáng)大的數(shù)據(jù)格式序列化為 JSON 這類簡(jiǎn)單的基本格式,一定會(huì)面臨許多問題。盡管已經(jīng)有許多出色的嘗試,在出現(xiàn)標(biāo)準(zhǔn)的轉(zhuǎn)換方法之前,應(yīng)用程序開發(fā)人員需要處理多種質(zhì)量參差不齊的不兼容序列化方法,本文描述的技術(shù)只是其中的一種。