| AndBase开发框架 1.6

- 首页
- 相关页面
- 命名空间
- 类
- 文件

- 类列表
- 类索引
- 类继承关系
- 类成员

全部 类 命名空间 文件 函数 变量 枚举值 页
Public 成员函数 | 包函数 | Private 成员函数 | Private 属性 | 所有成员列表
com.google.gson.stream.JsonWriter类 参考
类 com.google.gson.stream.JsonWriter 继承关系图:

## Public 成员函数
| JsonWriter (Writer out)
final void | setIndent (String indent)
final void | setLenient (boolean lenient)
boolean | isLenient ()
final void | setHtmlSafe (boolean htmlSafe)
final boolean | isHtmlSafe ()
final void | setSerializeNulls (boolean serializeNulls)
final boolean | getSerializeNulls ()
JsonWriter | beginArray () throws IOException
JsonWriter | endArray () throws IOException
JsonWriter | beginObject () throws IOException
JsonWriter | endObject () throws IOException
JsonWriter | name (String name) throws IOException
JsonWriter | value (String value) throws IOException
JsonWriter | nullValue () throws IOException
JsonWriter | value (boolean value) throws IOException
JsonWriter | value (double value) throws IOException
JsonWriter | value (long value) throws IOException
JsonWriter | value (Number value) throws IOException
void | flush () throws IOException
void | close () throws IOException

## 包函数
| [instance initializer]

## Private 成员函数
JsonWriter | open (JsonScope empty, String openBracket) throws IOException
JsonWriter | close (JsonScope empty, JsonScope nonempty, String closeBracket) throws IOException
JsonScope | peek ()
void | replaceTop (JsonScope topOfStack)
void | writeDeferredName () throws IOException
void | string (String value) throws IOException
void | newline () throws IOException
void | beforeName () throws IOException
void | beforeValue (boolean root) throws IOException

## Private 属性
final Writer | out
final List< JsonScope > | stack = new ArrayList<JsonScope>()
String | indent
String | separator = ":"
boolean | lenient
boolean | htmlSafe
String | deferredName
boolean | serializeNulls = true

## 详细描述

Writes a JSON (RFC 4627) encoded value to a stream, one token at a time. The stream includes both literal values (strings, numbers, booleans and nulls) as well as the begin and end delimiters of objects and arrays.

### Encoding JSON

To encode your data as JSON, create a new

JsonWriter

. Each JSON document must contain one top-level array or object. Call methods on the writer as you walk the structure's contents, nesting arrays and objects as necessary:

- To write arrays, first call beginArray(). Write each of the array's elements with the appropriate value methods or by nesting other arrays and objects. Finally close the array using endArray().
- To write objects, first call beginObject(). Write each of the object's properties by alternating calls to name with the property's value. Write property values with the appropriate value method or by nesting other objects or arrays. Finally close the object using endObject().

### Example

Suppose we'd like to encode a stream of messages such as the following:

    [
      {
        "id": 912345678901,
        "text": "How do I stream JSON in Java?",
        "geo": null,
        "user": {
          "name": "json_newb",
          "followers_count": 41
         }
      },
      {
        "id": 912345678902,
        "text": "@json_newb just use JsonWriter!",
        "geo": [50.454722, -104.606667],
        "user": {
          "name": "jesse",
          "followers_count": 2
        }
      }
    ]

This code encodes the above structure:

    public void writeJsonStream(OutputStream out, List<Message> messages) throws IOException {
      JsonWriter writer = new JsonWriter(new OutputStreamWriter(out, "UTF-8"));
      writer.setIndentSpaces(4);
      writeMessagesArray(writer, messages);
      writer.close();
    }

    public void writeMessagesArray(JsonWriter writer, List<Message> messages) throws IOException {
      writer.beginArray();
      for (Message message : messages) {
        writeMessage(writer, message);
      }
      writer.endArray();
    }

    public void writeMessage(JsonWriter writer, Message message) throws IOException {
      writer.beginObject();
      writer.name("id").value(message.getId());
      writer.name("text").value(message.getText());
      if (message.getGeo() != null) {
        writer.name("geo");
        writeDoublesArray(writer, message.getGeo());
      } else {
        writer.name("geo").nullValue();
      }
      writer.name("user");
      writeUser(writer, message.getUser());
      writer.endObject();
    }

    public void writeUser(JsonWriter writer, User user) throws IOException {
      writer.beginObject();
      writer.name("name").value(user.getName());
      writer.name("followers_count").value(user.getFollowersCount());
      writer.endObject();
    }

    public void writeDoublesArray(JsonWriter writer, List<Double> doubles) throws IOException {
      writer.beginArray();
      for (Double value : doubles) {
        writer.value(value);
      }
      writer.endArray();
    }

Each

JsonWriter

may be used to write a single JSON stream. Instances of this class are not thread safe. Calls that would result in a malformed JSON string will fail with an IllegalStateException.

作者
  Jesse Wilson

自从
  1.6

## 构造及析构函数说明

com.google.gson.stream.JsonWriter.JsonWriter | ( | Writer | out | ) |
| inline

Creates a new instance that writes a JSON-encoded stream to

out

. For best performance, ensure Writer is buffered; wrapping in BufferedWriter if necessary.

## 成员函数说明

com.google.gson.stream.JsonWriter.[instance initializer] | ( |  | ) |
| inline package

void com.google.gson.stream.JsonWriter.beforeName | ( |  | ) | throws IOException
| inline private

Inserts any necessary separators and whitespace before a name. Also adjusts the stack to expect the name's value.

void com.google.gson.stream.JsonWriter.beforeValue | ( | boolean | root | ) | throws IOException
| inline private

Inserts any necessary separators and whitespace before a literal value, inline array, or inline object. Also adjusts the stack to expect either a closing bracket or another element.

参数

  root | true if the value is a new array or object, the two values permitted as top-level elements.

JsonWriter com.google.gson.stream.JsonWriter.beginArray | ( |  | ) | throws IOException
| inline

Begins encoding a new array. Each call to this method must be paired with a call to endArray.

返回
  this writer.

JsonWriter com.google.gson.stream.JsonWriter.beginObject | ( |  | ) | throws IOException
| inline

Begins encoding a new object. Each call to this method must be paired with a call to endObject.

返回
  this writer.

JsonWriter com.google.gson.stream.JsonWriter.close | ( | JsonScope | empty,
|  | JsonScope | nonempty,
|  | String | closeBracket
| ) |  | throws IOException
| inline private

Closes the current scope by appending any necessary whitespace and the given bracket.

void com.google.gson.stream.JsonWriter.close | ( |  | ) | throws IOException
| inline

Flushes and closes this writer and the underlying Writer.

异常

  IOException | if the JSON document is incomplete.

JsonWriter com.google.gson.stream.JsonWriter.endArray | ( |  | ) | throws IOException
| inline

Ends encoding the current array.

返回
  this writer.

JsonWriter com.google.gson.stream.JsonWriter.endObject | ( |  | ) | throws IOException
| inline

Ends encoding the current object.

返回
  this writer.

void com.google.gson.stream.JsonWriter.flush | ( |  | ) | throws IOException
| inline

Ensures all buffered data is written to the underlying Writer and flushes that writer.

final boolean com.google.gson.stream.JsonWriter.getSerializeNulls | ( |  | ) |
| inline

Returns true if object members are serialized when their value is null. This has no impact on array elements. The default is true.

final boolean com.google.gson.stream.JsonWriter.isHtmlSafe | ( |  | ) |
| inline

Returns true if this writer writes JSON that's safe for inclusion in HTML and XML documents.

boolean com.google.gson.stream.JsonWriter.isLenient | ( |  | ) |
| inline

Returns true if this writer has relaxed syntax rules.

JsonWriter com.google.gson.stream.JsonWriter.name | ( | String | name | ) | throws IOException
| inline

Encodes the property name.

参数

  name | the name of the forthcoming value. May not be null.

返回
  this writer.

void com.google.gson.stream.JsonWriter.newline | ( |  | ) | throws IOException
| inline private

JsonWriter com.google.gson.stream.JsonWriter.nullValue | ( |  | ) | throws IOException
| inline

Encodes

null

.

返回
  this writer.

JsonWriter com.google.gson.stream.JsonWriter.open | ( | JsonScope | empty,
|  | String | openBracket
| ) |  | throws IOException
| inline private

Enters a new scope by appending any necessary whitespace and the given bracket.

JsonScope com.google.gson.stream.JsonWriter.peek | ( |  | ) |
| inline private

Returns the value on the top of the stack.

void com.google.gson.stream.JsonWriter.replaceTop | ( | JsonScope | topOfStack | ) |
| inline private

Replace the value on the top of the stack with the given value.

final void com.google.gson.stream.JsonWriter.setHtmlSafe | ( | boolean | htmlSafe | ) |
| inline

Configure this writer to emit JSON that's safe for direct inclusion in HTML and XML documents. This escapes the HTML characters

<

,

>

,

&

and

=

before writing them to the stream. Without this setting, your XML/HTML encoder should replace these characters with the corresponding escape sequences.

final void com.google.gson.stream.JsonWriter.setIndent | ( | String | indent | ) |
| inline

Sets the indentation string to be repeated for each level of indentation in the encoded document. If

indent.isEmpty()

the encoded document will be compact. Otherwise the encoded document will be more human-readable.

参数

  indent | a string containing only whitespace.

final void com.google.gson.stream.JsonWriter.setLenient | ( | boolean | lenient | ) |
| inline

Configure this writer to relax its syntax rules. By default, this writer only emits well-formed JSON as specified by RFC 4627. Setting the writer to lenient permits the following:

- Top-level values of any type. With strict writing, the top-level value must be an object or an array.
- Numbers may be NaNs or infinities.

final void com.google.gson.stream.JsonWriter.setSerializeNulls | ( | boolean | serializeNulls | ) |
| inline

Sets whether object members are serialized when their value is null. This has no impact on array elements. The default is true.

void com.google.gson.stream.JsonWriter.string | ( | String | value | ) | throws IOException
| inline private

JsonWriter com.google.gson.stream.JsonWriter.value | ( | String | value | ) | throws IOException
| inline

Encodes

value

.

参数

  value | the literal string value, or null to encode a null literal.

返回
  this writer.

JsonWriter com.google.gson.stream.JsonWriter.value | ( | boolean | value | ) | throws IOException
| inline

Encodes

value

.

返回
  this writer.

JsonWriter com.google.gson.stream.JsonWriter.value | ( | double | value | ) | throws IOException
| inline

Encodes

value

.

参数

  value | a finite value. May not be NaNs or infinities.

返回
  this writer.

JsonWriter com.google.gson.stream.JsonWriter.value | ( | long | value | ) | throws IOException
| inline

Encodes

value

.

返回
  this writer.

JsonWriter com.google.gson.stream.JsonWriter.value | ( | Number | value | ) | throws IOException
| inline

Encodes

value

.

参数

  value | a finite value. May not be NaNs or infinities.

返回
  this writer.

void com.google.gson.stream.JsonWriter.writeDeferredName | ( |  | ) | throws IOException
| inline private

## 类成员变量说明

String com.google.gson.stream.JsonWriter.deferredName
| private

boolean com.google.gson.stream.JsonWriter.htmlSafe
| private

String com.google.gson.stream.JsonWriter.indent
| private

A string containing a full set of spaces for a single level of indentation, or null for no pretty printing.

boolean com.google.gson.stream.JsonWriter.lenient
| private

final Writer com.google.gson.stream.JsonWriter.out
| private

The output data, containing at most one top-level array or object.

String com.google.gson.stream.JsonWriter.separator = ":"
| private

The name/value separator; either ":" or ": ".

boolean com.google.gson.stream.JsonWriter.serializeNulls = true
| private

final List<JsonScope> com.google.gson.stream.JsonWriter.stack = new ArrayList<JsonScope>()
| private

该类的文档由以下文件生成:

- src/com/google/gson/stream/JsonWriter.java