org.sonar.api.utils.text.JsonWriter Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of sonar-plugin-api Show documentation
Show all versions of sonar-plugin-api Show documentation
Plugin API for SonarQube, SonarCloud and SonarLint
/*
* Sonar Plugin API
* Copyright (C) 2009-2022 SonarSource SA
* mailto:info AT sonarsource DOT com
*
* This program 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.
*
* This program 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 java.io.IOException;
import java.io.Writer;
import java.util.Date;
import java.util.Map;
import javax.annotation.Nullable;
import org.sonar.api.utils.DateUtils;
/**
* 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 {@code null} serialization,
* use {@link #setSerializeNulls(boolean)}.
*
*
* By default, empty strings are serialized. To disable empty string serialization,
* use {@link #setSerializeEmptys(boolean)}.
*
*
* {@link JsonWriter} implements {@link AutoCloseable} since version 6.3. The
* method {@link #close()} closes the underlying writer.
*
*
* @since 4.2
* @deprecated since 8.3 this utility class will be moved out of the API. Use your own JSON dependency
* if you need to write JSON from your plugin
*/
@Deprecated
public class JsonWriter implements AutoCloseable {
private final com.google.gson.stream.JsonWriter stream;
private boolean serializeEmptyStrings;
private JsonWriter(Writer writer) {
this.stream = new com.google.gson.stream.JsonWriter(writer);
this.stream.setSerializeNulls(false);
this.stream.setLenient(false);
this.stream.setHtmlSafe(true);
this.serializeEmptyStrings = true;
}
// for unit testing
JsonWriter(com.google.gson.stream.JsonWriter stream) {
this.stream = stream;
}
public static JsonWriter of(Writer writer) {
return new JsonWriter(writer);
}
public JsonWriter setSerializeNulls(boolean b) {
this.stream.setSerializeNulls(b);
return this;
}
/**
* Enable/disable serialization of properties which value is an empty String.
*/
public JsonWriter setSerializeEmptys(boolean serializeEmptyStrings) {
this.serializeEmptyStrings = serializeEmptyStrings;
return this;
}
/**
* 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(serializeEmptyStrings ? value : emptyToNull(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)}
* - {@code Map
* - Iterable
*
*
* @throws org.sonar.api.utils.text.WriterException on any failure
*/
public JsonWriter valueObject(@Nullable Object value) {
try {
if (value == null) {
stream.nullValue();
return this;
}
valueNonNullObject(value);
return this;
} catch (IllegalArgumentException e) {
throw e;
} catch (Exception e) {
throw rethrow(e);
}
}
private void valueNonNullObject(Object value) throws IOException {
if (value instanceof String) {
stream.value(serializeEmptyStrings ? (String) value : emptyToNull((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 Enum) {
stream.value(((Enum) value).name());
} else if (value instanceof Map) {
stream.beginObject();
for (Map.Entry