Java tutorial
/* * Copyright (c) 2016 Artur Kraft * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package de.arkraft.jenkins; import com.google.gson.*; import de.arkraft.jenkins.entities.*; import de.arkraft.jenkins.enums.BallColor; import de.arkraft.jenkins.enums.EditType; import de.arkraft.jenkins.enums.HealthIcon; import org.joda.time.DateTime; import org.joda.time.Duration; import org.joda.time.format.DateTimeFormat; import org.joda.time.format.DateTimeFormatter; import java.lang.reflect.Type; /** * Created by arkraft on 18.06.2016. */ public class JenkinsHelper { private static final DateTimeFormatter ISO_8601_WITH_MILLIS; private static final DateTimeFormatter JENKINS_FORMAT; static { ISO_8601_WITH_MILLIS = DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ssZ Z"); JENKINS_FORMAT = DateTimeFormat.forPattern("yyyy-MM-dd_HH-mm-ss"); } public static GsonBuilder getGsonBuilder() { GsonBuilder builder = new GsonBuilder(); builder.registerTypeAdapter(DateTime.class, new JsonDeserializer<DateTime>() { @Override public DateTime deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { // using the correct parser right away should save init time compared to new DateTime(<string>) return ISO_8601_WITH_MILLIS.parseDateTime(json.getAsString()); } }); builder.registerTypeAdapter(DateTime.class, new JsonSerializer<DateTime>() { @Override public JsonElement serialize(DateTime src, Type typeOfSrc, JsonSerializationContext context) { return new JsonPrimitive(src.toString()); } }); builder.registerTypeAdapter(Duration.class, new JsonDeserializer() { @Override public Object deserialize(JsonElement json, Type type, JsonDeserializationContext jsonDeserializationContext) throws JsonParseException { return Duration.parse("PT" + json.getAsString() + "S"); } }); builder.registerTypeAdapter(Action.class, new JsonDeserializer<Action>() { @Override public Action deserialize(JsonElement jsonElement, Type type, JsonDeserializationContext jsonDeserializationContext) throws JsonParseException { JsonObject object = ((JsonObject) jsonElement); if (object.has("causes")) { JsonElement element = object.get("causes"); if (element.isJsonArray()) { return jsonDeserializationContext.deserialize(element, Causes.class); } } if (object.has("failCount")) { return jsonDeserializationContext.deserialize(object, CountAction.class); } return null; } }); builder.registerTypeAdapter(ChangeSet.class, new JsonDeserializer<ChangeSet>() { @Override public ChangeSet deserialize(JsonElement jsonElement, Type type, JsonDeserializationContext context) throws JsonParseException { ChangeSet changeSet = new ChangeSet(); JsonObject object = (JsonObject) jsonElement; if (object.has("items") && object.get("items").isJsonArray()) { for (JsonElement element : object.get("items").getAsJsonArray()) { ChangeSet.Change change = context.deserialize(element, ChangeSet.Change.class); changeSet.add(change); } } if (object.has("kind")) { changeSet.setKind(object.get("kind").getAsString()); } return changeSet; } }); builder.registerTypeAdapter(Duration.class, new JsonSerializer<Duration>() { @Override public JsonElement serialize(Duration duration, Type type, JsonSerializationContext jsonSerializationContext) { return new JsonPrimitive(duration.toString().replace("PT", "").replace("S", "")); } }); builder.registerTypeAdapter(EditType.class, new JsonDeserializer<EditType>() { @Override public EditType deserialize(JsonElement jsonElement, Type type, JsonDeserializationContext jsonDeserializationContext) throws JsonParseException { return EditType.byName(jsonElement.getAsString()); } }); builder.registerTypeAdapter(HealthIcon.class, new JsonDeserializer<HealthIcon>() { @Override public HealthIcon deserialize(JsonElement jsonElement, Type type, JsonDeserializationContext jsonDeserializationContext) throws JsonParseException { return HealthIcon.fromName(jsonElement.toString()); } }); builder.registerTypeAdapter(Queue.class, new JsonDeserializer<Queue>() { @Override public Queue deserialize(JsonElement jsonElement, Type type, JsonDeserializationContext context) throws JsonParseException { Queue queue = new Queue(); JsonObject jsonObject = (JsonObject) jsonElement; if (jsonObject.has("items") && jsonObject.get("items").isJsonArray()) { for (JsonElement element : jsonObject.get("items").getAsJsonArray()) { Queue.Item build = context.deserialize(element, Queue.Item.class); queue.add(build); } } return queue; } }); builder.registerTypeAdapter(JobCollection.class, new JsonDeserializer<JobCollection>() { @Override public JobCollection deserialize(JsonElement json, Type type, JsonDeserializationContext context) throws JsonParseException { JobCollection jobs = new JobCollection(); JsonObject object = (JsonObject) json; if (object.has("jobs") && object.get("jobs").isJsonArray()) { for (JsonElement element : object.get("jobs").getAsJsonArray()) { Job job = context.deserialize(element, Job.class); jobs.add(job); } } return jobs; } }); builder.registerTypeAdapter(BallColor.class, new JsonDeserializer<BallColor>() { @Override public BallColor deserialize(JsonElement jsonElement, Type type, JsonDeserializationContext json) throws JsonParseException { return BallColor.valueOf(jsonElement.getAsString().toUpperCase()); } }); return builder; } }