# JSONStringer

public class **JSONStringer** extends Object  
Implements `https://developers.google.com/j2objc/javadoc/android/reference/org/json/JSONObject.html#toString()` and `https://developers.google.com/j2objc/javadoc/android/reference/org/json/JSONArray.html#toString()`. Most
application developers should use those methods directly and disregard this
API. For example:

```
 JSONObject object = ...
 String json = object.toString();
```

Stringers only encode well-formed JSON strings. In particular:

- The stringer must have exactly one top-level array or object.
- Lexical scopes must be balanced: every call to `https://developers.google.com/j2objc/javadoc/android/reference/org/json/JSONStringer.html#array()` must have a matching call to `https://developers.google.com/j2objc/javadoc/android/reference/org/json/JSONStringer.html#endArray()` and every call to `https://developers.google.com/j2objc/javadoc/android/reference/org/json/JSONStringer.html#object()` must have a matching call to `https://developers.google.com/j2objc/javadoc/android/reference/org/json/JSONStringer.html#endObject()`.
- Arrays may not contain keys (property names).
- Objects must alternate keys (property names) and values.
- Values are inserted with either literal `https://developers.google.com/j2objc/javadoc/android/reference/org/json/JSONStringer.html#value(java.lang.Object)` calls, or by nesting arrays or objects.

Calls that would result in a malformed JSON string will fail with a `https://developers.google.com/j2objc/javadoc/android/reference/org/json/JSONException.html`.

This class provides no facility for pretty-printing (ie. indenting)
output. To encode indented output, use `https://developers.google.com/j2objc/javadoc/android/reference/org/json/JSONObject.html#toString(int)` or
`https://developers.google.com/j2objc/javadoc/android/reference/org/json/JSONArray.html#toString(int)`.

Some implementations of the API support at most 20 levels of nesting.
Attempts to create more than 20 levels of nesting may fail with a `https://developers.google.com/j2objc/javadoc/android/reference/org/json/JSONException.html`.

Each stringer may be used to encode a single top level value. Instances of
this class are not thread safe. Although this class is nonfinal, it was not
designed for inheritance and should not be subclassed. In particular,
self-use by overrideable methods is not specified. See *Effective Java*
Item 17, "Design and Document or inheritance or else prohibit it" for further
information.

### Public Constructor Summary

|---|---|
|   | [JSONStringer](https://developers.google.com/j2objc/javadoc/android/reference/org/json/JSONStringer.html#JSONStringer())() |

### Public Method Summary

|---|---|
| [JSONStringer](https://developers.google.com/j2objc/javadoc/android/reference/org/json/JSONStringer.html) | [array](https://developers.google.com/j2objc/javadoc/android/reference/org/json/JSONStringer.html#array())() Begins encoding a new array. |
| [JSONStringer](https://developers.google.com/j2objc/javadoc/android/reference/org/json/JSONStringer.html) | [endArray](https://developers.google.com/j2objc/javadoc/android/reference/org/json/JSONStringer.html#endArray())() Ends encoding the current array. |
| [JSONStringer](https://developers.google.com/j2objc/javadoc/android/reference/org/json/JSONStringer.html) | [endObject](https://developers.google.com/j2objc/javadoc/android/reference/org/json/JSONStringer.html#endObject())() Ends encoding the current object. |
| [JSONStringer](https://developers.google.com/j2objc/javadoc/android/reference/org/json/JSONStringer.html) | [key](https://developers.google.com/j2objc/javadoc/android/reference/org/json/JSONStringer.html#key(java.lang.String))(String name) Encodes the key (property name) to this stringer. |
| [JSONStringer](https://developers.google.com/j2objc/javadoc/android/reference/org/json/JSONStringer.html) | [object](https://developers.google.com/j2objc/javadoc/android/reference/org/json/JSONStringer.html#object())() Begins encoding a new object. |
| String | [toString](https://developers.google.com/j2objc/javadoc/android/reference/org/json/JSONStringer.html#toString())() Returns the encoded JSON string. |
| [JSONStringer](https://developers.google.com/j2objc/javadoc/android/reference/org/json/JSONStringer.html) | [value](https://developers.google.com/j2objc/javadoc/android/reference/org/json/JSONStringer.html#value(long))(long value) Encodes `value` to this stringer. |
| [JSONStringer](https://developers.google.com/j2objc/javadoc/android/reference/org/json/JSONStringer.html) | [value](https://developers.google.com/j2objc/javadoc/android/reference/org/json/JSONStringer.html#value(double))(double value) Encodes `value` to this stringer. |
| [JSONStringer](https://developers.google.com/j2objc/javadoc/android/reference/org/json/JSONStringer.html) | [value](https://developers.google.com/j2objc/javadoc/android/reference/org/json/JSONStringer.html#value(boolean))(boolean value) Encodes `value` to this stringer. |
| [JSONStringer](https://developers.google.com/j2objc/javadoc/android/reference/org/json/JSONStringer.html) | [value](https://developers.google.com/j2objc/javadoc/android/reference/org/json/JSONStringer.html#value(java.lang.Object))(Object value) Encodes `value`. |

### Inherited Method Summary

From class java.lang.Object

|---|---|
| Object | clone() |
| boolean | equals(Object arg0) |
| void | finalize() |
| final Class\<?\> | getClass() |
| int | hashCode() |
| final void | notify() |
| final void | notifyAll() |
| String | toString() |
| final void | wait(long arg0, int arg1) |
| final void | wait(long arg0) |
| final void | wait() |

## Public Constructors

#### public
**JSONStringer**
()

<br />

## Public Methods

#### public [JSONStringer](https://developers.google.com/j2objc/javadoc/android/reference/org/json/JSONStringer.html)
**array**
()

Begins encoding a new array. Each call to this method must be paired with
a call to `https://developers.google.com/j2objc/javadoc/android/reference/org/json/JSONStringer.html#endArray()`.

##### Returns

- this stringer.

##### Throws

| [JSONException](https://developers.google.com/j2objc/javadoc/android/reference/org/json/JSONException.html) |   |
|---|---|

#### public [JSONStringer](https://developers.google.com/j2objc/javadoc/android/reference/org/json/JSONStringer.html)
**endArray**
()

Ends encoding the current array.

##### Returns

- this stringer.

##### Throws

| [JSONException](https://developers.google.com/j2objc/javadoc/android/reference/org/json/JSONException.html) |   |
|---|---|

#### public [JSONStringer](https://developers.google.com/j2objc/javadoc/android/reference/org/json/JSONStringer.html)
**endObject**
()

Ends encoding the current object.

##### Returns

- this stringer.

##### Throws

| [JSONException](https://developers.google.com/j2objc/javadoc/android/reference/org/json/JSONException.html) |   |
|---|---|

#### public [JSONStringer](https://developers.google.com/j2objc/javadoc/android/reference/org/json/JSONStringer.html)
**key**
(String name)

Encodes the key (property name) to this stringer.

##### Parameters

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

##### Returns

- this stringer.

##### Throws

| [JSONException](https://developers.google.com/j2objc/javadoc/android/reference/org/json/JSONException.html) |   |
|---|---|

#### public [JSONStringer](https://developers.google.com/j2objc/javadoc/android/reference/org/json/JSONStringer.html)
**object**
()

Begins encoding a new object. Each call to this method must be paired
with a call to `https://developers.google.com/j2objc/javadoc/android/reference/org/json/JSONStringer.html#endObject()`.

##### Returns

- this stringer.

##### Throws

| [JSONException](https://developers.google.com/j2objc/javadoc/android/reference/org/json/JSONException.html) |   |
|---|---|

#### public String
**toString**
()

Returns the encoded JSON string.

If invoked with unterminated arrays or unclosed objects, this method's
return value is undefined.

**Warning:** although it contradicts the general contract
of `https://developers.google.com/j2objc/javadoc/android/reference/java/lang/Object.html#toString()`, this method returns null if the stringer
contains no data.

#### public [JSONStringer](https://developers.google.com/j2objc/javadoc/android/reference/org/json/JSONStringer.html)
**value**
(long value)

Encodes `value` to this stringer.

##### Parameters

| value |   |
|---|---|

##### Returns

- this stringer.

##### Throws

| [JSONException](https://developers.google.com/j2objc/javadoc/android/reference/org/json/JSONException.html) |   |
|---|---|

#### public [JSONStringer](https://developers.google.com/j2objc/javadoc/android/reference/org/json/JSONStringer.html)
**value**
(double value)

Encodes `value` to this stringer.

##### Parameters

| value | a finite value. May not be `https://developers.google.com/j2objc/javadoc/android/reference/java/lang/Double.html#isNaN()` or `https://developers.google.com/j2objc/javadoc/android/reference/java/lang/Double.html#isInfinite()`. |
|---|---|

##### Returns

- this stringer.

##### Throws

| [JSONException](https://developers.google.com/j2objc/javadoc/android/reference/org/json/JSONException.html) |   |
|---|---|

#### public [JSONStringer](https://developers.google.com/j2objc/javadoc/android/reference/org/json/JSONStringer.html)
**value**
(boolean value)

Encodes `value` to this stringer.

##### Parameters

| value |   |
|---|---|

##### Returns

- this stringer.

##### Throws

| [JSONException](https://developers.google.com/j2objc/javadoc/android/reference/org/json/JSONException.html) |   |
|---|---|

#### public [JSONStringer](https://developers.google.com/j2objc/javadoc/android/reference/org/json/JSONStringer.html)
**value**
(Object value)

Encodes `value`.

##### Parameters

| value | a `https://developers.google.com/j2objc/javadoc/android/reference/org/json/JSONObject.html`, `https://developers.google.com/j2objc/javadoc/android/reference/org/json/JSONArray.html`, String, Boolean, Integer, Long, Double or null. May not be `https://developers.google.com/j2objc/javadoc/android/reference/java/lang/Double.html#isNaN()` or `https://developers.google.com/j2objc/javadoc/android/reference/java/lang/Double.html#isInfinite()`. |
|---|---|

##### Returns

- this stringer.

##### Throws

| [JSONException](https://developers.google.com/j2objc/javadoc/android/reference/org/json/JSONException.html) |   |
|---|---|