List of usage examples for com.google.gson.stream JsonReader beginArray
public void beginArray() throws IOException
From source file:org.apache.airavata.workflow.core.parser.JsonWorkflowParser.java
License:Apache License
private List<InPort> readApplicationInputs(JsonReader jsonReader) throws IOException, ParserException { List<InPort> inPorts = new ArrayList<>(); JsonToken peek = jsonReader.peek();/* www . j av a 2 s.c o m*/ PortModel portModel; InPort inPort; String name; if (peek == JsonToken.NULL) { jsonReader.nextNull(); } else if (peek == JsonToken.BEGIN_ARRAY) { jsonReader.beginArray(); while (jsonReader.hasNext()) { portModel = new PortModel(); inPort = new InputPortIml(portModel); jsonReader.beginObject(); while (jsonReader.hasNext()) { name = jsonReader.nextName(); if (name.equals(NAME)) { portModel.setName(jsonReader.nextString()); } else if (name.equals(ID)) { portModel.setPortId(jsonReader.nextString()); } else if (name.equals(DATATYPE)) { jsonReader.skipValue(); } else if (name.equals(DEFAULT_VALUE)) { inPort.setDefaultValue(jsonReader.nextString()); } else if (name.equals(DESCRIPTION)) { portModel.setDescription(jsonReader.nextString()); } else { jsonReader.skipValue(); } } jsonReader.endObject(); inPorts.add(inPort); } jsonReader.endArray(); } else { throw new ParserException( "Error! reading application inputs, expected " + getTokenString(JsonToken.NULL) + " or " + getTokenString(JsonToken.BEGIN_ARRAY) + " but found " + getTokenString(peek)); } return inPorts; }
From source file:org.apache.airavata.workflow.core.parser.JsonWorkflowParser.java
License:Apache License
private List<OutPort> readApplicationOutputs(JsonReader jsonReader) throws IOException, ParserException { List<OutPort> outPorts = new ArrayList<>(); PortModel portModel;/*from www . j a v a2 s . com*/ OutPort outPort; String name; JsonToken peek = jsonReader.peek(); if (peek == JsonToken.NULL) { jsonReader.nextNull(); } else if (peek == JsonToken.BEGIN_ARRAY) { jsonReader.beginArray(); while (jsonReader.hasNext()) { portModel = new PortModel(); outPort = new OutPortImpl(portModel); jsonReader.beginObject(); while (jsonReader.hasNext()) { name = jsonReader.nextName(); if (name.equals(NAME)) { portModel.setName(jsonReader.nextString()); } else if (name.equals(ID)) { portModel.setPortId(jsonReader.nextString()); } else if (name.equals(DATATYPE)) { jsonReader.skipValue(); } else if (name.equals(DEFAULT_VALUE)) { jsonReader.skipValue(); // can output has default values? } else if (name.equals(DESCRIPTION)) { portModel.setDescription(jsonReader.nextString()); } else { jsonReader.skipValue(); } } jsonReader.endObject(); outPorts.add(outPort); } jsonReader.endArray(); } else { throw new ParserException( "Error! reading application outputs, expected " + getTokenString(JsonToken.NULL) + " or " + getTokenString(JsonToken.BEGIN_ARRAY) + " but found " + getTokenString(peek)); } return outPorts; }
From source file:org.apache.ambari.view.hive.resources.uploads.parsers.json.JSONIterator.java
License:Apache License
public JSONIterator(JsonReader reader) throws IOException { this.reader = reader; // test the start of array JsonToken jt = reader.peek();/*w w w. ja va2s .c om*/ if (jt != JsonToken.BEGIN_ARRAY) { throw new IllegalArgumentException("Expected the whole document to contain a single JsonArray."); } reader.beginArray(); // read the start of array try { nextObject = readNextObject(this.reader); } catch (EndOfDocumentException e) { } }
From source file:org.apache.axis2.json.gson.rpc.JsonUtils.java
License:Apache License
public static Object invokeServiceClass(JsonReader jsonReader, Object service, Method operation, Class[] paramClasses, int paramCount) throws InvocationTargetException, IllegalAccessException, IOException { Object[] methodParam = new Object[paramCount]; Gson gson = new Gson(); String[] argNames = new String[paramCount]; if (!jsonReader.isLenient()) { jsonReader.setLenient(true);//ww w . j av a2 s. c o m } jsonReader.beginObject(); jsonReader.nextName(); // get message name from input json stream jsonReader.beginArray(); int i = 0; for (Class paramType : paramClasses) { jsonReader.beginObject(); argNames[i] = jsonReader.nextName(); methodParam[i] = gson.fromJson(jsonReader, paramType); // gson handle all types well and return an object from it jsonReader.endObject(); i++; } jsonReader.endArray(); jsonReader.endObject(); return operation.invoke(service, methodParam); }
From source file:org.apache.hadoop.dynamodb.importformat.ImportInputFormat.java
License:Open Source License
/** * This method retrieves the URLs of all S3 files and generates input splits by combining * multiple S3 URLs into one split.//from w ww . j a v a 2 s . c o m * * @return a list of input splits. The length of this list may not be exactly the same as * <code>numSplits</code>. For example, if numSplits is larger than MAX_NUM_SPLITS or the number * of S3 files, then numSplits is ignored. Furthermore, not all input splits contain the same * number of S3 files. For example, with five S3 files {s1, s2, s3, s4, s5} and numSplits = 3, * this method returns a list of three input splits: {s1, s2}, {s3, s4} and {s5}. */ private List<InputSplit> readEntries(JsonReader reader, JobConf job) throws IOException { List<Path> paths = new ArrayList<Path>(); Gson gson = DynamoDBUtil.getGson(); reader.beginArray(); while (reader.hasNext()) { ExportManifestEntry entry = gson.fromJson(reader, ExportManifestEntry.class); paths.add(new Path(entry.url)); } reader.endArray(); log.info("Number of S3 files: " + paths.size()); if (paths.size() == 0) { return Collections.emptyList(); } int filesPerSplit = (int) Math.ceil((double) (paths.size()) / Math.min(MAX_NUM_SPLITS, paths.size())); int numSplits = (int) Math.ceil((double) (paths.size()) / filesPerSplit); long[] fileMaxLengths = new long[filesPerSplit]; Arrays.fill(fileMaxLengths, Long.MAX_VALUE / filesPerSplit); long[] fileStarts = new long[filesPerSplit]; Arrays.fill(fileStarts, 0); List<InputSplit> splits = new ArrayList<InputSplit>(numSplits); for (int i = 0; i < numSplits; i++) { int start = filesPerSplit * i; int end = filesPerSplit * (i + 1); if (i == (numSplits - 1)) { end = paths.size(); } Path[] pathsInOneSplit = paths.subList(start, end).toArray(new Path[end - start]); CombineFileSplit combineFileSplit = new CombineFileSplit(job, pathsInOneSplit, fileStarts, fileMaxLengths, new String[0]); splits.add(combineFileSplit); } return splits; }
From source file:org.apache.jclouds.oneandone.rest.util.ServerFirewallPolicyAdapter.java
License:Apache License
@Override public List<T> read(JsonReader reader) throws IOException { List<ServerFirewallPolicy> list = new ArrayList<ServerFirewallPolicy>(); if (reader.peek() == JsonToken.BEGIN_OBJECT) { Type mapType = new TypeToken<Map<String, Object>>() { }.getType();/*from w ww. j a v a 2 s . c om*/ Map<String, String> jsonMap = gson.fromJson(reader, mapType); ServerFirewallPolicy inning = ServerFirewallPolicy.create(jsonMap.get("id"), jsonMap.get("name")); list.add(inning); } else if (reader.peek() == JsonToken.BEGIN_ARRAY) { reader.beginArray(); while (reader.hasNext()) { Type mapType = new TypeToken<Map<String, Object>>() { }.getType(); Map<String, String> jsonMap = gson.fromJson(reader, mapType); ServerFirewallPolicy inning = ServerFirewallPolicy.create(jsonMap.get("id"), jsonMap.get("name")); list.add(inning); } reader.endArray(); } else { reader.skipValue(); } return (List<T>) list; }
From source file:org.apache.jclouds.oneandone.rest.util.SnapshotAdapter.java
License:Apache License
@Override public List<T> read(JsonReader reader) throws IOException { List<Snapshot> list = new ArrayList<Snapshot>(); if (reader.peek() == JsonToken.BEGIN_OBJECT) { Type mapType = new TypeToken<Map<String, Object>>() { }.getType();//from w w w . jav a 2s .c o m Map<String, String> jsonMap = gson.fromJson(reader, mapType); Snapshot inning = Snapshot.create(jsonMap.get("id"), jsonMap.get("creation_date"), jsonMap.get("deletion_date")); list.add(inning); } else if (reader.peek() == JsonToken.BEGIN_ARRAY) { reader.beginArray(); while (reader.hasNext()) { Type mapType = new TypeToken<Map<String, Object>>() { }.getType(); Map<String, String> jsonMap = gson.fromJson(reader, mapType); Snapshot inning = Snapshot.create(jsonMap.get("id"), jsonMap.get("creation_date"), jsonMap.get("deletion_date")); list.add(inning); } reader.endArray(); } else { reader.skipValue(); } return (List<T>) list; }
From source file:org.apache.nifi.toolkit.zkmigrator.ZooKeeperMigrator.java
License:Apache License
void writeZooKeeper(InputStream zkData, AuthMode authMode, byte[] authData, boolean ignoreSource, boolean useExistingACL) throws IOException, ExecutionException, InterruptedException { // ensure that the chroot path exists ZooKeeper zooKeeperRoot = getZooKeeper(Joiner.on(',').join(zooKeeperEndpointConfig.getServers()), authMode, authData);// w ww . j a v a 2s. c om ensureNodeExists(zooKeeperRoot, zooKeeperEndpointConfig.getPath(), CreateMode.PERSISTENT); closeZooKeeper(zooKeeperRoot); ZooKeeper zooKeeper = getZooKeeper(zooKeeperEndpointConfig.getConnectString(), authMode, authData); JsonReader jsonReader = new JsonReader(new BufferedReader(new InputStreamReader(zkData))); Gson gson = new GsonBuilder().create(); jsonReader.beginArray(); // determine source ZooKeeperEndpointConfig for this data final ZooKeeperEndpointConfig sourceZooKeeperEndpointConfig = gson.fromJson(jsonReader, ZooKeeperEndpointConfig.class); LOGGER.info("Source data was obtained from ZooKeeper: {}", sourceZooKeeperEndpointConfig); Preconditions.checkArgument( !Strings.isNullOrEmpty(sourceZooKeeperEndpointConfig.getConnectString()) && !Strings.isNullOrEmpty(sourceZooKeeperEndpointConfig.getPath()) && sourceZooKeeperEndpointConfig.getServers() != null && sourceZooKeeperEndpointConfig.getServers().size() > 0, "Source ZooKeeper %s from %s is invalid", sourceZooKeeperEndpointConfig, zkData); Preconditions.checkArgument( Collections .disjoint(zooKeeperEndpointConfig.getServers(), sourceZooKeeperEndpointConfig.getServers()) || !zooKeeperEndpointConfig.getPath().equals(sourceZooKeeperEndpointConfig.getPath()) || ignoreSource, "Source ZooKeeper config %s for the data provided can not contain the same server and path as the configured destination ZooKeeper config %s", sourceZooKeeperEndpointConfig, zooKeeperEndpointConfig); // stream through each node read from the json input final Stream<DataStatAclNode> stream = StreamSupport .stream(new Spliterators.AbstractSpliterator<DataStatAclNode>(0, 0) { @Override public boolean tryAdvance(Consumer<? super DataStatAclNode> action) { try { // stream each DataStatAclNode from configured json file synchronized (jsonReader) { if (jsonReader.hasNext()) { action.accept(gson.fromJson(jsonReader, DataStatAclNode.class)); return true; } else { return false; } } } catch (IOException e) { throw new RuntimeException("unable to read nodes from json", e); } } }, false); final List<CompletableFuture<Stat>> writeFutures = stream.parallel().map(node -> { /* * create stage to determine the acls that should be applied to the node. * this stage will be used to initialize the chain */ final CompletableFuture<List<ACL>> determineACLStage = CompletableFuture .supplyAsync(() -> determineACLs(node, authMode, useExistingACL)); /* * create stage to apply acls to nodes and transform node to DataStatAclNode object */ final Function<List<ACL>, CompletableFuture<DataStatAclNode>> transformNodeStage = acls -> CompletableFuture .supplyAsync(() -> transformNode(node, acls)); /* * create stage to ensure that nodes exist for the entire path of the zookeeper node, must be invoked after the transformNode stage to * ensure that the node will exist after path migration */ final Function<DataStatAclNode, CompletionStage<String>> ensureNodeExistsStage = dataStatAclNode -> CompletableFuture .supplyAsync(() -> ensureNodeExists(zooKeeper, dataStatAclNode.getPath(), dataStatAclNode.getEphemeralOwner() == 0 ? CreateMode.PERSISTENT : CreateMode.EPHEMERAL)); /* * create stage that waits for both the transformNode and ensureNodeExists stages complete, and also provides that the given transformed node is * available to the next stage */ final BiFunction<String, DataStatAclNode, DataStatAclNode> combineEnsureNodeAndTransferNodeStage = (u, dataStatAclNode) -> dataStatAclNode; /* * create stage to transmit the node to the destination zookeeper endpoint, must be invoked after the node has been transformed and its path * has been created (or already exists) in the destination zookeeper */ final Function<DataStatAclNode, CompletionStage<Stat>> transmitNodeStage = dataStatNode -> CompletableFuture .supplyAsync(() -> transmitNode(zooKeeper, dataStatNode)); /* * submit the stages chained together in the proper order to perform the processing on the given node */ final CompletableFuture<DataStatAclNode> dataStatAclNodeCompletableFuture = determineACLStage .thenCompose(transformNodeStage); return dataStatAclNodeCompletableFuture.thenCompose(ensureNodeExistsStage) .thenCombine(dataStatAclNodeCompletableFuture, combineEnsureNodeAndTransferNodeStage) .thenCompose(transmitNodeStage); }).collect(Collectors.toList()); CompletableFuture<Void> allWritesFuture = CompletableFuture .allOf(writeFutures.toArray(new CompletableFuture[writeFutures.size()])); final CompletableFuture<List<Stat>> finishedWrites = allWritesFuture .thenApply(v -> writeFutures.stream().map(CompletableFuture::join).collect(Collectors.toList())); final List<Stat> writesDone = finishedWrites.get(); if (LOGGER.isInfoEnabled()) { final int writeCount = writesDone.size(); LOGGER.info("{} {} transferred to {}", writeCount, writeCount == 1 ? "node" : "nodes", zooKeeperEndpointConfig); } jsonReader.close(); closeZooKeeper(zooKeeper); }
From source file:org.apache.olingo.odata2.core.ep.consumer.JsonErrorDocumentConsumer.java
License:Apache License
private String readJson(final JsonReader reader) throws IOException { StringBuilder sb = new StringBuilder(); while (reader.hasNext()) { JsonToken token = reader.peek(); if (token == JsonToken.NAME) { if (sb.length() > 0) { sb.append(","); }/*from w ww . j ava 2 s . c o m*/ String name = reader.nextName(); sb.append("\"").append(name).append("\"").append(":"); } else if (token == JsonToken.BEGIN_OBJECT) { reader.beginObject(); sb.append("{").append(readJson(reader)).append("}"); reader.endObject(); } else if (token == JsonToken.BEGIN_ARRAY) { reader.beginArray(); sb.append("[").append(readJson(reader)).append("]"); reader.endArray(); } else { sb.append("\"").append(reader.nextString()).append("\""); } } return sb.toString(); }
From source file:org.bimserver.client.ClientIfcModel.java
License:Open Source License
@SuppressWarnings({ "unchecked", "rawtypes" }) private void processDownload(Long download) throws BimServerClientException, UserException, ServerException, PublicInterfaceNotFoundException { WaitingList<Long> waitingList = new WaitingList<Long>(); try {//from w w w.j ava 2s . c o m InputStream downloadData = bimServerClient.getDownloadData(download, getIfcSerializerOid()); boolean log = false; // TODO Make this streaming again, make sure the EmfSerializer getInputStream method is working properly if (log) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); if (downloadData instanceof SerializerInputstream) { SerializerInputstream serializerInputStream = (SerializerInputstream) downloadData; serializerInputStream.getEmfSerializer().writeToOutputStream(baos); } else { IOUtils.copy((InputStream) downloadData, baos); } FileOutputStream fos = new FileOutputStream(new File(download + ".json")); IOUtils.write(baos.toByteArray(), fos); fos.close(); downloadData = new ByteArrayInputStream(baos.toByteArray()); } else { ByteArrayOutputStream baos = new ByteArrayOutputStream(); if (downloadData instanceof SerializerInputstream) { SerializerInputstream serializerInputStream = (SerializerInputstream) downloadData; serializerInputStream.getEmfSerializer().writeToOutputStream(baos); } else { IOUtils.copy((InputStream) downloadData, baos); } downloadData = new ByteArrayInputStream(baos.toByteArray()); } JsonReader jsonReader = new JsonReader(new InputStreamReader(downloadData, Charsets.UTF_8)); try { jsonReader.beginObject(); if (jsonReader.nextName().equals("objects")) { jsonReader.beginArray(); while (jsonReader.hasNext()) { jsonReader.beginObject(); if (jsonReader.nextName().equals("__oid")) { long oid = jsonReader.nextLong(); if (jsonReader.nextName().equals("__type")) { String type = jsonReader.nextString(); EClass eClass = (EClass) Ifc2x3tc1Package.eINSTANCE.getEClassifier(type); if (eClass == null) { throw new BimServerClientException("No class found with name " + type); } if (jsonReader.nextName().equals("__state")) { String state = jsonReader.nextString(); IdEObject object = null; if (containsNoFetch(oid)) { object = getNoFetch(oid); } else { object = (IdEObject) Ifc2x3tc1Factory.eINSTANCE.create(eClass); ((IdEObjectImpl) object).eSetStore(eStore); ((IdEObjectImpl) object).setOid(oid); add(oid, object); } if (state.equals("NOT_LOADED")) { ((IdEObjectImpl) object).setLoadingState(State.TO_BE_LOADED); } else { while (jsonReader.hasNext()) { String featureName = jsonReader.nextName(); boolean embedded = false; if (featureName.startsWith("__ref")) { featureName = featureName.substring(5); } else if (featureName.startsWith("__emb")) { embedded = true; featureName = featureName.substring(5); } EStructuralFeature eStructuralFeature = eClass .getEStructuralFeature(featureName); if (eStructuralFeature == null) { throw new BimServerClientException("Unknown field (" + featureName + ") on class " + eClass.getName()); } if (eStructuralFeature.isMany()) { jsonReader.beginArray(); if (eStructuralFeature instanceof EAttribute) { List list = (List) object.eGet(eStructuralFeature); List<String> stringList = null; if (eStructuralFeature.getEType() == EcorePackage.eINSTANCE .getEDoubleObject() || eStructuralFeature .getEType() == EcorePackage.eINSTANCE .getEDouble()) { EStructuralFeature asStringFeature = eClass .getEStructuralFeature( eStructuralFeature.getName() + "AsString"); stringList = (List<String>) object.eGet(asStringFeature); } while (jsonReader.hasNext()) { Object e = readPrimitive(jsonReader, eStructuralFeature); list.add(e); if (eStructuralFeature.getEType() == EcorePackage.eINSTANCE .getEDouble()) { double val = (Double) e; stringList.add("" + val); // TODO this is losing precision, maybe also send the string value? } } } else if (eStructuralFeature instanceof EReference) { int index = 0; while (jsonReader.hasNext()) { if (embedded) { List list = (List) object.eGet(eStructuralFeature); jsonReader.beginObject(); String n = jsonReader.nextName(); if (n.equals("__type")) { String t = jsonReader.nextString(); IdEObject wrappedObject = (IdEObject) Ifc2x3tc1Factory.eINSTANCE .create((EClass) Ifc2x3tc1Package.eINSTANCE .getEClassifier(t)); if (jsonReader.nextName().equals("value")) { EStructuralFeature wv = wrappedObject.eClass() .getEStructuralFeature("wrappedValue"); wrappedObject.eSet(wv, readPrimitive(jsonReader, wv)); list.add(wrappedObject); } else { // error } } else if (n.equals("oid")) { // Sometimes embedded is true, bot also referenced are included, those are always embdedded in an object long refOid = jsonReader.nextLong(); if (containsNoFetch(refOid)) { IdEObject refObj = getNoFetch(refOid); AbstractEList l = (AbstractEList) object .eGet(eStructuralFeature); while (l.size() <= index) { l.addUnique(refObj.eClass().getEPackage() .getEFactoryInstance() .create(refObj.eClass())); } l.setUnique(index, refObj); } else { waitingList.add(refOid, new ListWaitingObject( object, eStructuralFeature, index)); } } jsonReader.endObject(); } else { long refOid = jsonReader.nextLong(); if (containsNoFetch(refOid)) { IdEObject refObj = getNoFetch(refOid); AbstractEList l = (AbstractEList) object .eGet(eStructuralFeature); while (l.size() <= index) { l.addUnique(refObj.eClass().getEPackage() .getEFactoryInstance() .create(refObj.eClass())); } l.setUnique(index, refObj); } else { waitingList.add(refOid, new ListWaitingObject( object, eStructuralFeature, index)); } index++; } } } jsonReader.endArray(); } else { if (eStructuralFeature instanceof EAttribute) { Object x = readPrimitive(jsonReader, eStructuralFeature); if (eStructuralFeature.getEType() == EcorePackage.eINSTANCE .getEDouble()) { EStructuralFeature asStringFeature = object.eClass() .getEStructuralFeature( eStructuralFeature.getName() + "AsString"); object.eSet(asStringFeature, "" + x); // TODO this is losing precision, maybe also send the string value? } object.eSet(eStructuralFeature, x); } else if (eStructuralFeature instanceof EReference) { if (embedded) { jsonReader.beginObject(); if (jsonReader.nextName().equals("__type")) { String t = jsonReader.nextString(); IdEObject wrappedObject = (IdEObject) Ifc2x3tc1Factory.eINSTANCE .create((EClass) Ifc2x3tc1Package.eINSTANCE .getEClassifier(t)); if (jsonReader.nextName().equals("value")) { EStructuralFeature wv = wrappedObject.eClass() .getEStructuralFeature("wrappedValue"); wrappedObject.eSet(wv, readPrimitive(jsonReader, wv)); object.eSet(eStructuralFeature, wrappedObject); } else { // error } } jsonReader.endObject(); } else { long refOid = jsonReader.nextLong(); if (containsNoFetch(refOid)) { IdEObject refObj = getNoFetch(refOid); object.eSet(eStructuralFeature, refObj); } else { waitingList.add(refOid, new SingleWaitingObject(object, eStructuralFeature)); } } } } } } if (waitingList.containsKey(oid)) { try { waitingList.updateNode(oid, eClass, object); } catch (DeserializeException e) { LOGGER.error("", e); } } } } } jsonReader.endObject(); } jsonReader.endArray(); } jsonReader.endObject(); } catch (IfcModelInterfaceException e1) { LOGGER.error("", e1); } finally { jsonReader.close(); } } catch (IOException e) { LOGGER.error("", e); } catch (SerializerException e) { LOGGER.error("", e); } finally { waitingList.dumpIfNotEmpty(); bimServerClient.getServiceInterface().cleanupLongAction(download); } }