@Override protected Class<?> classForName(String typeName, String origin) { if(typeName.equals("external")) return CRPluggableArtifact.class; if (typeName.equals("build") || typeName.equals("test")) return CRBuiltInArtifact.class; else throw new JsonParseException( String.format("Invalid or unknown task type '%s'",typeName)); }
@Override public DashboardFilter deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { JsonObject jsonObject = json.getAsJsonObject(); final String name = defensivelyGetString(jsonObject, KEY_NAME); jsonObject.addProperty(KEY_NAME, name); final String type = defensivelyGetString(jsonObject, KEY_TYPE); if (StringUtils.isBlank(type)) throw new JsonParseException("Missing filter type"); switch (type) { case TYPE_WHITELIST: return context.deserialize(jsonObject, WhitelistFilter.class); case TYPE_BLACKLIST: return context.deserialize(jsonObject, BlacklistFilter.class); default: throw new JsonParseException(format("Don't know how to deserialize filter type: \"%s\"", type)); } } }
public JsonReader jsonReaderFrom(String string) { try { if (StringUtils.isBlank(string)) { string = "{}"; } return new JsonReader(GSON.fromJson(string, JsonElement.class).getAsJsonObject()); } catch (Exception e) { throw new JsonParseException(e); } }
public JsonReader jsonReaderFrom(Map map) { try { return new JsonReader(GSON.toJsonTree(map).getAsJsonObject()); } catch (Exception e) { throw new JsonParseException(e); } }
@Override public Date deserialize(final JsonElement json, final Type typeOfT, final JsonDeserializationContext context) throws JsonParseException { final Date date; if (json.isJsonPrimitive()) { date = new Date(this.timeUnit.toMillis(json.getAsLong())); } else if (json.isJsonNull()) { date = null; } else { throw new JsonParseException("Dates represented as time since the epoch must either be numbers or null."); } return date; }
public static ArtifactOrigin getArtifactOrigin(String origin) { return Arrays.stream(values()) .filter(item -> item.toString().equals(origin)) .findFirst() .orElseThrow(() -> new JsonParseException(String.format("Invalid artifact origin '%s' for fetch task.", origin))); } }
@Override public Filters deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { final JsonObject j = json.getAsJsonObject(); final JsonElement filters = j.get(KEY_FILTERS); if (null == filters) { throw new JsonParseException("Missing filters array!"); } final ArrayList<DashboardFilter> viewFilters = new ArrayList<>(); filters.getAsJsonArray().forEach((f) -> viewFilters.add(context.deserialize(f, DashboardFilter.class))); return new Filters(viewFilters); } }
@Override public BlockVector3 deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { JsonArray jsonArray = json.getAsJsonArray(); if (jsonArray.size() != 3) { throw new JsonParseException("Expected array of 3 length for BlockVector3"); } double x = jsonArray.get(0).getAsInt(); double y = jsonArray.get(1).getAsInt(); double z = jsonArray.get(2).getAsInt(); return BlockVector3.at(x, y, z); } }
private ColliderInfo processAABBShape(JsonDeserializationContext context, JsonObject colliderDef) { Vector3f offset = context.deserialize(colliderDef.get(POSITION), Vector3f.class); Vector3f extent = context.deserialize(colliderDef.get(EXTENTS), Vector3f.class); if (offset == null) { throw new JsonParseException("AABB Collider missing position"); } if (extent == null) { throw new JsonParseException("AABB Collider missing extents"); } extent.absolute(); return new ColliderInfo(offset, COLLISION_SHAPE_FACTORY.getNewBox(extent)); }
@Override protected Class<?> classForName(String typeName, String origin) { if (typeName.equals(CRExecTask.TYPE_NAME)) return CRExecTask.class; if (typeName.equals(CRBuildTask.RAKE_TYPE_NAME)) return CRBuildTask.class; if (typeName.equals(CRBuildTask.ANT_TYPE_NAME)) return CRBuildTask.class; if (typeName.equals(CRBuildTask.NANT_TYPE_NAME)) return CRNantTask.class; if (typeName.equals(CRPluggableTask.TYPE_NAME)) return CRPluggableTask.class; if (typeName.equals(CRAbstractFetchTask.TYPE_NAME)) { return CRAbstractFetchTask.ArtifactOrigin.getArtifactOrigin(origin).getArtifactTaskClass(); } throw new JsonParseException(String.format("Invalid or unknown task type '%s'.", typeName)); }
/** * Throws a new IO exception with the given message and a context snippet * with this reader's content. */ @NotNull private JsonParseException createParseError(String message) { throw new JsonParseException(message + " at line " + getLineNumber() + " column " + getColumnNumber()); } }
@Override public String nextString() throws IOException { JsonToken peeked = peek(); if (peeked == JsonToken.STRING) { return super.nextString(); } throw new JsonParseException( String.format("In strict mode (!lenient) string read only from string literal, not %s. Path: %s", peeked, getPath())); } };
@Override public R read(JsonReader in) throws IOException { JsonElement jsonElement = Streams.parse(in); JsonElement labelJsonElement = jsonElement.getAsJsonObject().remove(typeFieldName); String label = (labelJsonElement == null ? null : labelJsonElement.getAsString()); @SuppressWarnings("unchecked") // registration requires that subtype extends T TypeAdapter<R> delegate = (TypeAdapter<R>) labelToDelegate.get(label); if (delegate == null) { throw new JsonParseException("cannot deserialize " + baseType + " subtype named " + label + "; did you forget to register a subtype?"); } return delegate.fromJsonTree(jsonElement); }
private ColliderInfo processSphereShape(JsonDeserializationContext context, JsonObject colliderDef) { Vector3f offset = context.deserialize(colliderDef.get(POSITION), Vector3f.class); float radius = colliderDef.get(RADIUS).getAsFloat(); if (offset == null) { throw new JsonParseException("Sphere Collider missing position"); } return new ColliderInfo(offset, COLLISION_SHAPE_FACTORY.getNewSphere(radius)); }
@Override public Vector3 deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { JsonArray jsonArray = json.getAsJsonArray(); if (jsonArray.size() != 3) { throw new JsonParseException("Expected array of 3 length for Vector3"); } double x = jsonArray.get(0).getAsDouble(); double y = jsonArray.get(1).getAsDouble(); double z = jsonArray.get(2).getAsDouble(); return Vector3.at(x, y, z); } }
public static ConfigurationProperty fromJSON(JsonReader jsonReader) { try { String key = jsonReader.getString("key"); String value = jsonReader.optString("value").orElse(null); String encryptedValue = jsonReader.optString("encrypted_value").orElse(null); return new ConfigurationProperty().deserialize(key, value, encryptedValue); } catch (Exception e) { throw new JsonParseException("Could not parse configuration property"); } } }
public static Role fromJSON(JsonReader jsonReader) { Role model; String type = jsonReader.optString("type").orElse(""); if ("gocd".equals(type)) { model = GoCDRoleConfigRepresenter.fromJSON(jsonReader.readJsonObject("attributes")); } else if ("plugin".equals(type)) { model = PluginRoleConfigRepresenter.fromJSON(jsonReader.readJsonObject("attributes")); } else { throw new JsonParseException("Invalid role type '%s'. It has to be one of 'gocd' or 'plugin'"); } model.setName(new CaseInsensitiveString(jsonReader.optString("name").orElse(null))); return model; }
public static Role fromJSON(JsonReader jsonReader) { Role model; String type = jsonReader.optString("type").orElse(""); if ("gocd".equals(type)) { model = GoCDRoleConfigRepresenter.fromJSON(jsonReader.readJsonObject("attributes")); } else if ("plugin".equals(type)) { model = PluginRoleConfigRepresenter.fromJSON(jsonReader.readJsonObject("attributes")); } else { throw new JsonParseException("Invalid role type '%s'. It has to be one of 'gocd' or 'plugin'"); } model.setName(new CaseInsensitiveString(jsonReader.optString("name").orElse(null))); return model; }
public WxMpUserCumulate deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { WxMpUserCumulate cumulate = new WxMpUserCumulate(); JsonObject summaryJsonObject = json.getAsJsonObject(); try { String refDate = GsonHelper.getString(summaryJsonObject, "ref_date"); if (refDate != null) { cumulate.setRefDate(SIMPLE_DATE_FORMAT.parse(refDate)); } cumulate.setCumulateUser(GsonHelper.getInteger(summaryJsonObject, "cumulate_user")); } catch (ParseException e) { throw new JsonParseException(e); } return cumulate; }
public WxMpUserSummary deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { WxMpUserSummary summary = new WxMpUserSummary(); JsonObject summaryJsonObject = json.getAsJsonObject(); try { String refDate = GsonHelper.getString(summaryJsonObject, "ref_date"); if (refDate != null) { summary.setRefDate(SIMPLE_DATE_FORMAT.parse(refDate)); } summary.setUserSource(GsonHelper.getInteger(summaryJsonObject, "user_source")); summary.setNewUser(GsonHelper.getInteger(summaryJsonObject, "new_user")); summary.setCancelUser(GsonHelper.getInteger(summaryJsonObject, "cancel_user")); } catch (ParseException e) { throw new JsonParseException(e); } return summary; }