List of usage examples for com.google.gson.stream JsonReader JsonReader
public JsonReader(Reader in)
From source file:com.seagate.kinetic.simulator.heartbeat.SampleHeartbeatListener.java
License:Open Source License
@Override public void onMessage(byte[] data) { try {//from ww w . j a v a 2s. com String message = new String(data, "UTF8"); JsonReader reader = new JsonReader(new StringReader(message)); reader.setLenient(true); // pretty print use this Gson gson = new GsonBuilder().setPrettyPrinting().create(); // normal print, use this // Gson gson = new Gson(); HeartbeatMessage hbm = gson.fromJson(reader, HeartbeatMessage.class); String jsonOutput = gson.toJson(hbm); logger.info("received heart beat: " + jsonOutput); } catch (Exception e) { logger.log(Level.WARNING, e.getMessage(), e); } }
From source file:com.seleritycorp.common.base.coreservices.RawCoreServiceClient.java
License:Apache License
JsonReader getJsonReader(InputStream stream) { return new JsonReader( new BufferedReader(new InputStreamReader(stream, StandardCharsets.UTF_8), BUFFER_SIZE)); }
From source file:com.semenov.core.data.json.JSONImport.java
/** * Gets uploaded data file//from w w w. ja v a 2 s . c o m * @return parsed object * @throws XMLException */ private DataImportTemplate loadFile() throws JSONException { try { Gson g = new Gson(); return g.fromJson(new JsonReader(new InputStreamReader(request.getPart(DATA_FILE).getInputStream())), DataImportTemplate.class); } catch (Exception e) { throw new JSONException("Could not parse file: " + e); } }
From source file:com.solidfire.jsvcgen.client.ServiceBase.java
License:Open Source License
/** * Decodes the JSON-RPC response.//from w w w .j a va 2 s. c o m * * @param response the JSON encoded response * @param resultParamsClass the class (type) of the result object returned * @return the result (response) object */ protected <TResult> TResult decodeResponse(String response, Class<TResult> resultParamsClass) { log.debug("Response: {}", response); final Gson gson = getGsonBuilder().create(); try { final JsonReader reader = new JsonReader(new StringReader(response)); reader.setLenient(true); final JsonObject resultObj = gson.fromJson(reader, JsonObject.class); checkForError(resultObj); TResult result = gson.fromJson(resultObj.get("result"), resultParamsClass); OptionalAdaptorUtils.initializeAllNullOptionalFieldsAsEmpty(result); ArrayAdaptorUtils.convertAllArrayListsToArrays(result); return result; } catch (ClassCastException e) { final Pattern pattern = Pattern.compile("<p> (.*?)</p>"); final Matcher matcher = pattern.matcher(response); if (matcher.find()) { throw new ApiServerException("Not Found", "404", matcher.group(1)); } throw new ApiException( format("There was a problem parsing the response from the server. ( response=%s )", response), e); } catch (NullPointerException | JsonParseException e) { log.debug(response); throw new ApiException( format("There was a problem parsing the response from the server. ( response=%s )", response), e); } }
From source file:com.sonaive.v2ex.sync.V2exDataHandler.java
License:Open Source License
/** * Processes data body and calls the appropriate data type handlers * to process each of the objects represented therein. * * @param dataBundle The body of data to process * @throws IOException If there is an error parsing the data. *//*from w w w.j a v a 2 s . c om*/ private void processDataBody(Bundle dataBundle, String key) throws IOException { if (mHandlerForKey.containsKey(key)) { String dataBody = mHandlerForKey.get(key).getBody(dataBundle); JsonReader reader = new JsonReader(new StringReader(dataBody)); JsonParser parser = new JsonParser(); // pass the value to the corresponding handler mHandlerForKey.get(key).process(parser.parse(reader)); } }
From source file:com.sonaive.v2ex.util.ModelUtils.java
License:Open Source License
public static Member getAuthor(String json) { Member result = null;/*from ww w. ja v a 2s .c o m*/ try { JsonReader reader = new JsonReader(new StringReader(json)); JsonParser parser = new JsonParser(); result = new Gson().fromJson(parser.parse(reader), Member.class); } catch (NullPointerException e) { LOGE(TAG, "Failed parsing member, JSON source is null, catch a NullPointerException"); } catch (JsonIOException e) { LOGE(TAG, "Failed parsing JSON source: " + json + ", catch a JsonIOException"); } catch (JsonSyntaxException e) { LOGE(TAG, "Failed parsing JSON source: " + json + ", catch a JsonSyntaxException"); } return result; }
From source file:com.sonaive.v2ex.util.ModelUtils.java
License:Open Source License
public static Node getNode(String json) { Node result = null;// w w w .j a v a 2s . co m try { JsonReader reader = new JsonReader(new StringReader(json)); JsonParser parser = new JsonParser(); result = new Gson().fromJson(parser.parse(reader), Node.class); } catch (NullPointerException e) { LOGE(TAG, "Failed parsing node, JSON source is null, catch a NullPointerException"); } catch (JsonIOException e) { LOGE(TAG, "Failed parsing JSON source: " + json + ", catch a JsonIOException"); } catch (JsonSyntaxException e) { LOGE(TAG, "Failed parsing JSON source: " + json + ", catch a JsonSyntaxException"); } return result; }
From source file:com.splunk.ResultsReaderJson.java
License:Apache License
ResultsReaderJson(InputStream inputStream, boolean isInMultiReader) throws IOException { super(inputStream, isInMultiReader); jsonReader = new JsonReader(new InputStreamReader(inputStream, "UTF-8")); // if stream is empty, return a null reader. jsonReader.setLenient(true);/*w w w. j a v a 2 s .co m*/ if (isExportStream || isInMultiReader) exportHelper = new ExportHelper(); finishInitialization(); }
From source file:com.splunk.sdk.ResultsReaderJson.java
License:Apache License
/** * Class constructor./*www . j ava2 s . c o m*/ * * Constructs a streaming JSON reader for the event stream. You should only * attempt to parse a JSON stream with the JSON reader. Using a non-JSON * stream yields unpredictable results. * * @param inputStream The stream to parse. * @throws Exception On exception. */ public ResultsReaderJson(InputStream inputStream) throws IOException { super(inputStream); jsonReader = new JsonReader(new InputStreamReader(inputStream, "UTF8")); // if stream is empty, return a null reader. try { if (jsonReader.peek() == JsonToken.BEGIN_OBJECT) { // In Splunk 5.0, JSON output is an object, and the results are // an array at that object's key "results". In Splunk 4.3, the // array was the top level returned. So if we find an object // at top level, we step into it until we find the right key, // then leave it in that state to iterate over. jsonReader.beginObject(); String key; while (true) { key = jsonReader.nextName(); if (key.equals("results")) { jsonReader.beginArray(); return; } else { skipEntity(); } } } else { // We're on Splunk 4.x, and we just need to start the array. jsonReader.beginArray(); return; } } catch (EOFException e) { jsonReader = null; return; } }
From source file:com.sra.biotech.submittool.cdm.builder.ExperimentBuilder.java
public Experiment buildFromJson(String json) { //System.out.println(" Experiment buildFromJson = " + json); GsonBuilder gsonBuilder = new GsonBuilder(); gsonBuilder.registerTypeAdapter(TypeLibraryLayouts.class, new AttributeTypeLibraryLayoutsDeserializer()); gsonBuilder.registerTypeAdapter(TypeLibrarySelections.class, new AttributeTypeLibrarySelectionsDeserializer()); gsonBuilder.registerTypeAdapter(TypeLibrarySources.class, new AttributeTypeLibrarySourcesDeserializer()); gsonBuilder.registerTypeAdapter(TypeLibraryStrategies.class, new AttributeTypeLibraryStrategiesDeserializer()); gsonBuilder.registerTypeAdapter(TypeFileTypes.class, new AttributeTypeFileTypesDeserializer()); Gson gson = gsonBuilder.create();// www.j a v a2 s .c om Experiment instance = null; JsonReader reader = null; try { reader = new JsonReader(new StringReader(json)); } catch (Exception e) { // TODO: handle exception } try { // instance = gson.fromJson(new FileReader("data/study.json"), // Experiment.class); Library library = new LibraryBuilder().buildFromJson(json); instance = gson.fromJson(reader, Experiment.class); Run run = new Run(); //List<File> files = new FileBuilder().buildFromGson(gson); //List<File> files =instance.getFiles(); DataBlock dataBlock = new DataBlockBuilder().buildFromJson(json); for (File file : dataBlock.getFiles()) { file.setType(TypeFileTypes.FASTQ); } //dataBlock.setFiles(files); run.getDataBlocks().add(dataBlock); instance.getRuns().add(run); instance.setLibrary(library); final String jsonStr = gson.toJson(instance); JsonParser parser = new JsonParser(); JsonObject json2 = parser.parse(jsonStr).getAsJsonObject(); Gson gson2 = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().setPrettyPrinting().create(); String prettyJson = gson.toJson(json2); System.out.println("Deserializer " + " Experiment"); //System.out.println(prettyJson); } catch (JsonSyntaxException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (JsonIOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return instance; }