io.quarkus.vertx.http.runtime.devmode.Json Maven / Gradle / Ivy
The newest version!
package io.quarkus.vertx.http.runtime.devmode;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.ListIterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Objects;
/**
* A simple JSON string generator.
*/
public final class Json {
private static final String OBJECT_START = "{";
private static final String OBJECT_END = "}";
private static final String ARRAY_START = "[";
private static final String ARRAY_END = "]";
private static final String NAME_VAL_SEPARATOR = ":";
private static final String ENTRY_SEPARATOR = ",";
private static final int CONTROL_CHAR_START = 0;
private static final int CONTROL_CHAR_END = 0x1f;
private static final char CHAR_QUOTATION_MARK = '"';
private static final Map REPLACEMENTS;
static {
REPLACEMENTS = new HashMap<>();
// control characters
for (int i = CONTROL_CHAR_START; i <= CONTROL_CHAR_END; i++) {
REPLACEMENTS.put((char) i, String.format("\\u%04x", i));
}
// quotation mark
REPLACEMENTS.put('"', "\\\"");
// reverse solidus
REPLACEMENTS.put('\\', "\\\\");
}
private Json() {
}
/**
* @return the new JSON array builder, empty builders are not ignored
*/
public static JsonArrayBuilder array() {
return new JsonArrayBuilder(false);
}
/**
*
* @param ignoreEmptyBuilders
* @return the new JSON array builder
* @see JsonBuilder#ignoreEmptyBuilders
*/
public static JsonArrayBuilder array(boolean ignoreEmptyBuilders) {
return new JsonArrayBuilder(ignoreEmptyBuilders);
}
/**
*
* @return the new JSON object builder, empty builders are not ignored
*/
public static JsonObjectBuilder object() {
return new JsonObjectBuilder(false);
}
/**
*
* @param ignoreEmptyBuilders
* @return the new JSON object builder
* @see JsonBuilder#ignoreEmptyBuilders
*/
public static JsonObjectBuilder object(boolean ignoreEmptyBuilders) {
return new JsonObjectBuilder(ignoreEmptyBuilders);
}
abstract static class JsonBuilder {
protected final boolean ignoreEmptyBuilders;
/**
*
* @param ignoreEmptyBuilders If set to true all empty builders added to this builder will be ignored during
* {@link #build()}
*/
JsonBuilder(boolean ignoreEmptyBuilders) {
this.ignoreEmptyBuilders = ignoreEmptyBuilders;
}
/**
*
* @return true
if there are no elements/properties, false
otherwise
*/
public abstract boolean isEmpty();
/**
*
* @return a string representation
*/
public abstract String build();
/**
*
* @param value
* @return true
if the value is null or an empty builder and {@link #ignoreEmptyBuilders} is set to
* true
, false
* otherwise
*/
protected boolean isIgnored(Object value) {
return value == null || (ignoreEmptyBuilders && value instanceof JsonBuilder && ((JsonBuilder>) value).isEmpty());
}
protected boolean isValuesEmpty(Collection
© 2015 - 2025 Weber Informatics LLC | Privacy Policy