本文整理汇总了Java中com.google.gson.JsonIOException类的典型用法代码示例。如果您正苦于以下问题:Java JsonIOException类的具体用法?Java JsonIOException怎么用?Java JsonIOException使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
JsonIOException类属于com.google.gson包,在下文中一共展示了JsonIOException类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: readGeneric
import com.google.gson.JsonIOException; //导入依赖的package包/类
public static int readGeneric(File f) {
int count = 0;
Gson gson = new GsonBuilder().setPrettyPrinting().registerTypeAdapter(Location.class, new LocationAdapter()).create();
try (FileReader reader = new FileReader(f)) {
GenericNPC[] vils = gson.fromJson(reader, GenericNPC[].class);
for (GenericNPC gv : vils) {
if (gv == null)
continue;
NPCManager.register(gv);
genericVillagers.add(gv);
}
count += vils.length;
} catch (JsonSyntaxException | JsonIOException | IOException e) {
System.err.println("Error reading NPC in " + f.getName() + ".");
e.printStackTrace();
}
return count;
}
示例2: handleResponseError
import com.google.gson.JsonIOException; //导入依赖的package包/类
@Override
public void handleResponseError(Context context, Throwable t) {
Timber.tag("Catch-Error").w(t.getMessage());
//这里不光是只能打印错误,还可以根据不同的错误作出不同的逻辑处理
String msg = "未知错误";
if (t instanceof UnknownHostException) {
msg = "网络不可用";
} else if (t instanceof SocketTimeoutException) {
msg = "请求网络超时";
} else if (t instanceof HttpException) {
HttpException httpException = (HttpException) t;
msg = convertStatusCode(httpException);
} else if (t instanceof JsonParseException || t instanceof ParseException || t instanceof JSONException || t instanceof JsonIOException) {
msg = "数据解析错误";
}
ArmsUtils.snackbarText(msg);
}
示例3: writeInternal
import com.google.gson.JsonIOException; //导入依赖的package包/类
protected void writeInternal(Object o, HttpOutputMessage outputMessage) throws IOException, HttpMessageNotWritableException {
Charset charset = this.getCharset(outputMessage.getHeaders());
OutputStreamWriter writer = new OutputStreamWriter(outputMessage.getBody(), charset);
try {
if(this.jsonPrefix != null) {
writer.append(this.jsonPrefix);
}
this.gson.toJson(o, writer);
writer.close();
} catch (JsonIOException var7) {
throw new HttpMessageNotWritableException("Could not write JSON: " + var7.getMessage(), var7);
}
}
示例4: createJsonArrayFromString
import com.google.gson.JsonIOException; //导入依赖的package包/类
/**
* Creates a {@link JsonArray} from the given json string.
*
* @param jsonString the json string to parse
* @return the {@link JsonArray} received while parsing the given json string
* @throws JsonParsingFailedException if any exception occurred at runtime
*/
public static JsonArray createJsonArrayFromString(final String jsonString) throws JsonParsingFailedException {
InputStream stream = new ByteArrayInputStream(jsonString.getBytes(StandardCharsets.UTF_8));
Reader reader = new InputStreamReader(stream, StandardCharsets.UTF_8);
JsonArray readJsonArray = null;
try {
readJsonArray = new Gson().fromJson(reader, JsonArray.class);
} catch (JsonSyntaxException jsonSyntaxException) {
throw new JsonParsingFailedException(jsonSyntaxException);
} catch (JsonIOException jsonIoException) {
throw new JsonParsingFailedException(jsonIoException);
}
return readJsonArray;
}
示例5: createJsonObjectFromString
import com.google.gson.JsonIOException; //导入依赖的package包/类
/**
* Creates a {@link JsonObject} from the given json string.
*
* @param jsonString the json string to parse
* @return the {@link JsonObject} received while parsing the given json string
* @throws JsonParsingFailedException if any exception occurred at runtime
*/
public static JsonObject createJsonObjectFromString(final String jsonString) throws JsonParsingFailedException {
InputStream stream = new ByteArrayInputStream(jsonString.getBytes(StandardCharsets.UTF_8));
Reader reader = new InputStreamReader(stream, StandardCharsets.UTF_8);
JsonObject readJsonObject = null;
try {
readJsonObject = new Gson().fromJson(reader, JsonObject.class);
} catch (JsonSyntaxException jsonSyntaxException) {
throw new JsonParsingFailedException(jsonSyntaxException);
} catch (JsonIOException jsonIoException) {
throw new JsonParsingFailedException(jsonIoException);
}
return readJsonObject;
}
示例6: createJsonObjectFromFilePath
import com.google.gson.JsonIOException; //导入依赖的package包/类
/**
* Creates a {@link JsonObject} from the json file which can be found at the given path.
*
* Note that this method should only be used with a jsonFilePath starting at folders inside the
* resource directory, e.g. algorithm/baselearner/... . Otherwise it will result in a
* {@link NullPointerException}. For accessing {@link JsonObject}s with a more detailed path, use
* {@link #createJsonObjectFromFile(File)}.
*
* @param jsonFilePath the path of the json file
* @return the {@link JsonObject} received while parsing the json file
* @throws JsonParsingFailedException if any exception occurred at runtime
* @throws NullPointerException if the given file path does not start with a folder or file
* inside the resource directory
*/
public static JsonObject createJsonObjectFromFilePath(final String jsonFilePath) throws JsonParsingFailedException {
JsonObject readJsonObject = null;
InputStream inputStream = JsonUtils.class.getResourceAsStream(jsonFilePath);
InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
try (Reader reader = new BufferedReader(inputStreamReader)) {
readJsonObject = new Gson().fromJson(reader, JsonObject.class);
} catch (JsonSyntaxException jsonSyntaxException) {
throw new JsonParsingFailedException(jsonSyntaxException);
} catch (JsonIOException jsonIoException) {
throw new JsonParsingFailedException(jsonIoException);
} catch (FileNotFoundException fileNotFoundException) {
throw new JsonParsingFailedException(fileNotFoundException);
} catch (IOException ioException) {
throw new JsonParsingFailedException(ioException);
}
return readJsonObject;
}
示例7: createJsonObjectFromFile
import com.google.gson.JsonIOException; //导入依赖的package包/类
/**
* Creates a {@link JsonObject} from the given json file.
*
* @param jsonFile the json filet to read
* @return the {@link JsonObject} received while parsing the json file
* @throws JsonParsingFailedException if any exception occurred at runtime
*/
public static JsonObject createJsonObjectFromFile(final File jsonFile) throws JsonParsingFailedException {
JsonObject readJsonObject = null;
try (Reader reader = new FileReader(jsonFile)) {
readJsonObject = new Gson().fromJson(reader, JsonObject.class);
} catch (JsonSyntaxException jsonSyntaxException) {
throw new JsonParsingFailedException(jsonSyntaxException);
} catch (JsonIOException jsonIoException) {
throw new JsonParsingFailedException(jsonIoException);
} catch (FileNotFoundException fileNotFoundException) {
throw new JsonParsingFailedException(fileNotFoundException);
} catch (IOException ioException) {
throw new JsonParsingFailedException(ioException);
}
return readJsonObject;
}
示例8: parse
import com.google.gson.JsonIOException; //导入依赖的package包/类
public static JsonElement parse(JsonReader reader) throws JsonParseException {
boolean isEmpty = true;
try {
reader.peek();
isEmpty = false;
return (JsonElement) TypeAdapters.JSON_ELEMENT.read(reader);
} catch (Throwable e) {
if (isEmpty) {
return JsonNull.INSTANCE;
}
throw new JsonIOException(e);
} catch (Throwable e2) {
throw new JsonSyntaxException(e2);
} catch (Throwable e22) {
throw new JsonIOException(e22);
} catch (Throwable e222) {
throw new JsonSyntaxException(e222);
}
}
示例9: decode
import com.google.gson.JsonIOException; //导入依赖的package包/类
@Override
public Object decode(Response response, Type type) throws IOException {
if (response.status() == 404) return Util.emptyValueOf(type);
if (response.body() == null) return null;
Reader reader = response.body().asReader();
try {
return gson.fromJson(reader, type);
} catch (JsonIOException e) {
if (e.getCause() != null && e.getCause() instanceof IOException) {
throw IOException.class.cast(e.getCause());
}
throw e;
} finally {
ensureClosed(reader);
}
}
示例10: decode
import com.google.gson.JsonIOException; //导入依赖的package包/类
@Override
public Object decode(Response response, Type type) throws IOException {
if (void.class == type || response.body() == null) {
return null;
}
Reader reader = response.body().asReader();
try {
return gson.fromJson(reader, type);
} catch (JsonIOException e) {
if (e.getCause() != null && e.getCause() instanceof IOException) {
throw IOException.class.cast(e.getCause());
}
throw e;
} finally {
ensureClosed(reader);
}
}
示例11: writeInternal
import com.google.gson.JsonIOException; //导入依赖的package包/类
@Override
protected void writeInternal(Object o, Type type, HttpOutputMessage outputMessage)
throws IOException, HttpMessageNotWritableException {
Charset charset = getCharset(outputMessage.getHeaders());
try (OutputStreamWriter writer = new OutputStreamWriter(outputMessage.getBody(), charset)) {
if (ignoreType) {
gsonForWriter.toJson(o, writer);
return;
}
if (type != null) {
gsonForWriter.toJson(o, type, writer);
return;
}
gsonForWriter.toJson(o, writer);
} catch (JsonIOException ex) {
throw new HttpMessageNotWritableException("Could not write JSON: " + ex.getMessage(), ex);
}
}
示例12: parse
import com.google.gson.JsonIOException; //导入依赖的package包/类
public static JsonElement parse(JsonReader reader) throws JsonParseException {
boolean isEmpty = true;
try {
reader.peek();
isEmpty = false;
return (JsonElement) TypeAdapters.JSON_ELEMENT.read(reader);
} catch (Throwable e) {
if (isEmpty) {
return JsonNull.INSTANCE;
}
throw new JsonSyntaxException(e);
} catch (Throwable e2) {
throw new JsonSyntaxException(e2);
} catch (Throwable e22) {
throw new JsonIOException(e22);
} catch (Throwable e222) {
throw new JsonSyntaxException(e222);
}
}
示例13: fetchUserAccounts
import com.google.gson.JsonIOException; //导入依赖的package包/类
private void fetchUserAccounts() {
NetworkManager.Get(requestUrl, new HttpResponse() {
@Override
public void success(String response) {
try {
Type listType = new TypeToken<ArrayList<NRAccount>>() {
}.getType();
List<NRAccount> accounts = new Gson().fromJson(response, listType);
initRecyclerView(accounts);
}
catch(JsonSyntaxException | JsonIOException jioe) {
Log.i("gson", "parse failed");
}
}
@Override
public void error() {
}
});
}
示例14: handleResponseError
import com.google.gson.JsonIOException; //导入依赖的package包/类
@Override
public void handleResponseError(Context context, Throwable t) {
//Used to provide a monitor for handling all errors
//rxjava need to use ErrorHandleSubscriber (the default implementation of Subscriber's onError method), this monitor to take effect
Timber.tag("Catch-Error").w(t.getMessage());
//Here is not only print errors, but also according to different errors to make different logical processing
String msg = "Unknown";
if (t instanceof UnknownHostException) {
msg = "The network is not available";
} else if (t instanceof SocketTimeoutException) {
msg = "Network timeout";
} else if (t instanceof HttpException) {
HttpException httpException = (HttpException) t;
msg = convertStatusCode(httpException);
} else if (t instanceof JsonParseException || t instanceof ParseException || t instanceof JSONException || t instanceof JsonIOException) {
msg = "Data parsing error";
}
UiUtils.snackbarText(msg);
}
示例15: LoadRecipes
import com.google.gson.JsonIOException; //导入依赖的package包/类
public List<Recipe> LoadRecipes() {
Gson gson = new Gson();
List<Recipe> recipes = new ArrayList<>();
for (File file : getRecipesFolder().listFiles()) {
if (!file.getName().endsWith(".json"))
continue;
try {
JsonReader reader = new JsonReader(new FileReader(file));
Recipe recipe = gson.fromJson(reader, Recipe.class);
recipes.add(recipe);
} catch (FileNotFoundException | JsonSyntaxException | JsonIOException e) {
logger.error(e.getMessage());
e.printStackTrace();
}
}
return recipes;
}