org.sonar.api.utils.text
Class JsonWriter

java.lang.Object
  extended by org.sonar.api.utils.text.JsonWriter

public class JsonWriter
extends Object

Writes JSON as a stream. This class allows plugins to not directly depend on the underlying JSON library.

How to use

   StringWriter json = new StringWriter();
   JsonWriter writer = JsonWriter.of(json);
   writer
     .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()
     .close();
 

Since:
4.2

Method Summary
 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(String name)
          Encodes the property name.
static JsonWriter of(Writer writer)
           
 JsonWriter prop(String name, boolean value)
           
 JsonWriter prop(String name, double value)
           
 JsonWriter prop(String name, long value)
           
 JsonWriter prop(String name, Number value)
          Encodes the property name and value.
 JsonWriter prop(String name, String value)
           
 JsonWriter propDate(String name, Date value)
          Encodes the property name and date value (ISO format).
 JsonWriter propDateTime(String name, Date value)
          Encodes the property name and datetime value (ISO format).
 JsonWriter value(boolean value)
          Encodes value.
 JsonWriter value(double value)
           
 JsonWriter value(long value)
           
 JsonWriter value(Number value)
           
 JsonWriter value(String value)
           
 JsonWriter valueDate(Date value)
           
 JsonWriter valueDateTime(Date value)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

of

public static JsonWriter of(Writer writer)

beginArray

public JsonWriter beginArray()
Begins encoding a new array. Each call to this method must be paired with a call to endArray(). 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 to endObject(). 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(String name)
Encodes the property name. Output is "theName":.

Throws:
WriterException - on any failure

value

public JsonWriter value(boolean value)
Encodes value. Output is true or false.

Throws:
WriterException - on any failure

value

public JsonWriter value(double value)
Throws:
WriterException - on any failure

value

public JsonWriter value(@Nullable
                        String value)
Throws:
WriterException - on any failure

valueDate

public JsonWriter valueDate(@Nullable
                            Date value)
Throws:
WriterException - on any failure

valueDateTime

public JsonWriter valueDateTime(@Nullable
                                Date value)

value

public JsonWriter value(long value)
Throws:
WriterException - on any failure

value

public JsonWriter value(@Nullable
                        Number value)
Throws:
WriterException - on any failure

prop

public JsonWriter prop(String name,
                       @Nullable
                       Number value)
Encodes the property name and value. Output is for example "theName":123.

Throws:
WriterException - on any failure

propDate

public JsonWriter propDate(String name,
                           @Nullable
                           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(String name,
                               @Nullable
                               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(String name,
                       @Nullable
                       String value)
Throws:
WriterException - on any failure

prop

public JsonWriter prop(String name,
                       boolean value)
Throws:
WriterException - on any failure

prop

public JsonWriter prop(String name,
                       long value)
Throws:
WriterException - on any failure

prop

public JsonWriter prop(String name,
                       double value)
Throws:
WriterException - on any failure

close

public void close()
Throws:
WriterException - on any failure


Copyright © 2009-2014 SonarSource. All Rights Reserved.