Search in sources :

Example 1 with JsonToken

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

the class ListChildrenCommand 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);
        Gson gson = new Gson();
        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.BEGIN_OBJECT) {
                    ResourceProxy child = new ResourceProxy(PathUtil.join(path, name));
                    ResourceWithPrimaryType resourceWithPrimaryType = gson.fromJson(jsonReader, ResourceWithPrimaryType.class);
                    // evaluate its jcr:primaryType as well!
                    child.addProperty(Repository.JCR_PRIMARY_TYPE, resourceWithPrimaryType.getPrimaryType());
                    resource.addChild(child);
                } else if (token == JsonToken.STRING) {
                    if (Repository.JCR_PRIMARY_TYPE.equals(name)) {
                        String primaryType = jsonReader.nextString();
                        if (primaryType != null) {
                            // TODO - needed?
                            resource.addProperty(Repository.JCR_PRIMARY_TYPE, primaryType);
                        }
                    }
                } 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) Gson(com.google.gson.Gson) RepositoryException(org.apache.sling.ide.transport.RepositoryException) ResourceProxy(org.apache.sling.ide.transport.ResourceProxy) RepositoryException(org.apache.sling.ide.transport.RepositoryException) UsernamePasswordCredentials(org.apache.commons.httpclient.UsernamePasswordCredentials) 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) UsernamePasswordCredentials(org.apache.commons.httpclient.UsernamePasswordCredentials) Credentials(org.apache.commons.httpclient.Credentials)

Example 2 with JsonToken

use of com.google.gson.stream.JsonToken in project smarthome by eclipse.

the class RuleGSONParser method parse.

@Override
public Set<Rule> parse(InputStreamReader reader) throws ParsingException {
    JsonReader jr = new JsonReader(reader);
    try {
        Set<Rule> rules = new HashSet<>();
        if (jr.hasNext()) {
            JsonToken token = jr.peek();
            if (JsonToken.BEGIN_ARRAY.equals(token)) {
                rules.addAll(gson.fromJson(jr, new TypeToken<List<Rule>>() {
                }.getType()));
            } else {
                Rule rule = gson.fromJson(jr, Rule.class);
                rules.add(rule);
            }
            return rules;
        }
    } catch (Exception e) {
        throw new ParsingException(new ParsingNestedException(ParsingNestedException.RULE, null, e));
    } finally {
        try {
            jr.close();
        } catch (IOException e) {
        }
    }
    return Collections.emptySet();
}
Also used : ParsingException(org.eclipse.smarthome.automation.parser.ParsingException) ParsingNestedException(org.eclipse.smarthome.automation.parser.ParsingNestedException) JsonReader(com.google.gson.stream.JsonReader) JsonToken(com.google.gson.stream.JsonToken) List(java.util.List) Rule(org.eclipse.smarthome.automation.Rule) IOException(java.io.IOException) ParsingException(org.eclipse.smarthome.automation.parser.ParsingException) ParsingNestedException(org.eclipse.smarthome.automation.parser.ParsingNestedException) IOException(java.io.IOException) HashSet(java.util.HashSet)

Example 3 with JsonToken

use of com.google.gson.stream.JsonToken in project smarthome by eclipse.

the class TemplateGSONParser method parse.

@Override
public Set<Template> parse(InputStreamReader reader) throws ParsingException {
    JsonReader jr = new JsonReader(reader);
    try {
        if (jr.hasNext()) {
            JsonToken token = jr.peek();
            Set<Template> templates = new HashSet<>();
            if (JsonToken.BEGIN_ARRAY.equals(token)) {
                templates.addAll(gson.fromJson(jr, new TypeToken<List<RuleTemplate>>() {
                }.getType()));
            } else {
                Template template = gson.fromJson(jr, RuleTemplate.class);
                templates.add(template);
            }
            return templates;
        }
    } catch (Exception e) {
        throw new ParsingException(new ParsingNestedException(ParsingNestedException.TEMPLATE, null, e));
    } finally {
        try {
            jr.close();
        } catch (IOException e) {
        }
    }
    return Collections.emptySet();
}
Also used : ParsingException(org.eclipse.smarthome.automation.parser.ParsingException) ParsingNestedException(org.eclipse.smarthome.automation.parser.ParsingNestedException) JsonReader(com.google.gson.stream.JsonReader) JsonToken(com.google.gson.stream.JsonToken) List(java.util.List) IOException(java.io.IOException) ParsingException(org.eclipse.smarthome.automation.parser.ParsingException) ParsingNestedException(org.eclipse.smarthome.automation.parser.ParsingNestedException) IOException(java.io.IOException) RuleTemplate(org.eclipse.smarthome.automation.template.RuleTemplate) Template(org.eclipse.smarthome.automation.template.Template) HashSet(java.util.HashSet)

Example 4 with JsonToken

use of com.google.gson.stream.JsonToken in project smarthome by eclipse.

the class PropertiesTypeAdapter method read.

@Override
public Map<String, Object> read(JsonReader in) throws IOException {
    // gson implementation code is modified when deserializing numbers
    JsonToken peek = in.peek();
    if (peek == JsonToken.NULL) {
        in.nextNull();
        return null;
    }
    Map<String, Object> map = constructor.get(TOKEN).construct();
    if (peek == JsonToken.BEGIN_ARRAY) {
        in.beginArray();
        while (in.hasNext()) {
            // entry array
            in.beginArray();
            String key = keyAdapter.read(in);
            // modification
            Object value = getValue(in);
            Object replaced = map.put(key, value);
            if (replaced != null) {
                throw new JsonSyntaxException("duplicate key: " + key);
            }
            in.endArray();
        }
        in.endArray();
    } else {
        in.beginObject();
        while (in.hasNext()) {
            JsonReaderInternalAccess.INSTANCE.promoteNameToValue(in);
            String key = keyAdapter.read(in);
            // modification
            Object value = getValue(in);
            Object replaced = map.put(key, value);
            if (replaced != null) {
                throw new JsonSyntaxException("duplicate key: " + key);
            }
        }
        in.endObject();
    }
    return map;
}
Also used : JsonSyntaxException(com.google.gson.JsonSyntaxException) JsonToken(com.google.gson.stream.JsonToken)

Example 5 with JsonToken

use of com.google.gson.stream.JsonToken in project cdap by caskdata.

the class JsonStructuredRecordDatumReader method decodeUnion.

@Override
protected Object decodeUnion(Decoder decoder, Schema unionSchema) throws IOException {
    JsonReader jsonReader = getJsonReader(decoder);
    JsonToken token = jsonReader.peek();
    // Based on the token to guess the schema
    for (Schema schema : unionSchema.getUnionSchemas()) {
        if (SCHEMA_TO_JSON_TYPE.get(schema.getType()) == token) {
            return decode(decoder, schema);
        }
    }
    throw new IOException(String.format("No matching schema found for union type: %s for token: %s", unionSchema, token));
}
Also used : Schema(co.cask.cdap.api.data.schema.Schema) JsonReader(com.google.gson.stream.JsonReader) JsonToken(com.google.gson.stream.JsonToken) IOException(java.io.IOException)

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