org.jboss.weld.probe.Json Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of weld-servlet-shaded Show documentation
Show all versions of weld-servlet-shaded Show documentation
This jar bundles all the bits of Weld and CDI required for running in a Servlet container.
/*
* JBoss, Home of Professional Open Source
* Copyright 2014, Red Hat, Inc., and individual contributors
* by the @authors tag. See the copyright.txt in the distribution for a
* full listing of individual contributors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jboss.weld.probe;
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 javax.enterprise.inject.Vetoed;
import org.jboss.weld.util.Preconditions;
/**
* Simple JSON generator. A third-party library is not used intentionally - we don't need any other dependencies.
*
* @author Martin Kouba
*/
@Vetoed
final class Json {
private static final String NAME = "name";
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 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 static final char CHAR_QUOTATION_MARK = '"';
private Json() {
}
/**
* @return the new JSON array builder, empty builders are not ignored
*/
static JsonArrayBuilder arrayBuilder() {
return new JsonArrayBuilder(false);
}
/**
*
* @param ignoreEmptyBuilders
* @return the new JSON array builder
* @see JsonBuilder#ignoreEmptyBuilders
*/
static JsonArrayBuilder arrayBuilder(boolean ignoreEmptyBuilders) {
return new JsonArrayBuilder(ignoreEmptyBuilders);
}
/**
*
* @return the new JSON object builder, empty builders are not ignored
*/
static JsonObjectBuilder objectBuilder() {
return new JsonObjectBuilder(false);
}
/**
*
* @param ignoreEmptyBuilders
* @return the new JSON object builder
* @see JsonBuilder#ignoreEmptyBuilders
*/
static JsonObjectBuilder objectBuilder(boolean ignoreEmptyBuilders) {
return new JsonObjectBuilder(ignoreEmptyBuilders);
}
/**
*
* @author Martin Kouba
*
* @param Builder type
*/
abstract static class JsonBuilder {
protected boolean ignoreEmptyBuilders = false;
/**
*
* @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
*/
abstract boolean isEmpty();
/**
*
* @return a string representation
*/
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