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

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

Introduction

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

Prototype

public void beginObject() throws IOException 

Source Link

Document

Consumes the next token from the JSON stream and asserts that it is the beginning of a new object.

Usage

From source file:org.gradle.api.internal.artifacts.ivyservice.ivyresolve.parser.ModuleMetadataParser.java

License:Apache License

private List<ModuleFile> consumeFiles(JsonReader reader) throws IOException {
    List<ModuleFile> files = new ArrayList<ModuleFile>();
    reader.beginArray();//from w ww  .  j av a 2  s.co m
    while (reader.peek() != END_ARRAY) {
        reader.beginObject();
        String fileName = null;
        String fileUri = null;
        while (reader.peek() != END_OBJECT) {
            String name = reader.nextName();
            if (name.equals("name")) {
                fileName = reader.nextString();
            } else if (name.equals("url")) {
                fileUri = reader.nextString();
            } else {
                consumeAny(reader);
            }
        }
        reader.endObject();
        files.add(new ModuleFile(fileName, fileUri));
    }
    reader.endArray();
    return files;
}

From source file:org.gradle.api.internal.artifacts.ivyservice.ivyresolve.parser.ModuleMetadataParser.java

License:Apache License

private ImmutableAttributes consumeAttributes(JsonReader reader) throws IOException {
    ImmutableAttributes attributes = ImmutableAttributes.EMPTY;
    reader.beginObject();
    while (reader.peek() != END_OBJECT) {
        String attrName = reader.nextName();
        if (reader.peek() == BOOLEAN) {
            boolean attrValue = reader.nextBoolean();
            attributes = attributesFactory.concat(attributes, Attribute.of(attrName, Boolean.class), attrValue);
        } else {//from  w  ww .  j a v  a  2s  .c o  m
            String attrValue = reader.nextString();
            attributes = attributesFactory.concat(attributes, Attribute.of(attrName, String.class),
                    new CoercingStringValueSnapshot(attrValue, instantiator));
        }
    }
    reader.endObject();
    return attributes;
}

From source file:org.gradle.nativeplatform.toolchain.internal.msvcpp.version.CommandLineToolVersionLocator.java

License:Apache License

private VisualStudioInstallCandidate readInstall(JsonReader reader) throws IOException {
    String visualStudioInstallPath = null;
    String visualStudioVersion = null;

    reader.beginObject();
    while (reader.hasNext()) {
        String key = reader.nextName();
        if (key.equals(INSTALLATION_PATH_KEY)) {
            visualStudioInstallPath = reader.nextString();
        } else if (key.equals(INSTALLATION_VERSION_KEY)) {
            visualStudioVersion = reader.nextString();
        } else {/*from w  ww  .  j  ava 2s.c o  m*/
            reader.skipValue();
        }
    }
    reader.endObject();

    File visualStudioInstallDir = new File(visualStudioInstallPath);
    VisualCppInstallCandidate visualCppMetadata = findVisualCppMetadata(visualStudioInstallDir,
            visualStudioVersion);

    if (visualCppMetadata == null) {
        LOGGER.debug("Ignoring candidate Visual Studio version " + visualStudioVersion + " at "
                + visualStudioInstallPath + " because it does not appear to be a valid installation");
        return null;
    } else {
        return new VisualStudioMetadataBuilder().installDir(visualStudioInstallDir)
                .visualCppDir(visualCppMetadata.getVisualCppDir())
                .version(VersionNumber.parse(visualStudioVersion))
                .visualCppVersion(visualCppMetadata.getVersion()).build();
    }
}

From source file:org.guvnor.ala.openshift.access.OpenShiftRuntimeId.java

License:Apache License

public static OpenShiftRuntimeId fromString(String s) {
    String project = null;/*from   w  ww  .j  a va  2 s .com*/
    String service = null;
    String application = null;
    try {
        JsonReader jr = new JsonReader(new StringReader(s));
        jr.beginObject();
        while (jr.hasNext()) {
            String n = jr.nextName();
            if (PROJECT.equals(n)) {
                project = jr.nextString();
            } else if (SERVICE.equals(n)) {
                service = jr.nextString();
            } else if (APPLICATION.equals(n)) {
                application = jr.nextString();
            } else {
                jr.skipValue();
            }
        }
        jr.endObject();
        jr.close();
    } catch (IOException ex) {
        throw new RuntimeException(ex.getMessage(), ex);
    }
    return new OpenShiftRuntimeId(project, service, application);
}

From source file:org.gw2InfoViewer.services.json.JsonConversionService.java

License:Open Source License

public static EventList parseEventListWithoutNames(InputStream json) throws IOException {
    List<Event> events;

    events = new ArrayList<Event>();

    JsonReader reader = new JsonReader(new InputStreamReader(json));
    reader.beginObject();
    if (reader.nextName().equals("events")) {
        reader.beginArray();/*  w  w w.j ava 2 s  . c  o m*/
        while (reader.peek() != JsonToken.END_ARRAY) {
            Event event = gsonBuilder.create().fromJson(reader, Event.class);
            events.add(event);
        }
        reader.endArray();
        reader.endObject();
    }
    return new EventList(events);
}

From source file:org.gw2InfoViewer.services.json.typeadaptors.EventAdapter.java

License:Open Source License

@Override
public Event read(JsonReader reader) throws IOException {
    if (reader.peek() == JsonToken.NULL) {
        reader.nextNull();//w w w .  ja v  a  2s.  c  om
        return null;
    }

    Event event = new Event();

    reader.beginObject();
    reader.nextName(); //world id
    event.setWorldId(reader.nextInt());
    reader.nextName(); //map id
    event.setMapId(reader.nextInt());
    reader.nextName(); //event id
    event.setEventId(reader.nextString());
    reader.nextName();
    event.setState(EventState.valueOf(reader.nextString())); //state
    reader.endObject();

    return event;
}

From source file:org.hibernate.search.backend.elasticsearch.document.model.impl.esnative.ElasticsearchRoutingTypeJsonAdapter.java

License:LGPL

@Override
public RoutingType read(JsonReader in) throws IOException {
    if (in.peek() == JsonToken.NULL) {
        in.nextNull();/*from   w  w  w .  ja v  a2 s  .  com*/
        return null;
    }

    RoutingType value = null;
    in.beginObject();
    while (in.hasNext()) {
        String name = in.nextName();
        switch (name) {
        case "required":
            if (in.nextBoolean()) {
                value = RoutingType.REQUIRED;
            } else {
                value = RoutingType.OPTIONAL;
            }
            break;
        default:
            throw new AssertionFailure(
                    "Unexpected property for attribute of type " + RoutingType.class + ": " + name);
        }
    }

    return value;
}

From source file:org.hibernate.search.backend.elasticsearch.gson.impl.AbstractExtraPropertiesJsonAdapter.java

License:LGPL

@Override
public T read(JsonReader in) throws IOException {
    if (in.peek() == JsonToken.NULL) {
        in.nextNull();//from   w ww .ja  v  a2  s.  c o m
        return null;
    }

    T instance = createInstance();
    try {
        in.beginObject();
        while (in.hasNext()) {
            String name = in.nextName();
            FieldAdapter<? super T> fieldAdapter = fieldAdapters.get(name);
            if (fieldAdapter == null) {
                extraPropertyAdapter.readOne(in, name, instance);
            } else {
                fieldAdapter.read(in, instance);
            }
        }
        in.endObject();
    } catch (IllegalStateException e) {
        throw new JsonSyntaxException(e);
    }

    return instance;
}

From source file:org.hillview.table.rows.GuessSchema.java

License:Open Source License

/**
 * Throws if the value is not valid JSON
 * The gson parser is not strict enough: it parses
 * unquoted strings as JSON.String, so we have to do this manually.
 * Returns true if this is a complex json value, false otherwise.
 *//*ww  w  .  j  a v  a 2s  .  co m*/
private static boolean isJsonValid(final JsonReader jsonReader) throws IOException {
    JsonToken token;
    boolean isComplex = false;
    loop: while ((token = jsonReader.peek()) != JsonToken.END_DOCUMENT && token != null) {
        switch (token) {
        case BEGIN_ARRAY:
            isComplex = true;
            jsonReader.beginArray();
            break;
        case END_ARRAY:
            isComplex = true;
            jsonReader.endArray();
            break;
        case BEGIN_OBJECT:
            isComplex = true;
            jsonReader.beginObject();
            break;
        case END_OBJECT:
            isComplex = true;
            jsonReader.endObject();
            break;
        case NAME:
            jsonReader.nextName();
            break;
        case STRING:
        case NUMBER:
        case BOOLEAN:
        case NULL:
            jsonReader.skipValue();
            break;
        case END_DOCUMENT:
            break loop;
        default:
            throw new AssertionError(token);
        }
    }
    return isComplex;
}

From source file:org.imperiumstudios.Imperium1871Bungee.BukkitWarpHelper.files.JsonFile.java

License:Open Source License

/**
 * Read the config File into RAM.//from w w w  . j  av  a2 s  .co  m
 */
public void readFile() {
    try {
        JsonReader reader = new JsonReader(new FileReader(jsonFile));

        reader.beginObject();

        while (reader.hasNext())
            jsonFileContent.put(reader.nextName(), reader.nextString());

        reader.endObject();
        reader.close();
    } catch (IOException ex) {
        IMP.log.error("JsonFile.java > readFile() > Unable to read " + jsonFile + ". Disabling this plugin ..");
        IMP.log.error("JsonFile.java > readFile() > Error: " + ex.getMessage());
        IMP.disable();
    }
}