package mesosphere.marathon.client.model.v2;

import java.lang.reflect.Type;

import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonParseException;
import com.google.gson.JsonPrimitive;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer;

/**
 * @author Lewis Headden <lewis_headden@condenast.com>
 */
public abstract class EnvValue {

	public abstract boolean isSecret();

	public abstract EnvSecret getAsSecret();

	public abstract boolean isString();

	public abstract EnvString getAsString();

	public static class EnvValueAdapter implements JsonDeserializer<EnvValue>, JsonSerializer<EnvValue> {

		@Override
		public EnvValue deserialize(JsonElement jsonElement, Type type, JsonDeserializationContext jsonDeserializationContext) throws JsonParseException {
			if (jsonElement.isJsonObject() && jsonElement.getAsJsonObject().has("secret"))
				return jsonDeserializationContext.deserialize(jsonElement, EnvSecret.class);
			else return new EnvString(jsonElement.getAsString());
		}

		@Override
		public JsonElement serialize(EnvValue envValue, Type type, JsonSerializationContext jsonSerializationContext) {
			if (envValue instanceof EnvString) {
				return new JsonPrimitive(((EnvString) envValue).getValue());
			} else if (envValue instanceof EnvSecret) {
				return jsonSerializationContext.serialize(envValue);
			} else {
				throw new IllegalStateException("Cannot serialize " + envValue.getClass());
			}
		}
	}

}

@@ -1064,16 +1064,57 @@
 %09%09%09else 
+if (jsonElement.isJsonPrimitive()) %7B%0A%09%09%09%09
 return n
@@ -1154,16 +1154,135 @@
 ing());%0A
+%09%09%09%7D else %7B%0A%09%09%09%09throw new IllegalStateException(%22Cannot deserialize %22 + jsonElement.toString() + %22 as EnvValue%22);%0A%09%09%09%7D%0A
 %09%09%7D%0A%0A%09%09@

Add explicit exception if value is not EnvValue