Search in sources :

Example 1 with JsonParseException

use of com.google.gson.JsonParseException in project buck by facebook.

the class XctoolOutputParsing method streamOutputFromReader.

/**
   * Decodes a stream of JSON objects as produced by {@code xctool -reporter json-stream}
   * and invokes the callbacks in {@code eventCallback} with each event in the stream.
   */
public static void streamOutputFromReader(Reader reader, XctoolEventCallback eventCallback) {
    Gson gson = new Gson();
    JsonStreamParser streamParser = new JsonStreamParser(reader);
    try {
        while (streamParser.hasNext()) {
            dispatchEventCallback(gson, streamParser.next(), eventCallback);
        }
    } catch (JsonParseException e) {
        LOG.warn(e, "Couldn't parse xctool JSON stream");
    }
}
Also used : Gson(com.google.gson.Gson) JsonParseException(com.google.gson.JsonParseException) JsonStreamParser(com.google.gson.JsonStreamParser)

Example 2 with JsonParseException

use of com.google.gson.JsonParseException in project SeriesGuide by UweTrottmann.

the class JsonExportTask method exportData.

private int exportData(File exportPath, @BackupType int type) {
    // check if there is any data to export
    Cursor data = getDataCursor(type);
    if (data == null) {
        // query failed
        return ERROR;
    }
    if (data.getCount() == 0) {
        // There is no data? Done.
        data.close();
        return SUCCESS;
    }
    publishProgress(data.getCount(), 0);
    // try to export all data
    try {
        if (!isUseDefaultFolders) {
            // ensure the user has selected a backup file
            Uri backupFileUri = getDataBackupFile(type);
            if (backupFileUri == null) {
                return ERROR_FILE_ACCESS;
            }
            ParcelFileDescriptor pfd = context.getContentResolver().openFileDescriptor(backupFileUri, "w");
            if (pfd == null) {
                return ERROR_FILE_ACCESS;
            }
            FileOutputStream out = new FileOutputStream(pfd.getFileDescriptor());
            if (type == BACKUP_SHOWS) {
                writeJsonStreamShows(out, data);
            } else if (type == BACKUP_LISTS) {
                writeJsonStreamLists(out, data);
            } else if (type == BACKUP_MOVIES) {
                writeJsonStreamMovies(out, data);
            }
            // let the document provider know we're done.
            pfd.close();
        } else {
            File backupFile;
            if (type == BACKUP_SHOWS) {
                backupFile = new File(exportPath, EXPORT_JSON_FILE_SHOWS);
            } else if (type == BACKUP_LISTS) {
                backupFile = new File(exportPath, EXPORT_JSON_FILE_LISTS);
            } else if (type == BACKUP_MOVIES) {
                backupFile = new File(exportPath, EXPORT_JSON_FILE_MOVIES);
            } else {
                return ERROR;
            }
            OutputStream out = new FileOutputStream(backupFile);
            if (type == BACKUP_SHOWS) {
                writeJsonStreamShows(out, data);
            } else if (type == BACKUP_LISTS) {
                writeJsonStreamLists(out, data);
            } else {
                writeJsonStreamMovies(out, data);
            }
        }
    } catch (FileNotFoundException e) {
        Timber.e(e, "Backup file not found.");
        removeBackupFileUri(type);
        return ERROR_FILE_ACCESS;
    } catch (IOException | SecurityException e) {
        Timber.e(e, "Could not access backup file.");
        removeBackupFileUri(type);
        return ERROR_FILE_ACCESS;
    } catch (JsonParseException e) {
        Timber.e(e, "JSON export failed.");
        return ERROR;
    } finally {
        data.close();
    }
    return SUCCESS;
}
Also used : FileOutputStream(java.io.FileOutputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) ParcelFileDescriptor(android.os.ParcelFileDescriptor) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) Cursor(android.database.Cursor) JsonParseException(com.google.gson.JsonParseException) Uri(android.net.Uri) File(java.io.File)

Example 3 with JsonParseException

use of com.google.gson.JsonParseException in project SeriesGuide by UweTrottmann.

the class JsonImportTask method importData.

private int importData(File importPath, @JsonExportTask.BackupType int type) {
    // so make sure to not fail just because a default folder file is missing
    if (!isUseDefaultFolders) {
        // make sure we have a file uri...
        Uri backupFileUri = getDataBackupFile(type);
        if (backupFileUri == null) {
            return ERROR_FILE_ACCESS;
        }
        // ...and the file actually exists
        ParcelFileDescriptor pfd;
        try {
            pfd = context.getContentResolver().openFileDescriptor(backupFileUri, "r");
        } catch (FileNotFoundException | SecurityException e) {
            Timber.e(e, "Backup file not found.");
            return ERROR_FILE_ACCESS;
        }
        if (pfd == null) {
            Timber.e("File descriptor is null.");
            return ERROR_FILE_ACCESS;
        }
        clearExistingData(type);
        // Access JSON from backup file and try to import data
        FileInputStream in = new FileInputStream(pfd.getFileDescriptor());
        try {
            importFromJson(type, in);
            // let the document provider know we're done.
            pfd.close();
        } catch (JsonParseException | IOException | IllegalStateException e) {
            // the given Json might not be valid or unreadable
            Timber.e(e, "JSON import failed");
            return ERROR;
        }
    } else {
        // make sure we can access the backup file
        File backupFile = null;
        if (type == JsonExportTask.BACKUP_SHOWS) {
            backupFile = new File(importPath, JsonExportTask.EXPORT_JSON_FILE_SHOWS);
        } else if (type == JsonExportTask.BACKUP_LISTS) {
            backupFile = new File(importPath, JsonExportTask.EXPORT_JSON_FILE_LISTS);
        } else if (type == JsonExportTask.BACKUP_MOVIES) {
            backupFile = new File(importPath, JsonExportTask.EXPORT_JSON_FILE_MOVIES);
        }
        if (backupFile == null || !backupFile.canRead()) {
            return ERROR_FILE_ACCESS;
        }
        if (!backupFile.exists()) {
            // no backup file, so nothing to restore, skip it
            return SUCCESS;
        }
        FileInputStream in;
        try {
            in = new FileInputStream(backupFile);
        } catch (FileNotFoundException e) {
            Timber.e(e, "Backup file not found.");
            return ERROR_FILE_ACCESS;
        }
        clearExistingData(type);
        // Access JSON from backup file and try to import data
        try {
            importFromJson(type, in);
        } catch (JsonParseException | IOException | IllegalStateException e) {
            // the given Json might not be valid or unreadable
            Timber.e(e, "JSON show import failed");
            return ERROR;
        }
    }
    return SUCCESS;
}
Also used : ParcelFileDescriptor(android.os.ParcelFileDescriptor) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) JsonParseException(com.google.gson.JsonParseException) Uri(android.net.Uri) File(java.io.File) FileInputStream(java.io.FileInputStream)

Example 4 with JsonParseException

use of com.google.gson.JsonParseException in project MinecraftForge by MinecraftForge.

the class MetadataCollection method from.

public static MetadataCollection from(@Nullable InputStream inputStream, String sourceName) {
    if (inputStream == null) {
        return new MetadataCollection();
    }
    InputStreamReader reader = new InputStreamReader(inputStream);
    try {
        MetadataCollection collection;
        Gson gson = new GsonBuilder().registerTypeAdapter(ArtifactVersion.class, new ArtifactVersionAdapter()).create();
        JsonParser parser = new JsonParser();
        JsonElement rootElement = parser.parse(reader);
        if (rootElement.isJsonArray()) {
            collection = new MetadataCollection();
            JsonArray jsonList = rootElement.getAsJsonArray();
            collection.modList = new ModMetadata[jsonList.size()];
            int i = 0;
            for (JsonElement mod : jsonList) {
                collection.modList[i++] = gson.fromJson(mod, ModMetadata.class);
            }
        } else {
            collection = gson.fromJson(rootElement, MetadataCollection.class);
        }
        collection.parseModMetadataList();
        return collection;
    } catch (JsonParseException e) {
        FMLLog.log(Level.ERROR, e, "The mcmod.info file in %s cannot be parsed as valid JSON. It will be ignored", sourceName);
        return new MetadataCollection();
    } catch (Exception e) {
        throw Throwables.propagate(e);
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) GsonBuilder(com.google.gson.GsonBuilder) Gson(com.google.gson.Gson) JsonParseException(com.google.gson.JsonParseException) JsonParseException(com.google.gson.JsonParseException) IOException(java.io.IOException) JsonArray(com.google.gson.JsonArray) ArtifactVersion(net.minecraftforge.fml.common.versioning.ArtifactVersion) JsonElement(com.google.gson.JsonElement) JsonParser(com.google.gson.JsonParser)

Example 5 with JsonParseException

use of com.google.gson.JsonParseException in project weave by continuuity.

the class LogEntryDecoder method deserialize.

@Override
public LogEntry deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
    if (!json.isJsonObject()) {
        return null;
    }
    JsonObject jsonObj = json.getAsJsonObject();
    final String name = JsonUtils.getAsString(jsonObj, "name");
    final String host = JsonUtils.getAsString(jsonObj, "host");
    final long timestamp = JsonUtils.getAsLong(jsonObj, "timestamp", 0);
    LogEntry.Level l;
    try {
        l = LogEntry.Level.valueOf(JsonUtils.getAsString(jsonObj, "level"));
    } catch (Exception e) {
        l = LogEntry.Level.FATAL;
    }
    final LogEntry.Level logLevel = l;
    final String className = JsonUtils.getAsString(jsonObj, "className");
    final String method = JsonUtils.getAsString(jsonObj, "method");
    final String file = JsonUtils.getAsString(jsonObj, "file");
    final String line = JsonUtils.getAsString(jsonObj, "line");
    final String thread = JsonUtils.getAsString(jsonObj, "thread");
    final String message = JsonUtils.getAsString(jsonObj, "message");
    final StackTraceElement[] stackTraces = context.deserialize(jsonObj.get("stackTraces").getAsJsonArray(), StackTraceElement[].class);
    return new LogEntry() {

        @Override
        public String getLoggerName() {
            return name;
        }

        @Override
        public String getHost() {
            return host;
        }

        @Override
        public long getTimestamp() {
            return timestamp;
        }

        @Override
        public Level getLogLevel() {
            return logLevel;
        }

        @Override
        public String getSourceClassName() {
            return className;
        }

        @Override
        public String getSourceMethodName() {
            return method;
        }

        @Override
        public String getFileName() {
            return file;
        }

        @Override
        public int getLineNumber() {
            if (line.equals("?")) {
                return -1;
            } else {
                return Integer.parseInt(line);
            }
        }

        @Override
        public String getThreadName() {
            return thread;
        }

        @Override
        public String getMessage() {
            return message;
        }

        @Override
        public StackTraceElement[] getStackTraces() {
            return stackTraces;
        }
    };
}
Also used : JsonObject(com.google.gson.JsonObject) LogEntry(com.continuuity.weave.api.logging.LogEntry) JsonParseException(com.google.gson.JsonParseException)

Aggregations

JsonParseException (com.google.gson.JsonParseException)229 JsonObject (com.google.gson.JsonObject)87 JsonElement (com.google.gson.JsonElement)61 IOException (java.io.IOException)61 Gson (com.google.gson.Gson)36 InputStreamReader (java.io.InputStreamReader)28 JsonArray (com.google.gson.JsonArray)23 JsonParser (com.google.gson.JsonParser)22 InputStream (java.io.InputStream)21 JsonPrimitive (com.google.gson.JsonPrimitive)20 JsonReader (com.google.gson.stream.JsonReader)19 Map (java.util.Map)19 GsonBuilder (com.google.gson.GsonBuilder)18 Type (java.lang.reflect.Type)17 ArrayList (java.util.ArrayList)16 HttpUrl (okhttp3.HttpUrl)13 Request (okhttp3.Request)13 Response (okhttp3.Response)13 JsonSyntaxException (com.google.gson.JsonSyntaxException)10 HashMap (java.util.HashMap)10