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();
}
}
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();
}
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();
}
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;
}
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));
}
Aggregations