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:org.apache.axis2.json.gson.rpc.JsonUtils.java

License:Apache License

public static Object invokeServiceClass(JsonReader jsonReader, Object service, Method operation,
        Class[] paramClasses, int paramCount)
        throws InvocationTargetException, IllegalAccessException, IOException {

    Object[] methodParam = new Object[paramCount];
    Gson gson = new Gson();
    String[] argNames = new String[paramCount];

    if (!jsonReader.isLenient()) {
        jsonReader.setLenient(true);/*w w  w  . j ava  2  s .com*/
    }
    jsonReader.beginObject();
    jsonReader.nextName(); // get message name from input json stream
    jsonReader.beginArray();

    int i = 0;
    for (Class paramType : paramClasses) {
        jsonReader.beginObject();
        argNames[i] = jsonReader.nextName();
        methodParam[i] = gson.fromJson(jsonReader, paramType); // gson handle all types well and return an object from it
        jsonReader.endObject();
        i++;
    }

    jsonReader.endArray();
    jsonReader.endObject();

    return operation.invoke(service, methodParam);

}

From source file:org.apache.carbondata.sdk.file.Schema.java

License:Apache License

/**
 * Create a Schema using JSON string, for example:
 * [// w w  w .jav a2s  .c  om
 *   {"name":"string"},
 *   {"age":"int"}
 * ]
 * @param json specified as string
 * @return Schema
 */
public static Schema parseJson(String json) {
    GsonBuilder gsonBuilder = new GsonBuilder();
    gsonBuilder.registerTypeAdapter(Field.class, new TypeAdapter<Field>() {
        @Override
        public void write(JsonWriter out, Field field) throws IOException {
            // noop
        }

        @Override
        public Field read(JsonReader in) throws IOException {
            in.beginObject();
            Field field = new Field(in.nextName(), in.nextString());
            in.endObject();
            return field;
        }
    });

    Field[] fields = gsonBuilder.create().fromJson(json, Field[].class);
    return new Schema(fields);
}

From source file:org.apache.hadoop.dynamodb.importformat.ImportInputFormat.java

License:Open Source License

/**
 * An example manifest file looks like/*from   www . j a va 2 s  . c  om*/
 *
 * {"name":"DynamoDB-export","version":3, "entries":[
 * {"url":"s3://path/to/object/92dd1414-a049-4c68-88fb-a23acd44907e","mandatory":true},
 * {"url":"s3://path/to/object/ba3f3535-7aa1-4f97-a530-e72938bf4b76","mandatory":true} ]}
 */
// @formatter:on
private List<InputSplit> parseManifest(FileSystem fs, Path manifestPath, JobConf job) throws IOException {
    List<InputSplit> splits = null;

    FSDataInputStream fp = fs.open(manifestPath);
    JsonReader reader = new JsonReader(new InputStreamReader(fp, Charsets.UTF_8));

    reader.beginObject();
    while (reader.hasNext()) {
        String name = reader.nextName();
        switch (name) {
        case VERSION_JSON_KEY:
            job.set(DynamoDBConstants.EXPORT_FORMAT_VERSION, String.valueOf(reader.nextInt()));
            break;
        case ENTRIES_JSON_KEY:
            splits = readEntries(reader, job);
            break;
        default:
            log.info("Skipping a JSON key in the manifest file: " + name);
            reader.skipValue();
            break;
        }
    }
    reader.endObject();

    if (splits == null) {
        return Collections.emptyList();
    }
    return splits;
}

From source file:org.apache.hadoop.fs.http.client.ContentSummary.java

License:Apache License

public static TypeAdapter adapter() {
    return new TypeAdapter<ContentSummary>() {
        @Override/*  w ww  .j av  a2s  .c o  m*/
        public void write(JsonWriter out, ContentSummary value) throws IOException {
            /* not implemented */
        }

        @Override
        public ContentSummary read(JsonReader in) throws IOException {
            ContentSummary instance = null;
            in.setLenient(true);

            if (in.peek() == JsonToken.BEGIN_OBJECT) {
                in.beginObject();

                if (in.nextName().equalsIgnoreCase("ContentSummary")) {
                    String name;
                    in.beginObject();
                    instance = new ContentSummary();

                    while (in.hasNext()) {
                        name = in.nextName();

                        if (name.equalsIgnoreCase("directoryCount")) {
                            instance.directoryCount = in.nextInt();
                        } else if (name.equalsIgnoreCase("fileCount")) {
                            instance.fileCount = in.nextInt();
                        } else if (name.equalsIgnoreCase("length")) {
                            instance.length = in.nextInt();
                        } else if (name.equalsIgnoreCase("quota")) {
                            instance.quota = in.nextInt();
                        } else if (name.equalsIgnoreCase("spaceConsumed")) {
                            instance.spaceConsumed = in.nextInt();
                        } else if (name.equalsIgnoreCase("spaceQuota")) {
                            instance.spaceQuota = in.nextInt();
                        }
                    }

                    in.endObject();
                }

                in.endObject();
            }
            return instance;
        }
    };
}

From source file:org.apache.hadoop.fs.http.client.FileStatus.java

License:Apache License

public static TypeAdapter adapter() {
    return new TypeAdapter<FileStatus>() {
        @Override// ww w .j av a 2 s.c o  m
        public void write(JsonWriter out, FileStatus value) throws IOException {
            /* not implemented */
        }

        @Override
        public FileStatus read(JsonReader in) throws IOException {
            FileStatus instance = null;
            in.setLenient(true);

            if (in.peek() == JsonToken.BEGIN_OBJECT) {
                in.beginObject();

                if (in.nextName().equalsIgnoreCase("FileStatus")) {
                    String name;
                    in.beginObject();
                    instance = new FileStatus();

                    while (in.hasNext()) {
                        name = in.nextName();

                        if (name.equalsIgnoreCase("accessTime")) {
                            instance.accessTime = in.nextLong();
                        } else if (name.equalsIgnoreCase("blockSize")) {
                            instance.blockSize = in.nextInt();
                        } else if (name.equalsIgnoreCase("length")) {
                            instance.length = in.nextLong();
                        } else if (name.equalsIgnoreCase("modificationTime")) {
                            instance.modTime = in.nextLong();
                        } else if (name.equalsIgnoreCase("replication")) {
                            instance.replication = in.nextInt();
                        } else if (name.equalsIgnoreCase("group")) {
                            instance.group = in.nextString();
                        } else if (name.equalsIgnoreCase("owner")) {
                            instance.owner = in.nextString();
                        } else if (name.equalsIgnoreCase("pathSuffix")) {
                            instance.suffix = in.nextString();
                        } else if (name.equalsIgnoreCase("permission")) {
                            instance.permission = in.nextString();
                        } else if (name.equalsIgnoreCase("type")) {
                            instance.type = FileType.valueOf(in.nextString());
                        }
                    }

                    in.endObject();
                }

                in.endObject();
            }
            return instance;
        }
    };
}

From source file:org.apache.olingo.odata2.core.ep.consumer.JsonErrorDocumentConsumer.java

License:Apache License

private ODataErrorContext parseJson(final JsonReader reader) throws IOException, EntityProviderException {
    ODataErrorContext errorContext;/*from   w  ww  .ja  v  a  2  s. c  om*/

    if (reader.hasNext()) {
        reader.beginObject();
        String currentName = reader.nextName();
        if (FormatJson.ERROR.equals(currentName)) {
            errorContext = parseError(reader);
        } else {
            throw new EntityProviderException(EntityProviderException.INVALID_STATE
                    .addContent("Invalid object with name '" + currentName + "' found."));
        }
    } else {
        throw new EntityProviderException(
                EntityProviderException.INVALID_STATE.addContent("No content to parse found."));
    }

    errorContext.setContentType(ContentType.APPLICATION_JSON.toContentTypeString());
    return errorContext;
}

From source file:org.apache.olingo.odata2.core.ep.consumer.JsonErrorDocumentConsumer.java

License:Apache License

private ODataErrorContext parseError(final JsonReader reader) throws IOException, EntityProviderException {
    ODataErrorContext errorContext = new ODataErrorContext();
    String currentName;/*from   w ww  .jav a  2s.c  om*/
    reader.beginObject();
    boolean messageFound = false;
    boolean codeFound = false;

    while (reader.hasNext()) {
        currentName = reader.nextName();
        if (FormatJson.CODE.equals(currentName)) {
            codeFound = true;
            errorContext.setErrorCode(getValue(reader));
        } else if (FormatJson.MESSAGE.equals(currentName)) {
            messageFound = true;
            parseMessage(reader, errorContext);
        } else if (FormatJson.INNER_ERROR.equals(currentName)) {
            parseInnerError(reader, errorContext);
        } else {
            throw new EntityProviderException(EntityProviderException.INVALID_STATE
                    .addContent("Invalid name '" + currentName + "' found."));
        }
    }

    if (!codeFound) {
        throw new EntityProviderException(
                EntityProviderException.MISSING_PROPERTY.addContent("Mandatory 'code' property not found.'"));
    }
    if (!messageFound) {
        throw new EntityProviderException(EntityProviderException.MISSING_PROPERTY
                .addContent("Mandatory 'message' property not found.'"));
    }

    reader.endObject();
    return errorContext;
}

From source file:org.apache.olingo.odata2.core.ep.consumer.JsonErrorDocumentConsumer.java

License:Apache License

private String readJson(final JsonReader reader) throws IOException {
    StringBuilder sb = new StringBuilder();

    while (reader.hasNext()) {
        JsonToken token = reader.peek();
        if (token == JsonToken.NAME) {
            if (sb.length() > 0) {
                sb.append(",");
            }/*from w  w  w .  ja  v a2s  . c  om*/
            String name = reader.nextName();
            sb.append("\"").append(name).append("\"").append(":");
        } else if (token == JsonToken.BEGIN_OBJECT) {
            reader.beginObject();
            sb.append("{").append(readJson(reader)).append("}");
            reader.endObject();
        } else if (token == JsonToken.BEGIN_ARRAY) {
            reader.beginArray();
            sb.append("[").append(readJson(reader)).append("]");
            reader.endArray();
        } else {
            sb.append("\"").append(reader.nextString()).append("\"");
        }
    }

    return sb.toString();
}

From source file:org.apache.olingo.odata2.core.ep.consumer.JsonErrorDocumentConsumer.java

License:Apache License

private void parseMessage(final JsonReader reader, final ODataErrorContext errorContext)
        throws IOException, EntityProviderException {

    reader.beginObject();//from  www . j a va  2s. co  m
    boolean valueFound = false;
    boolean langFound = false;
    String currentName;

    while (reader.hasNext()) {
        currentName = reader.nextName();
        if (FormatJson.LANG.equals(currentName)) {
            langFound = true;
            String langValue = getValue(reader);
            if (langValue != null) {
                errorContext.setLocale(getLocale(langValue));
            }
        } else if (FormatJson.VALUE.equals(currentName)) {
            valueFound = true;
            errorContext.setMessage(getValue(reader));
        } else {
            throw new EntityProviderException(EntityProviderException.INVALID_STATE
                    .addContent("Invalid name '" + currentName + "' found."));
        }
    }

    if (!langFound) {
        throw new EntityProviderException(
                EntityProviderException.MISSING_PROPERTY.addContent("Mandatory 'lang' property not found.'"));
    }
    if (!valueFound) {
        throw new EntityProviderException(
                EntityProviderException.MISSING_PROPERTY.addContent("Mandatory 'value' property not found.'"));
    }
    reader.endObject();
}

From source file:org.apache.olingo.odata2.core.ep.consumer.JsonPropertyConsumer.java

License:Apache License

public Map<String, Object> readPropertyStandalone(final JsonReader reader, final EdmProperty property,
        final EntityProviderReadProperties readProperties) throws EntityProviderException {
    try {/*w w w. jav a  2s  .  co m*/
        EntityPropertyInfo entityPropertyInfo = EntityInfoAggregator.create(property);
        Map<String, Object> typeMappings = readProperties == null ? null : readProperties.getTypeMappings();
        Map<String, Object> result = new HashMap<String, Object>();

        reader.beginObject();
        String nextName = reader.nextName();
        if (FormatJson.D.equals(nextName)) {
            reader.beginObject();
            nextName = reader.nextName();
            handleName(reader, typeMappings, entityPropertyInfo, readProperties, result, nextName);
            reader.endObject();
        } else {
            handleName(reader, typeMappings, entityPropertyInfo, readProperties, result, nextName);
        }
        reader.endObject();

        if (reader.peek() != JsonToken.END_DOCUMENT) {
            throw new EntityProviderException(
                    EntityProviderException.END_DOCUMENT_EXPECTED.addContent(reader.peek().toString()));
        }

        return result;
    } catch (final IOException e) {
        throw new EntityProviderException(
                EntityProviderException.EXCEPTION_OCCURRED.addContent(e.getClass().getSimpleName()), e);
    } catch (final IllegalStateException e) {
        throw new EntityProviderException(
                EntityProviderException.EXCEPTION_OCCURRED.addContent(e.getClass().getSimpleName()), e);
    }
}