org.sonar.api.utils.text.JsonWriter Maven / Gradle / Ivy
/*
* SonarQube, open source software quality management tool.
* Copyright (C) 2008-2014 SonarSource
* mailto:contact AT sonarsource DOT com
*
* SonarQube is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* SonarQube is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package org.sonar.api.utils.text;
import org.sonar.api.utils.DateUtils;
import javax.annotation.Nullable;
import java.io.Writer;
import java.util.Date;
import java.util.Map;
/**
* 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
*/
public class JsonWriter {
private final com.google.gson.stream.JsonWriter stream;
private JsonWriter(Writer writer) {
this.stream = new com.google.gson.stream.JsonWriter(writer);
this.stream.setSerializeNulls(false);
this.stream.setLenient(false);
}
// for unit testing
JsonWriter(com.google.gson.stream.JsonWriter stream) {
this.stream = stream;
}
public static JsonWriter of(Writer writer) {
return new JsonWriter(writer);
}
/**
* Begins encoding a new array. Each call to this method must be paired with
* a call to {@link #endArray}. Output is [
.
*
* @throws org.sonar.api.utils.text.WriterException on any failure
*/
public JsonWriter beginArray() {
try {
stream.beginArray();
return this;
} catch (Exception e) {
throw rethrow(e);
}
}
/**
* Ends encoding the current array. Output is ]
.
*
* @throws org.sonar.api.utils.text.WriterException on any failure
*/
public JsonWriter endArray() {
try {
stream.endArray();
return this;
} catch (Exception e) {
throw rethrow(e);
}
}
/**
* Begins encoding a new object. Each call to this method must be paired
* with a call to {@link #endObject}. Output is {
.
*
* @throws org.sonar.api.utils.text.WriterException on any failure
*/
public JsonWriter beginObject() {
try {
stream.beginObject();
return this;
} catch (Exception e) {
throw rethrow(e);
}
}
/**
* Ends encoding the current object. Output is }
.
*
* @throws org.sonar.api.utils.text.WriterException on any failure
*/
public JsonWriter endObject() {
try {
stream.endObject();
return this;
} catch (Exception e) {
throw rethrow(e);
}
}
/**
* Encodes the property name. Output is "theName":
.
*
* @throws org.sonar.api.utils.text.WriterException on any failure
*/
public JsonWriter name(String name) {
try {
stream.name(name);
return this;
} catch (Exception e) {
throw rethrow(e);
}
}
/**
* Encodes {@code value}. Output is true
or false
.
*
* @throws org.sonar.api.utils.text.WriterException on any failure
*/
public JsonWriter value(boolean value) {
try {
stream.value(value);
return this;
} catch (Exception e) {
throw rethrow(e);
}
}
/**
* @throws org.sonar.api.utils.text.WriterException on any failure
*/
public JsonWriter value(double value) {
try {
stream.value(value);
return this;
} catch (Exception e) {
throw rethrow(e);
}
}
/**
* @throws org.sonar.api.utils.text.WriterException on any failure
*/
public JsonWriter value(@Nullable String value) {
try {
stream.value(value);
return this;
} catch (Exception e) {
throw rethrow(e);
}
}
/**
* Encodes an object that can be a :
*
* - primitive types: String, Number, Boolean
* - java.util.Date: encoded as datetime (see {@link #valueDateTime(java.util.Date)}
* Map
. Method toString is called for the key.
* - Iterable
*
*
* @throws org.sonar.api.utils.text.WriterException on any failure
*/
public JsonWriter valueObject(@Nullable Object value) {
try {
if (value == null) {
stream.nullValue();
} else {
if (value instanceof String) {
stream.value((String) value);
} else if (value instanceof Number) {
stream.value((Number) value);
} else if (value instanceof Boolean) {
stream.value((Boolean) value);
} else if (value instanceof Date) {
valueDateTime((Date) value);
} else if (value instanceof Map) {
stream.beginObject();
for (Map.Entry
© 2015 - 2024 Weber Informatics LLC | Privacy Policy