详细阐述JSON 序列化的进行转换介绍


  本文标签:JSON 序列化

  将数据从一种格式转换成另一种格式总是一个很困难的任务  。将 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 所示  。

  1. <entry>  ...  <thr:in-reply-to ref="tag:example.org,2007:/foo/entries/2" />
  2.   <thr:in-reply-to ref="tag:example.org,2007:/foo/entries/3" />  ...entry> 

  文档转换

  现在,可以获取任何 Atom 文档并将其转换为有用的JSON 序列化  。清单 30 和 31 提供了转换过程的完整演示  。原始 Atom 文档包含相对 IRI、语言上下文、扩展、多种文本和内容类型等  。使用从 Internet 上获得的任何 XML 和 JSON 转换器运行此文档,产生的序列化都不可避免地遇到数据丢失和/或可用性问题  。

  1. xml version="1.0" encoding="utf-8" ?> <a:feed xmlns:a="http://www.w3.org/2005/Atom"    
  2.      xmlns:thr="http://purl.org/syndication/thread/1.0"      
  3.    xmlns="http://www.w3.org/1999/xhtml"      
  4.    xmlns:foo="http://example.org/unknown-markup"       
  5.   xml:lang="en-US"         xml:base="http://example.org/foo"     
  6.     dir="ltr">     <a:id>tag:example.org,2007:/fooa:id>  
  7.  <a:title>Example Feeda:title>   <a:subtitle type="html">This is an example feed

    ]]>
    a:subtitle> 
  8.   <a:rights type="xhtml">    <div>      <p>Copyright © James M Snellp>  
  9.   div>  a:rights>   <a:author xmlns="http://www.w3.org/2005/Atom">    
  10.  <name>James M Snellname>     <email>jasnell@example.orgemail>    
  11.  <uri>/'jasnelluri>   a:author>   <a:updated>2007-10-14T12:12:12Za:updated>  
  12.  <a:link rel="self" href="" />   <a:link href="/blog" />   <a:link rel="alternate" 
  13. type="application/json" href="/blog;json" />     <a:entry xml:base="entries/1">     
  14. <a:id>tag:example.org,2007:/foo/entries/1a:id>     <a:title type="text">Entry Number Onea:title>  
  15.    <a:summary type="xhtml">       <div>         <p>This is the first entry. You can read it <a href="">herea>p>  
  16.      div>     a:summary>     <a:rights type="html">      <p>Copyright © James M Snellp>    a:rights>  
  17.    <a:updated>2007-10-14T12:12:12Za:updated>     <a:link href="" />  
  18.    <a:link rel="alternate" type="application/json" href="1;json" />   
  19.   <a:link rel="replies" type="application/atom+xml"     
  20.   href="1;replies" thr:count="10" />     <a:content type="xhtml">    
  21.    <div>         <p>This is the content of the first entry. It contains a picture.p>     
  22.     <img src="/images/foo.jpg" />       div>     a:content>  
  23.    <thr:in-reply-to ref="tag:example.org,2007:/foo/entries/2" />     
  24. <a:category scheme="http://example.org/categories/" term="foo"      
  25.  label="test" xml:lang="en-US" />     <a:category scheme="http://example.org/categories/" term="bar"  
  26.      label="essai" xml:lang="fr" />     <foo:a><foo:b><foo:c d="e">ffoo:c>foo:b>foo:a>  
  27.  a:entry>    <a:entry xml:base="entries/2" xml:lang="fr">     <a:id>tag:example.org,2007:/foo/entries/2a:id>   
  28.   <a:title type="text">La première entréea:title>     <a:summary type="xhtml">       <div>       
  29.   <p>Il sagit de la première entrée. Vous pouvez lire         <a href="">est icia>p>     
  30.   div>     a:summary>     <a:rights type="html">      <p>Copyright © James M Snellp>  
  31.   a:rights>     <a:updated>2007-10-14T12:12:11Za:updated>     <a:link href="" />   
  32.   <a:link rel="alternate" type="application/json" href="2;json" />     <a:link rel="replies" type="application/atom+xml"     
  33.   href="2;replies" thr:count="10" />     <a:content type="xhtml">       <div>      
  34.    <p>Ceci est le contenu de la première entrée. Il contient une image.p>      
  35.    <img src="/images/foo.jpg" />       div>     a:content>    
  36. <a:category scheme="http://example.org/categories/" term="foo"  
  37.      label="test" xml:lang="en-US" />     <a:category scheme="http://example.org/categories/" term="bar" 
  38.       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 这类简单的基本格式,一定会面临许多问题  。尽管已经有许多出色的尝试,在出现标准的转换方法之前,应用程序开发人员需要处理多种质量参差不齐的不兼容序列化方法,本文描述的技术只是其中的一种  。