Package org.sonar.api.utils.text
Class JsonWriter
- java.lang.Object
-
- org.sonar.api.utils.text.JsonWriter
-
- All Implemented Interfaces:
java.lang.AutoCloseable
public class JsonWriter extends java.lang.Object implements java.lang.AutoCloseable
Writes JSON as a stream. This class allows plugins to not directly depend on the underlying JSON library.How to use
try (JsonWriter jsonWriter = JsonWriter.of(writer)) { jsonWriter .beginObject() .prop("aBoolean", true) .prop("aInt", 123) .prop("aString", "foo") .beginObject().name("aList") .beginArray() .beginObject().prop("key", "ABC").endObject() .beginObject().prop("key", "DEF").endObject() .endArray() .endObject() }
By default, null objects are not serialized. To enable
null
serialization, usesetSerializeNulls(boolean)
.By default, empty strings are serialized. To disable empty string serialization, use
setSerializeEmptys(boolean)
.JsonWriter
implementsAutoCloseable
since version 6.3. The methodclose()
closes the underlying writer.- Since:
- 4.2
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description JsonWriter
beginArray()
Begins encoding a new array.JsonWriter
beginObject()
Begins encoding a new object.void
close()
JsonWriter
endArray()
Ends encoding the current array.JsonWriter
endObject()
Ends encoding the current object.JsonWriter
name(java.lang.String name)
Encodes the property name.static JsonWriter
of(java.io.Writer writer)
JsonWriter
prop(java.lang.String name, boolean value)
JsonWriter
prop(java.lang.String name, double value)
JsonWriter
prop(java.lang.String name, long value)
JsonWriter
prop(java.lang.String name, java.lang.Number value)
Encodes the property name and value.JsonWriter
prop(java.lang.String name, java.lang.String value)
JsonWriter
propDate(java.lang.String name, java.util.Date value)
Encodes the property name and date value (ISO format).JsonWriter
propDateTime(java.lang.String name, java.util.Date value)
Encodes the property name and datetime value (ISO format).JsonWriter
setSerializeEmptys(boolean serializeEmptyStrings)
Enable/disable serialization of properties which value is an empty String.JsonWriter
setSerializeNulls(boolean b)
JsonWriter
value(boolean value)
Encodesvalue
.JsonWriter
value(double value)
JsonWriter
value(long value)
JsonWriter
value(java.lang.Number value)
JsonWriter
value(java.lang.String value)
JsonWriter
valueDate(java.util.Date value)
JsonWriter
valueDateTime(java.util.Date value)
JsonWriter
valueObject(java.lang.Object value)
Encodes an object that can be a : primitive types: String, Number, Boolean java.util.Date: encoded as datetime (seevalueDateTime(java.util.Date)
Map<Object, Object>
.JsonWriter
values(java.lang.Iterable<java.lang.String> values)
Write a list of values in an array, for example:
-
-
-
Method Detail
-
of
public static JsonWriter of(java.io.Writer writer)
-
setSerializeNulls
public JsonWriter setSerializeNulls(boolean b)
-
setSerializeEmptys
public JsonWriter setSerializeEmptys(boolean serializeEmptyStrings)
Enable/disable serialization of properties which value is an empty String.
-
beginArray
public JsonWriter beginArray()
Begins encoding a new array. Each call to this method must be paired with a call toendArray()
. Output is[
.- Throws:
WriterException
- on any failure
-
endArray
public JsonWriter endArray()
Ends encoding the current array. Output is]
.- Throws:
WriterException
- on any failure
-
beginObject
public JsonWriter beginObject()
Begins encoding a new object. Each call to this method must be paired with a call toendObject()
. Output is{
.- Throws:
WriterException
- on any failure
-
endObject
public JsonWriter endObject()
Ends encoding the current object. Output is}
.- Throws:
WriterException
- on any failure
-
name
public JsonWriter name(java.lang.String name)
Encodes the property name. Output is"theName":
.- Throws:
WriterException
- on any failure
-
value
public JsonWriter value(boolean value)
Encodesvalue
. Output istrue
orfalse
.- Throws:
WriterException
- on any failure
-
value
public JsonWriter value(double value)
- Throws:
WriterException
- on any failure
-
value
public JsonWriter value(@Nullable java.lang.String value)
- Throws:
WriterException
- on any failure
-
valueObject
public JsonWriter valueObject(@Nullable java.lang.Object value)
Encodes an object that can be a :- primitive types: String, Number, Boolean
- java.util.Date: encoded as datetime (see
valueDateTime(java.util.Date)
Map<Object, Object>
. Method toString is called for the key.- Iterable
- Throws:
WriterException
- on any failure
-
values
public JsonWriter values(java.lang.Iterable<java.lang.String> values)
Write a list of values in an array, for example:writer.beginArray().values(myValues).endArray();
- Throws:
WriterException
- on any failure
-
valueDate
public JsonWriter valueDate(@Nullable java.util.Date value)
- Throws:
WriterException
- on any failure
-
valueDateTime
public JsonWriter valueDateTime(@Nullable java.util.Date value)
-
value
public JsonWriter value(long value)
- Throws:
WriterException
- on any failure
-
value
public JsonWriter value(@Nullable java.lang.Number value)
- Throws:
WriterException
- on any failure
-
prop
public JsonWriter prop(java.lang.String name, @Nullable java.lang.Number value)
Encodes the property name and value. Output is for example"theName":123
.- Throws:
WriterException
- on any failure
-
propDate
public JsonWriter propDate(java.lang.String name, @Nullable java.util.Date value)
Encodes the property name and date value (ISO format). Output is for example"theDate":"2013-01-24"
.- Throws:
WriterException
- on any failure
-
propDateTime
public JsonWriter propDateTime(java.lang.String name, @Nullable java.util.Date value)
Encodes the property name and datetime value (ISO format). Output is for example"theDate":"2013-01-24T13:12:45+01"
.- Throws:
WriterException
- on any failure
-
prop
public JsonWriter prop(java.lang.String name, @Nullable java.lang.String value)
- Throws:
WriterException
- on any failure
-
prop
public JsonWriter prop(java.lang.String name, boolean value)
- Throws:
WriterException
- on any failure
-
prop
public JsonWriter prop(java.lang.String name, long value)
- Throws:
WriterException
- on any failure
-
prop
public JsonWriter prop(java.lang.String name, double value)
- Throws:
WriterException
- on any failure
-
close
public void close()
- Specified by:
close
in interfacejava.lang.AutoCloseable
- Throws:
WriterException
- on any failure
-
-