Example usage for com.google.gson.stream JsonReader hasNext

List of usage examples for com.google.gson.stream JsonReader hasNext

Introduction

In this page you can find the example usage for com.google.gson.stream JsonReader hasNext.

Prototype

public boolean hasNext() throws IOException 

Source Link

Document

Returns true if the current array or object has another element.

Usage

From source file:com.battlelancer.seriesguide.dataliberation.JsonImportTask.java

License:Apache License

private void importFromJson(@JsonExportTask.BackupType int type, FileInputStream in)
        throws JsonParseException, IOException, IllegalArgumentException {
    Gson gson = new Gson();
    JsonReader reader = new JsonReader(new InputStreamReader(in, "UTF-8"));
    reader.beginArray();/* w  w w. j  a  va  2 s  .co  m*/

    if (type == JsonExportTask.BACKUP_SHOWS) {
        while (reader.hasNext()) {
            Show show = gson.fromJson(reader, Show.class);
            addShowToDatabase(show);
        }
    } else if (type == JsonExportTask.BACKUP_LISTS) {
        while (reader.hasNext()) {
            List list = gson.fromJson(reader, List.class);
            addListToDatabase(list);
        }
    } else if (type == JsonExportTask.BACKUP_MOVIES) {
        while (reader.hasNext()) {
            Movie movie = gson.fromJson(reader, Movie.class);
            addMovieToDatabase(movie);
        }
    }

    reader.endArray();
    reader.close();
}

From source file:com.battlelancer.seriesguide.dataliberation.JsonImportTask.java

License:Apache License

private int importLists(File importPath) {
    File backupLists = new File(importPath, JsonExportTask.EXPORT_JSON_FILE_LISTS);
    if (!backupLists.exists() || !backupLists.canRead()) {
        // Skip lists if the file is not accessible
        return SUCCESS;
    }/*from   ww  w .  j  a v a 2s .com*/

    // Access JSON from backup folder to create new database
    try {
        InputStream in = new FileInputStream(backupLists);

        Gson gson = new Gson();

        JsonReader reader = new JsonReader(new InputStreamReader(in, "UTF-8"));
        reader.beginArray();

        while (reader.hasNext()) {
            List list = gson.fromJson(reader, List.class);
            addListToDatabase(list);
        }

        reader.endArray();
        reader.close();
    } catch (JsonParseException | IOException | IllegalStateException e) {
        // the given Json might not be valid or unreadable
        Timber.e(e, "JSON lists import failed");
        return ERROR;
    }

    return SUCCESS;
}

From source file:com.battlelancer.seriesguide.dataliberation.JsonImportTask.java

License:Apache License

private int importMovies(File importPath) {
    context.getContentResolver().delete(Movies.CONTENT_URI, null, null);
    File backupMovies = new File(importPath, JsonExportTask.EXPORT_JSON_FILE_MOVIES);
    if (!backupMovies.exists() || !backupMovies.canRead()) {
        // Skip movies if the file is not available
        return SUCCESS;
    }//from  w  w  w.ja v a2 s.  c o m

    // Access JSON from backup folder to create new database
    try {
        InputStream in = new FileInputStream(backupMovies);

        Gson gson = new Gson();

        JsonReader reader = new JsonReader(new InputStreamReader(in, "UTF-8"));
        reader.beginArray();

        while (reader.hasNext()) {
            Movie movie = gson.fromJson(reader, Movie.class);
            addMovieToDatabase(movie);
        }

        reader.endArray();
        reader.close();
    } catch (JsonParseException | IOException | IllegalStateException e) {
        // the given Json might not be valid or unreadable
        Timber.e(e, "JSON movies import failed");
        return ERROR;
    }

    return SUCCESS;
}

From source file:com.bzcentre.dapiPush.ReceipientTypeAdapter.java

License:Open Source License

@Override
public Receipient read(JsonReader in)
        throws IOException, IllegalStateException, JsonParseException, NumberFormatException {
    NginxClojureRT.log.debug(TAG + " invoked...");
    Receipient receipient = new Receipient();
    if (in.peek() == JsonToken.NULL) {
        in.nextNull();/*from w  w  w.  j  av a  2 s  . c o  m*/
        return null;
    }

    in.beginObject();
    while (in.hasNext()) {
        switch (in.nextName()) {
        case "apns_token":
            if (in.peek() == JsonToken.NULL) {
                in.nextNull();
            } else
                receipient.setApns_Token(in.nextString());
            break;
        case "fcm_token":
            if (in.peek() == JsonToken.NULL) {
                in.nextNull();
            } else
                receipient.setFcm_Token(in.nextString());
            break;
        case "payload":
            receipient.setPayload(extractPayload(in));
            break;
        }
    }
    in.endObject();

    NginxClojureRT.log.debug(TAG + "Deserializing and adding receipient of "
            + (receipient.getApns_Token() == null ? "fcm--" + receipient.getFcm_Token()
                    : "apns--" + receipient.getApns_Token()));
    return receipient;
}

From source file:com.bzcentre.dapiPush.ReceipientTypeAdapter.java

License:Open Source License

private MeetingPayload extractPayload(JsonReader in)
        throws IOException, NumberFormatException, IllegalStateException, JsonParseException {
    NginxClojureRT.log.debug(TAG + "TypeAdapter extracting Payload...");

    MeetingPayload meetingPayload = new MeetingPayload();
    if (in.peek() == JsonToken.NULL) {
        in.nextNull();//w w w.j av a  2 s  .  c o m
        throw new JsonParseException("null Payload");
    }

    in.beginObject();
    while (in.hasNext()) {
        switch (in.nextName()) {
        case "aps":
            in.beginObject();
            while (in.hasNext()) {
                switch (in.nextName()) {
                case "badge":
                    meetingPayload.getAps().setBadge(in.nextLong());
                    break;
                case "sound":
                    meetingPayload.getAps().setSound(in.nextString());
                    break;
                case "alert":
                    in.beginObject();
                    while (in.hasNext()) {
                        switch (in.nextName()) {
                        case "title":
                            meetingPayload.getAps().getAlert().setTitle(in.nextString());
                            break;
                        case "body":
                            meetingPayload.getAps().getAlert().setBody(in.nextString());
                            break;
                        case "action-loc-key":
                            meetingPayload.getAps().getAlert().setActionLocKey(in.nextString());
                            break;
                        }
                    }
                    in.endObject();
                    break;
                }
            }
            in.endObject();
            break;
        case "dapi":
            meetingPayload.setDapi(in.nextString());
            break;
        case "acme1":
            meetingPayload.setAcme1(in.nextString());
            break;
        case "acme2":
            meetingPayload.setAcme2(in.nextLong());
            break;
        case "acme3":
            meetingPayload.setAcme3(in.nextLong());
            break;
        case "acme4":
            NginxClojureRT.log.info(TAG + "TypeAdapter Reader is reading acme4...");
            meetingPayload.setAcme4(in.nextLong());
            break;
        case "acme5":
            meetingPayload.setAcme5(in.nextLong());
            break;
        case "acme6":
            meetingPayload.setAcme6(in.nextLong());
            break;
        case "acme7":
            ArrayList<String> attendees = new ArrayList<>();
            in.beginArray();
            while (in.hasNext()) {
                attendees.add(in.nextString());
            }
            in.endArray();
            meetingPayload.setAcme7(attendees);
            break;
        case "acme8":
            meetingPayload.setAcme8(in.nextString());
            break;
        }
    }
    in.endObject();

    return meetingPayload;
}

From source file:com.dabay6.libraries.androidshared.ui.dialogs.changelog.util.ChangeLogDialogUtils.java

License:Open Source License

/**
 * Builds the html using the change log json asset file.
 *
 * @param context   {@link Context} used to retrieve resources.
 * @param assetName Name of the asset file containing the change log json.
 * @param style     The css style to be applied to the html.
 *
 * @return A {@link String} containing html.
 *///w  ww.j ava 2 s  .c o m
private static String getHtmlChangeLog(final Context context, final String assetName, final String style)
        throws IOException {
    final JsonReader reader;
    final Gson gson = new Gson();
    final List<Release> releases = ListUtils.newList();
    String html = null;

    try {
        reader = new JsonReader(new InputStreamReader(AssetUtils.open(context, assetName), "UTF-8"));
        reader.beginArray();

        while (reader.hasNext()) {
            final Release release = gson.fromJson(reader, Release.class);

            releases.add(release);
        }

        reader.endArray();
        reader.close();

        if (releases.size() > 0) {
            html = String.format("<html><head>%s</head><body>", style);
            html += parseReleases(context, releases);
            html += "</body></html>";
        }
    } catch (final Exception ex) {
        html = null;

        Logger.error(TAG, ex.getMessage(), ex);
    }

    return html;
}

From source file:com.dangdang.ddframe.job.api.config.impl.AbstractJobConfigurationGsonTypeAdapter.java

License:Apache License

@Override
public T read(final JsonReader in) throws IOException {
    String jobName = "";
    String cron = "";
    int shardingTotalCount = 0;
    String shardingItemParameters = "";
    String jobParameter = "";
    boolean failover = false;
    boolean misfire = failover;
    String description = "";
    JobProperties jobProperties = new JobProperties();
    JobEventConfiguration[] jobEventConfigs = null;
    JobType jobType = null;/*from  w ww .  j  av  a  2s .co m*/
    String jobClass = "";
    DataflowJobConfiguration.DataflowType dataflowType = null;
    boolean streamingProcess = false;
    int concurrentDataProcessThreadCount = 0;
    String scriptCommandLine = "";
    Map<String, Object> customizedValueMap = new HashMap<>(32, 1);
    in.beginObject();
    while (in.hasNext()) {
        String jsonName = in.nextName();
        switch (jsonName) {
        case "jobName":
            jobName = in.nextString();
            break;
        case "cron":
            cron = in.nextString();
            break;
        case "shardingTotalCount":
            shardingTotalCount = in.nextInt();
            break;
        case "shardingItemParameters":
            shardingItemParameters = in.nextString();
            break;
        case "jobParameter":
            jobParameter = in.nextString();
            break;
        case "failover":
            failover = in.nextBoolean();
            break;
        case "misfire":
            misfire = in.nextBoolean();
            break;
        case "description":
            description = in.nextString();
            break;
        case "jobProperties":
            jobProperties = getJobProperties(in);
            break;
        case "jobEventConfigs":
            jobEventConfigs = getJobEventConfigs(in);
            break;
        case "jobType":
            jobType = JobType.valueOf(in.nextString());
            break;
        case "jobClass":
            jobClass = in.nextString();
            break;
        case "dataflowType":
            dataflowType = DataflowJobConfiguration.DataflowType.valueOf(in.nextString());
            break;
        case "streamingProcess":
            streamingProcess = in.nextBoolean();
            break;
        case "concurrentDataProcessThreadCount":
            concurrentDataProcessThreadCount = in.nextInt();
            break;
        case "scriptCommandLine":
            scriptCommandLine = in.nextString();
            break;
        default:
            addToCustomizedValueMap(jsonName, in, customizedValueMap);
            break;
        }
    }
    in.endObject();
    JobCoreConfiguration coreConfig = getJobCoreConfiguration(jobName, cron, shardingTotalCount,
            shardingItemParameters, jobParameter, failover, misfire, description, jobProperties,
            jobEventConfigs);
    JobTypeConfiguration typeConfig = getJobTypeConfiguration(coreConfig, jobType, jobClass, dataflowType,
            streamingProcess, concurrentDataProcessThreadCount, scriptCommandLine);
    return getJobRootConfiguration(typeConfig, customizedValueMap);
}

From source file:com.dangdang.ddframe.job.api.config.impl.AbstractJobConfigurationGsonTypeAdapter.java

License:Apache License

private JobProperties getJobProperties(final JsonReader in) throws IOException {
    JobProperties result = new JobProperties();
    in.beginObject();//from ww  w.  ja v  a2  s .c om
    while (in.hasNext()) {
        switch (in.nextName()) {
        case "job_exception_handler":
            result.put(JobProperties.JobPropertiesEnum.JOB_EXCEPTION_HANDLER.getKey(), in.nextString());
            break;
        case "executor_service_handler":
            result.put(JobProperties.JobPropertiesEnum.EXECUTOR_SERVICE_HANDLER.getKey(), in.nextString());
            break;
        default:
            break;
        }
    }
    in.endObject();
    return result;
}

From source file:com.dangdang.ddframe.job.api.config.impl.AbstractJobConfigurationGsonTypeAdapter.java

License:Apache License

private JobEventConfiguration[] getJobEventConfigs(final JsonReader in) throws IOException {
    List<JobEventConfiguration> result = new ArrayList<>(2);
    in.beginObject();// w  ww  .ja  v a2 s.com
    while (in.hasNext()) {
        String name = in.nextName();
        switch (name) {
        case "log":
            in.beginObject();
            result.add(new JobLogEventConfiguration());
            in.endObject();
            break;
        case "rdb":
            String url = "";
            String username = "";
            String password = "";
            String driverClassName = "";
            String logLevel = "";
            in.beginObject();
            while (in.hasNext()) {
                switch (in.nextName()) {
                case "url":
                    url = in.nextString();
                    break;
                case "username":
                    username = in.nextString();
                    break;
                case "password":
                    password = in.nextString();
                    break;
                case "driverClassName":
                    driverClassName = in.nextString();
                    break;
                case "logLevel":
                    logLevel = in.nextString();
                    break;
                default:
                    break;
                }
            }
            in.endObject();
            result.add(new JobRdbEventConfiguration(driverClassName, url, username, password,
                    LogLevel.valueOf(logLevel.toUpperCase())));
            break;
        default:
            break;
        }
    }
    in.endObject();
    return Iterables.toArray(result, JobEventConfiguration.class);
}

From source file:com.dangdang.ddframe.job.util.AbstractJobConfigurationGsonTypeAdapter.java

License:Apache License

@Override
public T read(final JsonReader in) throws IOException {
    String jobName = "";
    String cron = "";
    int shardingTotalCount = 0;
    String shardingItemParameters = "";
    String jobParameter = "";
    boolean failover = false;
    boolean misfire = failover;
    String description = "";
    JobProperties jobProperties = new JobProperties();
    JobEventConfiguration[] jobEventConfigs = null;
    JobType jobType = null;//  w w w. ja  v  a2  s  .c  o  m
    String jobClass = "";
    boolean streamingProcess = false;
    String scriptCommandLine = "";
    Map<String, Object> customizedValueMap = new HashMap<>(32, 1);
    in.beginObject();
    while (in.hasNext()) {
        String jsonName = in.nextName();
        switch (jsonName) {
        case "jobName":
            jobName = in.nextString();
            break;
        case "cron":
            cron = in.nextString();
            break;
        case "shardingTotalCount":
            shardingTotalCount = in.nextInt();
            break;
        case "shardingItemParameters":
            shardingItemParameters = in.nextString();
            break;
        case "jobParameter":
            jobParameter = in.nextString();
            break;
        case "failover":
            failover = in.nextBoolean();
            break;
        case "misfire":
            misfire = in.nextBoolean();
            break;
        case "description":
            description = in.nextString();
            break;
        case "jobProperties":
            jobProperties = getJobProperties(in);
            break;
        case "jobEventConfigs":
            jobEventConfigs = getJobEventConfigs(in);
            break;
        case "jobType":
            jobType = JobType.valueOf(in.nextString());
            break;
        case "jobClass":
            jobClass = in.nextString();
            break;
        case "streamingProcess":
            streamingProcess = in.nextBoolean();
            break;
        case "scriptCommandLine":
            scriptCommandLine = in.nextString();
            break;
        default:
            addToCustomizedValueMap(jsonName, in, customizedValueMap);
            break;
        }
    }
    in.endObject();
    JobCoreConfiguration coreConfig = getJobCoreConfiguration(jobName, cron, shardingTotalCount,
            shardingItemParameters, jobParameter, failover, misfire, description, jobProperties,
            jobEventConfigs);
    JobTypeConfiguration typeConfig = getJobTypeConfiguration(coreConfig, jobType, jobClass, streamingProcess,
            scriptCommandLine);
    return getJobRootConfiguration(typeConfig, customizedValueMap);
}