use of com.google.gson.stream.JsonToken in project airavata by apache.
the class JsonWorkflowParser method readWorkflowInputs.
private void readWorkflowInputs(JsonReader jsonReader) throws ParserException, IOException {
JsonToken peek = jsonReader.peek();
InputNode inputNode;
NodeModel nodeModel;
ComponentStatus status;
String name;
if (peek == JsonToken.NULL) {
throw new ParserException("Error! workflow inputs can't be null");
} else if (peek == JsonToken.BEGIN_ARRAY) {
jsonReader.beginArray();
while (jsonReader.hasNext()) {
jsonReader.beginObject();
nodeModel = new NodeModel();
status = new ComponentStatus();
status.setState(ComponentState.CREATED);
status.setReason("Created");
nodeModel.setStatus(status);
inputNode = new InputNodeImpl(nodeModel);
while (jsonReader.hasNext()) {
name = jsonReader.nextName();
if (name.equals(NAME)) {
nodeModel.setName(jsonReader.nextString());
} else if (name.equals(ID)) {
nodeModel.setNodeId(jsonReader.nextString());
} else if (name.equals(DATATYPE)) {
inputNode.setDataType(DataType.valueOf(jsonReader.nextString()));
} else if (name.equals(DESCRIPTION)) {
nodeModel.setDescription(jsonReader.nextString());
} else if (name.equals(POSITION)) {
readPosition(jsonReader);
} else if (name.equals(NODE_ID)) {
jsonReader.skipValue();
// nodeModel.setNodeId(jsonReader.nextString());
} else if (name.equals(DEFAULT_VALUE)) {
inputNode.setValue(jsonReader.nextString());
} else {
jsonReader.skipValue();
}
}
jsonReader.endObject();
inputs.add(inputNode);
}
jsonReader.endArray();
} else {
throw new ParserException("Error! Unsupported value for Workflow Inputs, exptected " + getTokenString(JsonToken.BEGIN_OBJECT) + " but found" + getTokenString(peek));
}
}
use of com.google.gson.stream.JsonToken in project gerrit by GerritCodeReview.
the class RestApiServlet method parseRequest.
private Object parseRequest(HttpServletRequest req, Type type) throws IOException, BadRequestException, SecurityException, IllegalArgumentException, NoSuchMethodException, IllegalAccessException, InstantiationException, InvocationTargetException, MethodNotAllowedException {
// 400). Consume the request body for all but raw input request types here.
if (isType(JSON_TYPE, req.getContentType())) {
try (BufferedReader br = req.getReader();
JsonReader json = new JsonReader(br)) {
try {
json.setLenient(true);
JsonToken first;
try {
first = json.peek();
} catch (EOFException e) {
throw new BadRequestException("Expected JSON object", e);
}
if (first == JsonToken.STRING) {
return parseString(json.nextString(), type);
}
return OutputFormat.JSON.newGson().fromJson(json, type);
} finally {
try {
// Reader.close won't consume the rest of the input. Explicitly consume the request
// body.
br.skip(Long.MAX_VALUE);
} catch (Exception e) {
// ignore, e.g. trying to consume the rest of the input may fail if the request was
// cancelled
}
}
}
}
String method = req.getMethod();
if (("PUT".equals(method) || "POST".equals(method)) && acceptsRawInput(type)) {
return parseRawInput(req, type);
}
if (isDelete(req) && hasNoBody(req)) {
return null;
}
if (hasNoBody(req)) {
return createInstance(type);
}
if (isType(PLAIN_TEXT, req.getContentType())) {
try (BufferedReader br = req.getReader()) {
char[] tmp = new char[256];
StringBuilder sb = new StringBuilder();
int n;
while (0 < (n = br.read(tmp))) {
sb.append(tmp, 0, n);
}
return parseString(sb.toString(), type);
}
}
if (isPost(req) && isType(FORM_TYPE, req.getContentType())) {
return OutputFormat.JSON.newGson().fromJson(ParameterParser.formToJson(req), type);
}
throw new BadRequestException("Expected Content-Type: " + JSON_TYPE);
}
use of com.google.gson.stream.JsonToken in project maple-ir by LLVM-but-worse.
the class JsonTreeReader method nextLong.
@Override
public long nextLong() throws IOException {
JsonToken token = peek();
if (token != JsonToken.NUMBER && token != JsonToken.STRING) {
throw new IllegalStateException("Expected " + JsonToken.NUMBER + " but was " + token);
}
long result = ((JsonPrimitive) peekStack()).getAsLong();
popStack();
return result;
}
use of com.google.gson.stream.JsonToken in project maple-ir by LLVM-but-worse.
the class JsonTreeReader method nextDouble.
@Override
public double nextDouble() throws IOException {
JsonToken token = peek();
if (token != JsonToken.NUMBER && token != JsonToken.STRING) {
throw new IllegalStateException("Expected " + JsonToken.NUMBER + " but was " + token);
}
double result = ((JsonPrimitive) peekStack()).getAsDouble();
if (!isLenient() && (Double.isNaN(result) || Double.isInfinite(result))) {
throw new NumberFormatException("JSON forbids NaN and infinities: " + result);
}
popStack();
return result;
}
use of com.google.gson.stream.JsonToken in project BIMserver by opensourceBIM.
the class SharedJsonDeserializer method read.
@SuppressWarnings("rawtypes")
public IfcModelInterface read(InputStream in, IfcModelInterface model, boolean checkWaitingList) throws DeserializeException {
if (model.getPackageMetaData().getSchemaDefinition() == null) {
throw new DeserializeException(DeserializerErrorCode.INTERNAL_BIMSERVER_ERROR, "No SchemaDefinition available");
}
WaitingList<Long> waitingList = new WaitingList<Long>();
final boolean log = false;
if (log) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try {
IOUtils.copy(in, baos);
File file = new File("debug.json");
System.out.println(file.getAbsolutePath());
FileUtils.writeByteArrayToFile(file, baos.toByteArray());
} catch (IOException e) {
e.printStackTrace();
}
in = new ByteArrayInputStream(baos.toByteArray());
}
JsonReader jsonReader = new JsonReader(new BufferedReader(new InputStreamReader(in, Charsets.UTF_8)));
int nrObjects = 0;
try {
JsonToken peek = jsonReader.peek();
if (peek != null && peek == JsonToken.BEGIN_OBJECT) {
jsonReader.beginObject();
peek = jsonReader.peek();
while (peek == JsonToken.NAME) {
String nextName = jsonReader.nextName();
if (nextName.equals("objects")) {
jsonReader.beginArray();
while (jsonReader.hasNext()) {
nrObjects++;
processObject(model, waitingList, jsonReader, null);
}
jsonReader.endArray();
} else if (nextName.equals("header")) {
IfcHeader ifcHeader = (IfcHeader) processObject(model, waitingList, jsonReader, StorePackage.eINSTANCE.getIfcHeader());
model.getModelMetaData().setIfcHeader(ifcHeader);
}
peek = jsonReader.peek();
}
jsonReader.endObject();
}
} catch (IOException e) {
LOGGER.error("", e);
} catch (IfcModelInterfaceException e) {
LOGGER.error("", e);
} finally {
LOGGER.debug("# Objects: " + nrObjects);
try {
jsonReader.close();
} catch (IOException e) {
LOGGER.error("", e);
}
}
boolean checkUnique = false;
if (checkUnique) {
for (IdEObject idEObject : model.getValues()) {
for (EStructuralFeature eStructuralFeature : idEObject.eClass().getEAllStructuralFeatures()) {
Object value = idEObject.eGet(eStructuralFeature);
if (eStructuralFeature instanceof EReference) {
if (eStructuralFeature.isMany()) {
List list = (List) value;
if (eStructuralFeature.isUnique()) {
Set<Object> t = new HashSet<>();
for (Object v : list) {
if (t.contains(v)) {
// LOGGER.error("NOT UNIQUE " + idEObject.eClass().getName() + "." + eStructuralFeature.getName());
}
t.add(v);
}
}
}
}
}
}
}
if (checkWaitingList && waitingList.size() > 0) {
try {
waitingList.dumpIfNotEmpty();
} catch (BimServerClientException e) {
e.printStackTrace();
}
throw new DeserializeException(DeserializerErrorCode.NON_EXISTING_ENTITY_REFERENCED, "Waitinglist should be empty (" + waitingList.size() + ")");
}
return model;
}
Aggregations