Chinaunix首页 | 论坛 | 博客
  • 博客访问: 95213
  • 博文数量: 11
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 191
  • 用 户 组: 普通用户
  • 注册时间: 2013-10-18 16:33
个人简介

有二事焉,恒然于心,敬之畏之,日省日甚!外乎者如璀璨星穹,内在者犹道德律令。

文章分类

全部博文(11)

文章存档

2017年(7)

2014年(2)

2013年(2)

我的朋友

分类: Java

2013-12-14 01:56:15

javax.json.stream

Interface JsonParser


All Superinterfaces:
        AutoCloseableCloseable


public interface JsonParser extends 

Provides forward, read-only access to JSON data in a streaming way. This is the most efficient way for reading JSON data. The class  contains methods to create parsers from input sources ( and ).
提供单向,只读的流的方式访问JSON数据。这是一种有效的读取JSON数据的方法。Json类包含了从输入源头(例如InputStream和Reader)创建parsers(解析器)的一些方法。

The following example demonstrates how to create a parser from a string that contains an empty JSON array:
如下的示例展示了如何从包含了一个空JSON数组的字符串创建一个解析器:


 JsonParser parser = Json.createParser(new StringReader("[]"));


The class  also contains methods to create JsonParserinstances.  is preferred when creating multiple parser instances. A sample usage is shown in the following example:
JsonParserFactory这个类也包含了一些创建JsonParser实例的方法。当创建多个parser(解析器)实例的时候JsonParserFactory更加优先的被使用。下面是一个简单的用法示例:


 JsonParserFactory factory = Json.createParserFactory();
 JsonParser parser1 = factory.createParser(...);
 JsonParser parser2 = factory.createParser(...);


JsonParser
 parses JSON using the pull parsing programming model. In this model the client code controls the thread and calls the method next() to advance the parser to the next state after processing each element. The parser can generate the following events: START_OBJECTEND_OBJECTSTART_ARRAYEND_ARRAYKEY_NAME,VALUE_STRINGVALUE_NUMBERVALUE_TRUEVALUE_FALSE, and VALUE_NULL.
JsonParser使用拉取式解析程序模型。在这种模型里,客户端代码控制进程并且通过调用next()方法来推进解析器到下一个状态在处理完当前元素之后。解析器能够产生下列事件:START_OBJECT, END_OBJECT, START_ARRAY, END_ARRAY, KEY_NAME, VALUE_STRING, VALUE_NUMBER, VALUE_TRUE, VALUE_FALSE, and VALUE_NULL.


For example, for an empty JSON object ({ }), the parser generates the eventSTART_OBJECT with the first call to the method next() and the event END_OBJECTwith the second call to the method next(). The following code demonstrates how to access these events:
例如,对于一个空JSON object({}),当首次调用next()方法时解析器将会产生事件START_OBJECT,然后第二次调用next()方法时会产生事件END_OBJECT。下面的代码展示了如何访问这些事件:


 Event event = parser.next();  // START_OBJECT
 event = parser.next();        // END_OBJECT

 {
    "firstName": "John", "lastName": "Smith", "age": 25,
    "phoneNumber": [
        { "type": "home", "number": "212 555-1234" },
        { "type": "fax", "number": "646 555-4567" }
    ]
 }


calls to the method next() result in parse events at the specified locations below(marked in bold):

调用next()方法所返回的解析器事件在下面的指定位置上(采用粗体标记)


{START_OBJECT

    “firstName”KEY_NAME : ”John”VALUE_STRING, “lastName”KEY_NAME : ”Smith”VALUE_STRING, “age”KEY_NAME : 25VALUE_NUMBER,

    “phoneNumber”KEY_NAME : [START_ARRAY

        {START_OBJECT “type”KEY_NAME : ”home”VALUE_STRING, “number”KEY_NAME : ”212 555-1234”VALUE_STRING }END_OBJECT,

        {START_OBJECT “type”KEY_NAME : ”fax”VALUE_STRING, “number”KEY_NAME : ”646 555-4567”VALUE_STRING }END_OBJECT

    ]END_ARRAY

}END_OBJECT


The methods next() and hasNext() enable iteration over parser events to process JSON data. JsonParser provides get methods to obtain the value at the current state of the parser. For example, the following code shows how to obtain the value “John” from the JSON above:

next()方法和hasNext()能够通过迭代整个解析器的方式去处理JSON数据。JsonParser类提供一些get方法去获取解析器当前状态的值。
例如,如下的代码展示了如何从上面的JSON中获取值“John”


 Event event = parser.next();  // START_OBJECT
 event = parser.next();        // KEY_NAME



    • Nested Class Summary(内嵌类概述)

      Modifier and Type(修饰符和类型 Interface and Description(接口及其描述。译者注:不明白这里为什么要用Interface
      static class
      An event from JsonParser.(一个来自于JsonParser的事件。译者注:这里的意思应该是说Event表示事件,是定义在JsonParser中的,其实Event是定义在JsonParser中的一个枚举型变量,所以这里称作为内嵌。
    • Method Summary(方法概述)

      Modifier and Type Method and Description方法及其描述
      void ()
      Closes this parser and frees any resources associated with the parser.(关闭该解析器并释放给该解析器分配的一切资源。译者注:这是一个对象级方法,当调用的时候就会作用在调用它的那个JsonParser对象上面。该方法是继承自java.io.Closeable接口的
      ()
      Returns a JSON number as a BigDecimal.(把一个JSON数字当做BigDecimal类型返回。
      int ()
      Returns a JSON number as an integer.(把一个JSON数字当做int类型返回。
      ()
      Return the location that corresponds to the parser's current state in the JSON input source.()
      long ()
      Returns a JSON number as a long.(把一个JSON数字当做long类型返回。
      ()
      Returns a String for the name in a name/value pair, for a string value or a number value.
      boolean ()
      Returns true if there are more parsing states.(如果还有更多的解析器事件,则返回true
      boolean ()
      Returns true if the JSON number at the current parser state is a integral number.(如果在当前解析器事件下的JSON数字是一个整数,则返回true。译者注:这个方法应该是用于判断当前事件下的json值是否是数字。JSON number的意思应该是说当一个值字面上看是数字的时候。
      ()
      Returns the event for the next parsing state.(返回下一个解析器事件状态。

  • Method Detail

    • hasNext

      boolean hasNext()
      Returns true if there are more parsing states. This method returns false if 
      the parser reaches the end of the JSON text.(如果还有更多的解析器事件的话就返回true。当解析器到达JSON文本最后时本方法将返回false。)
      Returns:
          true if there are more parsing states.(如果还有更多的解析器事件的话就返回true。
      Throws:
          JsonException - if an i/o error occurs (IOException would be cause of JsonException)(当一个i/o错误发生的时候(IOException可能会是引
          发JsonException的原因)

          JsonParsingException - if the parser encounters invalid JSON when advancing to next state.(解析器前进到下一个状态遇到非法的JSON的
          时候

    • next

      JsonParser.Event next()
      Returns the event for the parsing state
      .(返回解析器事件。译者注:解析器事件是一个枚举值
      Returns:
          Returns the event for the next parsing state.(返回下一个解析器事件
      Throws:
          JsonException - if an i/o error occurs (IOException would be cause of JsonException)
          (当一个i/o错误发生的时候(IOException可能会是引发JsonException的原因

          JsonParsingException - if the parser encounters invalid JSON when advancing to next state.
          解析器前进到下一个状态遇到非法的JSON的时候
          NoSuchElementException - if there are no more parsing states.
          (如果没有更多的解析器事件。译者注:当hasNext()方法返回false后,再次调用next()方法将会抛出本异常
    • getString

      String getString()
      Returns a String for the name in a name/value pair, for a string value or a number value. This method should only be called when the parser state is JsonParser.Event.KEY_NAME, JsonParser.Event.VALUE_STRING, or JsonParser.Event.VALUE_NUMBER.(返回名字/值对的名字,或者字符串值或者一个数字值。本方法应当仅在解析器事件KEY_NAME, VALUE_STRING或VALUE_NUMBER发生时被调用。
      Returns:
          a name then the parser state is JsonParser.Event.KEY_NAME a string value when the parser state is JsonParser.Event.VALUE_STRING
          a number value when the parser state is JsonParser.Event.VALUE_NUMBER.(当解析器事件为KEY_NAME是一个名字,当解析器事件为VALUE_STRING
          一个字符串,
      当解析器事件为VALUE_NUMBER是一个数字。译者注:这里的名字、字符串或数字都是以字符串的形式返回的

      Throws:
          IllegalStateException - when the parser state is not KEY_NAME, VALUE_STRING, or VALUE_NUMBER.
          (当解析器事件不是KEY_NAME, VALUE_STRING, 或VALUE_NUMBER时调用本方法将会抛出
    • isIntegralNumber

      boolean isIntegralNumber()
      Returns true if the JSON number at the current parser state is a integral number. A BigDecimal may be used to store the value internally and this method semantics are defined using its scale(). If the scale is zero, t
      hen it is considered integral type. This integral type information can be used to invoke an appropriate accessor method to obtain a numeric value as in the following example:(如果当前解析器状态,将会返回true。这个数值将会被存储在一个BigDecimal实例内部,并且本方法的语义将会根据它(译者注:应该是BigDecimal实例的scale方法而定义, 如果小数位精度等于0(译者注:scale的意思是精确到小数点后的位数),那么它将会表述为数型。在下面的例子中,可以根据这个整型的信息而调用一个适当的访问方法来获取数值。
          JsonParser parser = ...
          if (parser.isIntegralNumber()) {
              parser.getInt();    // or other methods to get integral value
          } else {
              parser.getBigDecimal();
          }
      (译者注:对于上述代码,如果有疑惑的读者可以采用类似这样的JSON数据进行测试:{"very_big_integer":"99...9(可以尽量的多)","very_big_double":"99...9(可以尽量的多,记得加小数点)"},则对于very_big_integer的话isIntegralNumber会返回true,而对于very_big_double则会返回false。)

      Returns:
          true if the number is a integral number, otherwise false.(如果数值是整数的话则返回true,否则返回false
      Throws:
          IllegalStateException - when the parser state is not VALUE_NUMBER.(当解析器事件不是VALUE_NUMBER时调用本方法将会抛出
    • getlong

      int getLong()
      Returns a JSON number as a long. The returned value is equal to new BigDecimal(getString()).longValue(). Note that this conversion can lose information about the overall magnitude and precision of the number value as well as return a result with the opposite sign. This method is only called when the parser state isJsonParser.Event.VALUE_NUMBER.(将一个JSON数值做为long类型返回。返回的值等效于new BigDecimal(getString()).longValue()。注意这样的转换操作将会丢失数值的所有精度信息好像返回了一个带有反转符号的结果。(译者注:不太明白这个opposite sign是什么意思)。这个方法只能在解析器事件是VALUE_NUMBER的时候被调用。
      Returns:
          a long for a JSON number一个long类型的JSON数值
      Throws:
          IllegalStateException - when the parser state is not VALUE_NUMBER.当解析器事件不是VALUE_NUMBER时调用本方法将会抛出
      See Also:
          BigDecimal.longValue()(另见BigDecimal.longValue()
    • getBigDecimal

      BigDecimal getBigDecimal()
      Returns a JSON number as a BigDecimal. The BigDecimal is created using new BigDecimal(getString()). This method should only called when the parser state is JsonParser.Event.VALUE_NUMBER.(将一个JSON数值做为BigDecimal类型返回。使用new BigDecimal(getString())来创建这个要返回的BigDecimal实例。这个方法只能在解析器事件是VALUE_NUMBER的时候被调用。
      Returns:
          a BigDecimal for a JSON number(一个BigDecimal类型的JSON数值
      Throws:
          IllegalStateException - when the parser state is not VALUE_NUMBER.当解析器事件不是VALUE_NUMBER时调用本方法将会抛出
    • getLocation

      JsonLocation getLocation()
      Return the location that corresponds to the parser's current state in the JSON input source. The location information is only valid in the current parser state (or until the parser is advanced to a next state).(返回当前的JSON输入源上的解析器的对应事件状态值。location信息只对当前的解析器事件是合法的(或者直到解析器前进进入下一个事件状态
      Returns:
          a non-null location corresponding to the current parser state in JSON input source(一个非null的JsonLocation对象,对应于当前JSON输入源上的解析器事件值
    • close

      void close()
      Closes this parser and frees any resources associated with the parser. This method closes the underlying input source.(关闭当前这个解析器并释放分配给它的所有资源。这个方法将会关闭底层的输入源。(译者注:构建解析器的时候会用到一个Stream对象,这个时候就会关闭它了。
      Specified by:
          close in interface AutoCloseable(定义在AutoCloseable接口中的close方法
      Specified by:
          close in interface Closeable定义在Closeable接口中的close方法

      Throws:
          JsonException - if an i/o error occurs (IOException would be cause of JsonException)
          当一个i/o错误发生的时候(IOException可能会是引发JsonException的原因

囿于译者水平有限,文中难免存在不妥之处,敬请谅解!!

阅读(3756) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~