Search in sources :

Example 16 with JsonToken

use of com.google.gson.stream.JsonToken in project J2ME-Loader by nikita36078.

the class SparseIntArrayAdapter method read.

@Override
public SparseIntArray read(JsonReader in) throws IOException {
    JsonToken peek = in.peek();
    if (peek == JsonToken.NULL) {
        in.nextNull();
        return null;
    }
    SparseIntArray array = new SparseIntArray();
    if (peek == JsonToken.STRING) {
        String s = in.nextString();
        try {
            JSONArray jsonArray = new JSONArray(s);
            for (int i = 0; i < jsonArray.length(); i++) {
                JSONObject item = jsonArray.getJSONObject(i);
                array.put(item.getInt("key"), item.getInt("value"));
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return array;
    }
    in.beginObject();
    while (in.hasNext()) {
        String name = in.nextName();
        int key = Integer.parseInt(name);
        int value = in.nextInt();
        array.put(key, value);
    }
    in.endObject();
    return array;
}
Also used : JSONObject(org.json.JSONObject) SparseIntArray(android.util.SparseIntArray) JSONArray(org.json.JSONArray) JSONException(org.json.JSONException) JsonToken(com.google.gson.stream.JsonToken)

Example 17 with JsonToken

use of com.google.gson.stream.JsonToken in project intellij-community by JetBrains.

the class JsonReaderEx method subReader.

@Nullable
public JsonReaderEx subReader() {
    JsonToken nextToken = peek();
    switch(nextToken) {
        case BEGIN_ARRAY:
        case BEGIN_OBJECT:
        case STRING:
        case NUMBER:
        case BOOLEAN:
            break;
        case NULL:
            // just return null
            return null;
        default:
            throw createParseError("Cannot create sub reader, next token " + nextToken + " is not value");
    }
    JsonReaderEx subReader = new JsonReaderEx(in, position, Arrays.copyOf(stack, stack.length));
    subReader.stackSize = stackSize;
    subReader.peeked = peeked;
    subReader.peekedLong = peekedLong;
    subReader.peekedNumberLength = peekedNumberLength;
    subReader.peekedString = peekedString;
    return subReader;
}
Also used : JsonToken(com.google.gson.stream.JsonToken) Nullable(org.jetbrains.annotations.Nullable)

Example 18 with JsonToken

use of com.google.gson.stream.JsonToken in project sling by apache.

the class GetNodeContentCommand method execute.

@Override
public Result<ResourceProxy> execute() {
    GetMethod get = new GetMethod(getPath());
    try {
        httpClient.getParams().setAuthenticationPreemptive(true);
        Credentials defaultcreds = new UsernamePasswordCredentials(repositoryInfo.getUsername(), repositoryInfo.getPassword());
        httpClient.getState().setCredentials(new AuthScope(repositoryInfo.getHost(), repositoryInfo.getPort(), AuthScope.ANY_REALM), defaultcreds);
        int responseStatus = httpClient.executeMethod(get);
        // return EncodingUtil.getString(rawdata, m.getResponseCharSet());
        if (!isSuccessStatus(responseStatus))
            return failureResultForStatusCode(responseStatus);
        ResourceProxy resource = new ResourceProxy(path);
        try (JsonReader jsonReader = new JsonReader(new InputStreamReader(get.getResponseBodyAsStream(), get.getResponseCharSet()))) {
            jsonReader.beginObject();
            while (jsonReader.hasNext()) {
                String name = jsonReader.nextName();
                JsonToken token = jsonReader.peek();
                if (token == JsonToken.STRING) {
                    resource.addProperty(name, jsonReader.nextString());
                } else {
                    jsonReader.skipValue();
                }
            }
            jsonReader.endObject();
        }
        return AbstractResult.success(resource);
    } catch (Exception e) {
        return AbstractResult.failure(new RepositoryException(e));
    } finally {
        get.releaseConnection();
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) GetMethod(org.apache.commons.httpclient.methods.GetMethod) AuthScope(org.apache.commons.httpclient.auth.AuthScope) JsonReader(com.google.gson.stream.JsonReader) JsonToken(com.google.gson.stream.JsonToken) RepositoryException(org.apache.sling.ide.transport.RepositoryException) Credentials(org.apache.commons.httpclient.Credentials) UsernamePasswordCredentials(org.apache.commons.httpclient.UsernamePasswordCredentials) ResourceProxy(org.apache.sling.ide.transport.ResourceProxy) RepositoryException(org.apache.sling.ide.transport.RepositoryException) UsernamePasswordCredentials(org.apache.commons.httpclient.UsernamePasswordCredentials)

Example 19 with JsonToken

use of com.google.gson.stream.JsonToken in project json-android-compare by martinadamek.

the class GsonJson method getValue.

static Object getValue(JsonReader r) throws IOException {
    Object value = null;
    JsonToken token = r.peek();
    switch(token) {
        case NULL:
            r.nextNull();
            break;
        case BOOLEAN:
            value = r.nextBoolean();
            break;
        default:
            value = r.nextString();
    }
    return value;
}
Also used : JsonToken(com.google.gson.stream.JsonToken)

Example 20 with JsonToken

use of com.google.gson.stream.JsonToken in project useful-java-links by Vedenin.

the class StreamingAPI method readJson.

/**
 *  Example to readJson using StreamingAPI
 */
private static void readJson() throws IOException {
    String str = "{\"message\":\"Hi\",\"place\":{\"name\":\"World!\"}}";
    InputStream in = new ByteArrayInputStream(str.getBytes(Charset.forName("UTF-8")));
    JsonReader reader = new JsonReader(new InputStreamReader(in, "UTF-8"));
    while (reader.hasNext()) {
        JsonToken jsonToken = reader.peek();
        if (jsonToken == JsonToken.BEGIN_OBJECT) {
            reader.beginObject();
        } else if (jsonToken == JsonToken.END_OBJECT) {
            reader.endObject();
        }
        if (jsonToken == JsonToken.STRING) {
            // print Hi World!
            System.out.print(reader.nextString() + " ");
        } else {
            reader.skipValue();
        }
    }
    reader.close();
}
Also used : JsonReader(com.google.gson.stream.JsonReader) JsonToken(com.google.gson.stream.JsonToken)

Aggregations

JsonToken (com.google.gson.stream.JsonToken)26 JsonReader (com.google.gson.stream.JsonReader)9 IOException (java.io.IOException)6 List (java.util.List)4 JsonPrimitive (com.google.gson.JsonPrimitive)3 InputStreamReader (java.io.InputStreamReader)3 ArrayList (java.util.ArrayList)3 HashSet (java.util.HashSet)3 BufferedReader (java.io.BufferedReader)2 PortModel (org.apache.airavata.model.PortModel)2 DeserializeException (org.bimserver.plugins.deserializers.DeserializeException)2 ListWaitingObject (org.bimserver.shared.ListWaitingObject)2 WaitingList (org.bimserver.shared.WaitingList)2 AbstractEList (org.eclipse.emf.common.util.AbstractEList)2 EList (org.eclipse.emf.common.util.EList)2 EObject (org.eclipse.emf.ecore.EObject)2 EReference (org.eclipse.emf.ecore.EReference)2 EStructuralFeature (org.eclipse.emf.ecore.EStructuralFeature)2 ParsingException (org.eclipse.smarthome.automation.parser.ParsingException)2 ParsingNestedException (org.eclipse.smarthome.automation.parser.ParsingNestedException)2