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

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

Introduction

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

Prototype

public String nextName() throws IOException 

Source Link

Document

Returns the next token, a com.google.gson.stream.JsonToken#NAME property name , and consumes it.

Usage

From source file:ch.cyberduck.core.importer.ExpandriveBookmarkCollection.java

License:Open Source License

@Override
protected void parse(final Local file) throws AccessDeniedException {
    try {/*from  w  ww . j a va  2s . c  om*/
        final JsonReader reader = new JsonReader(new InputStreamReader(file.getInputStream(), "UTF-8"));
        reader.beginArray();
        while (reader.hasNext()) {
            reader.beginObject();
            final Host current = new Host(new FTPProtocol(),
                    PreferencesFactory.get().getProperty("connection.hostname.default"));
            while (reader.hasNext()) {
                final String name = reader.nextName();
                switch (name) {
                case "server":
                    current.setHostname(reader.nextString());
                    break;
                case "username":
                    current.getCredentials().setUsername(reader.nextString());
                    break;
                case "private_key_file":
                    current.getCredentials().setIdentity(LocalFactory.get(reader.nextString()));
                    break;
                case "remotePath":
                    current.setDefaultPath(reader.nextString());
                    break;
                case "type":
                    final Protocol type = ProtocolFactory.forName(reader.nextString());
                    if (null != type) {
                        current.setProtocol(type);
                    }
                    break;
                case "protocol":
                    final Protocol protocol = ProtocolFactory.forName(reader.nextString());
                    if (null != protocol) {
                        current.setProtocol(protocol);
                        // Reset port to default
                        current.setPort(-1);
                    }
                    break;
                case "name":
                    current.setNickname(reader.nextString());
                    break;
                case "region":
                    current.setRegion(reader.nextString());
                    break;
                default:
                    log.warn(String.format("Ignore property %s", name));
                    reader.skipValue();
                    break;
                }
            }
            reader.endObject();
            this.add(current);
        }
        reader.endArray();
    } catch (IllegalStateException | IOException e) {
        throw new LocalAccessDeniedException(e.getMessage(), e);
    }
}

From source file:ch.cyberduck.core.importer.NetDrive2BookmarkCollection.java

License:Open Source License

@Override
protected void parse(final ProtocolFactory protocols, final Local file) throws AccessDeniedException {
    try {/*from w  w w. java2  s .  c  om*/
        final JsonReader reader = new JsonReader(new InputStreamReader(file.getInputStream(), "UTF-8"));
        reader.beginArray();
        String url;
        String user;
        boolean ssl;
        Protocol protocol;
        while (reader.hasNext()) {
            reader.beginObject();
            boolean skip = false;
            url = null;
            ssl = false;
            protocol = null;
            user = null;
            while (reader.hasNext()) {
                final String name = reader.nextName();
                switch (name) {
                case "url":
                    url = this.readNext(name, reader);
                    if (StringUtils.isBlank(url)) {
                        skip = true;
                    }
                    break;
                case "ssl":
                    ssl = reader.nextBoolean();
                    break;
                case "user":
                    user = this.readNext(name, reader);
                    break;
                case "type":
                    final String type = this.readNext(name, reader);
                    switch (type) {
                    case "google_cloud_storage":
                        protocol = protocols.forType(Protocol.Type.googlestorage);
                        break;
                    case "gdrive":
                        protocol = protocols.forType(Protocol.Type.googledrive);
                        break;
                    default:
                        protocol = protocols.forName(type);
                    }
                    break;

                default:
                    log.warn(String.format("Ignore property %s", name));
                    reader.skipValue();
                    break;
                }
            }
            reader.endObject();
            if (!skip && protocol != null && StringUtils.isNotBlank(user)) {
                if (ssl) {
                    switch (protocol.getType()) {
                    case ftp:
                        protocol = protocols.forScheme(Scheme.ftps);
                        break;
                    case dav:
                        protocol = protocols.forScheme(Scheme.davs);
                        break;
                    }
                }
                this.add(HostParser.parse(protocols, protocol, url));
            }
        }
        reader.endArray();
    } catch (IllegalStateException | IOException e) {
        throw new LocalAccessDeniedException(e.getMessage(), e);
    }
}

From source file:ch.cyberduck.core.s3.S3SessionCredentialsRetriever.java

License:Open Source License

protected AWSCredentials parse(final InputStream in) throws BackgroundException {
    try {//ww w. j ava  2s .  com
        final JsonReader reader = new JsonReader(new InputStreamReader(in, "UTF-8"));
        reader.beginObject();
        String key = null;
        String secret = null;
        String token = null;
        while (reader.hasNext()) {
            final String name = reader.nextName();
            final String value = reader.nextString();
            switch (name) {
            case "AccessKeyId":
                key = value;
                break;
            case "SecretAccessKey":
                secret = value;
                break;
            case "Token":
                token = value;
                break;
            }
        }
        reader.endObject();
        return new AWSSessionCredentials(key, secret, token);
    } catch (UnsupportedEncodingException e) {
        throw new BackgroundException(e);
    } catch (MalformedJsonException e) {
        throw new InteroperabilityException("Invalid JSON response", e);
    } catch (IOException e) {
        throw new DefaultIOExceptionMappingService().map(e);
    }
}

From source file:classifiers.DummyClassifier.java

License:Apache License

public void parseStreamAndClassify(String jsonFile, String resultsFile) throws IOException {

    String journalName;/*ww w. j a v  a  2 s.c om*/
    int count = 0;
    int abstract_count = 0;

    try {
        JsonReader reader = new JsonReader(new InputStreamReader(new FileInputStream(jsonFile)));
        JsonWriter writer = new JsonWriter(new OutputStreamWriter(new FileOutputStream(resultsFile), "UTF-8"));
        writer.setIndent("    ");

        //reader.setLenient(true);
        reader.beginArray();
        writer.beginArray();
        while (reader.hasNext()) {

            reader.beginObject();
            writer.beginObject();
            while (reader.hasNext()) {
                String name = reader.nextName();

                if (name.equals("abstract")) {
                    abstract_count++;
                    reader.skipValue();

                } else if (name.equals("pmid")) {
                    String pmid = reader.nextString();
                    writer.name("labels");
                    writeLabels(writer);
                    writer.name("pmid").value(pmid);
                } else if (name.equals("title")) {
                    reader.skipValue();
                } else {
                    System.out.println(name);
                    reader.skipValue();
                }
            }
            reader.endObject();
            writer.endObject();
        }
        reader.endArray();
        writer.endArray();

        System.out.println("Abstracts: " + abstract_count);

        writer.close();
    } catch (FileNotFoundException ex) {

    }
}

From source file:co.cask.cdap.common.stream.StreamEventTypeAdapter.java

License:Apache License

@Override
public StreamEvent read(JsonReader in) throws IOException {
    long timestamp = -1;
    Map<String, String> headers = null;
    ByteBuffer body = null;//from   w  w  w  .j  av a2  s.c om

    in.beginObject();
    while (in.peek() == JsonToken.NAME) {
        String key = in.nextName();
        if ("timestamp".equals(key)) {
            timestamp = in.nextLong();
        } else if ("headers".equals(key)) {
            headers = mapTypeAdapter.read(in);
        } else if ("body".equals(key)) {
            body = ByteBuffer.wrap(Bytes.toBytesBinary(in.nextString()));
        } else {
            in.skipValue();
        }
    }

    if (timestamp >= 0 && headers != null && body != null) {
        in.endObject();
        return new StreamEvent(headers, body, timestamp);
    }
    throw new IOException(String.format("Failed to read StreamEvent. Timestamp: %d, headers: %s, body: %s",
            timestamp, headers, body));
}

From source file:co.cask.cdap.format.StructuredRecordStringConverter.java

License:Apache License

private static Map<Object, Object> readMap(JsonReader reader, Map.Entry<Schema, Schema> mapSchema)
        throws IOException {
    Schema keySchema = mapSchema.getKey();
    if (!keySchema.isCompatible(Schema.of(Schema.Type.STRING))) {
        throw new IOException("Complex key type not supported: " + keySchema);
    }//  w w  w  .ja va 2 s .  co  m

    Schema valueSchema = mapSchema.getValue();
    Map<Object, Object> result = new HashMap<>();

    reader.beginObject();
    while (reader.peek() != JsonToken.END_OBJECT) {
        Object key = convertKey(reader.nextName(), keySchema.getType());
        result.put(key, readJson(reader, valueSchema));
    }
    reader.endObject();

    return result;
}

From source file:co.cask.cdap.format.StructuredRecordStringConverter.java

License:Apache License

private static StructuredRecord readRecord(JsonReader reader, Schema schema) throws IOException {
    StructuredRecord.Builder builder = StructuredRecord.builder(schema);

    reader.beginObject();/*from   w  w  w  .j a va2 s . c o  m*/
    while (reader.peek() != JsonToken.END_OBJECT) {
        Schema.Field field = schema.getField(reader.nextName());
        if (field == null) {
            // Ignore unrecognized fields
            reader.skipValue();
            continue;
        }

        builder.set(field.getName(), readJson(reader, field.getSchema()));
    }
    reader.endObject();

    return builder.build();
}

From source file:co.cask.cdap.internal.io.SchemaTypeAdapter.java

License:Apache License

/**
 * Reads json value and convert it into {@link Schema} object.
 *
 * @param reader Source of json/*from  w w  w  .  j  a v  a2 s. com*/
 * @param knownRecords Set of record name already encountered during the reading.
 * @return A {@link Schema} reflecting the json.
 * @throws IOException Any error during reading.
 */
private Schema read(JsonReader reader, Map<String, Schema> knownRecords) throws IOException {
    JsonToken token = reader.peek();
    switch (token) {
    case NULL:
        return null;
    case STRING: {
        // Simple type or know record type
        String name = reader.nextString();
        if (knownRecords.containsKey(name)) {
            Schema schema = knownRecords.get(name);
            /*
               schema is null and in the map if this is a recursive reference. For example,
               if we're looking at the inner 'node' record in the example below:
               {
                 "type": "record",
                 "name": "node",
                 "fields": [{
                   "name": "children",
                   "type": [{
                     "type": "array",
                     "items": ["node", "null"]
                   }, "null"]
                 }, {
                   "name": "data",
                   "type": "int"
                 }]
               }
             */
            return schema == null ? Schema.recordOf(name) : schema;
        }
        return Schema.of(Schema.Type.valueOf(name.toUpperCase()));
    }
    case BEGIN_ARRAY:
        // Union type
        return readUnion(reader, knownRecords);
    case BEGIN_OBJECT: {
        reader.beginObject();
        String name = reader.nextName();
        if (!"type".equals(name)) {
            throw new IOException("Property \"type\" missing.");
        }
        Schema.Type schemaType = Schema.Type.valueOf(reader.nextString().toUpperCase());

        Schema schema;
        switch (schemaType) {
        case ENUM:
            schema = readEnum(reader);
            break;
        case ARRAY:
            schema = readArray(reader, knownRecords);
            break;
        case MAP:
            schema = readMap(reader, knownRecords);
            break;
        case RECORD:
            schema = readRecord(reader, knownRecords);
            break;
        default:
            schema = Schema.of(schemaType);
        }
        reader.endObject();
        return schema;
    }
    }
    throw new IOException("Malformed schema input.");
}

From source file:co.cask.cdap.internal.io.SchemaTypeAdapter.java

License:Apache License

/**
 * Constructs {@link Schema.Type#ENUM ENUM} type schema from the json input.
 *
 * @param reader The {@link JsonReader} for streaming json input tokens.
 * @return A {@link Schema} of type {@link Schema.Type#ENUM ENUM}.
 * @throws IOException When fails to construct a valid schema from the input.
 *///from w  w  w.ja  v  a2s .c  o  m
private Schema readEnum(JsonReader reader) throws IOException {
    if (!"symbols".equals(reader.nextName())) {
        throw new IOException("Property \"symbols\" missing for enum.");
    }
    List<String> enumValues = new ArrayList<>();
    reader.beginArray();
    while (reader.peek() != JsonToken.END_ARRAY) {
        enumValues.add(reader.nextString());
    }
    reader.endArray();
    return Schema.enumWith(enumValues);
}

From source file:co.cask.cdap.internal.io.SchemaTypeAdapter.java

License:Apache License

/**
 * Constructs {@link Schema.Type#RECORD RECORD} type schema from the json input.
 *
 * @param reader The {@link JsonReader} for streaming json input tokens.
 * @param knownRecords Set of record name already encountered during the reading.
 * @return A {@link Schema} of type {@link Schema.Type#RECORD RECORD}.
 * @throws IOException When fails to construct a valid schema from the input.
 *//*from  w ww. j ava 2 s.c  o m*/
private Schema readRecord(JsonReader reader, Map<String, Schema> knownRecords) throws IOException {
    if (!"name".equals(reader.nextName())) {
        throw new IOException("Property \"name\" missing for record.");
    }

    String recordName = reader.nextString();

    // Read in fields schemas
    if (!"fields".equals(reader.nextName())) {
        throw new IOException("Property \"fields\" missing for record.");
    }

    /*
      put a null schema schema is null and in the map if this is a recursive reference.
      for example, if we are looking at the outer 'node' reference in the example below,
      when we get to the inner 'node' reference, we need some way to know that its a record type
      and not a Schema.Type.
      {
        "type": "record",
        "name": "node",
        "fields": [{
          "name": "children",
          "type": [{
    "type": "array",
    "items": ["node", "null"]
          }, "null"]
        }, {
          "name": "data",
          "type": "int"
        }]
      }
      the full schema will be put in at the end of this method
    */
    knownRecords.put(recordName, null);

    List<Schema.Field> fieldBuilder = new ArrayList<>();
    reader.beginArray();
    while (reader.peek() != JsonToken.END_ARRAY) {
        reader.beginObject();
        if (!"name".equals(reader.nextName())) {
            throw new IOException("Property \"name\" missing for record field.");
        }
        String fieldName = reader.nextString();
        fieldBuilder.add(Schema.Field.of(fieldName, readInnerSchema(reader, "type", knownRecords)));
        reader.endObject();
    }
    reader.endArray();
    Schema schema = Schema.recordOf(recordName, fieldBuilder);
    knownRecords.put(recordName, schema);
    return schema;
}