|
Added
Link Here
|
| 0 |
- |
1 |
/******************************************************************************* |
|
|
2 |
* Copyright (c) 2015. |
| 3 |
* All rights reserved. This program and the accompanying materials |
| 4 |
* are made available under the terms of the Eclipse Public License v1.0 |
| 5 |
* which accompanies this distribution, and is available at |
| 6 |
* http://www.eclipse.org/legal/epl-v10.html |
| 7 |
* |
| 8 |
* Contributors: |
| 9 |
* sbegaudeau - initial API and implementation |
| 10 |
*******************************************************************************/ |
| 11 |
package org.eclipse.wst.jsdt.bower.core.api; |
| 12 |
|
| 13 |
import com.google.gson.JsonArray; |
| 14 |
import com.google.gson.JsonDeserializationContext; |
| 15 |
import com.google.gson.JsonDeserializer; |
| 16 |
import com.google.gson.JsonElement; |
| 17 |
import com.google.gson.JsonObject; |
| 18 |
import com.google.gson.JsonParseException; |
| 19 |
|
| 20 |
import java.lang.reflect.Type; |
| 21 |
import java.util.ArrayList; |
| 22 |
import java.util.HashMap; |
| 23 |
import java.util.List; |
| 24 |
import java.util.Map; |
| 25 |
import java.util.Map.Entry; |
| 26 |
import java.util.Set; |
| 27 |
|
| 28 |
/** |
| 29 |
* @author sbegaudeau |
| 30 |
*/ |
| 31 |
public class BowerJsonDeserializer implements JsonDeserializer<BowerJson> { |
| 32 |
|
| 33 |
/** |
| 34 |
* The name. |
| 35 |
*/ |
| 36 |
private static final String NAME = "name"; //$NON-NLS-1$ |
| 37 |
|
| 38 |
/** |
| 39 |
* The description. |
| 40 |
*/ |
| 41 |
private static final String DESCRIPTION = "description"; //$NON-NLS-1$ |
| 42 |
|
| 43 |
/** |
| 44 |
* Ther version. |
| 45 |
*/ |
| 46 |
private static final String VERSION = "version"; //$NON-NLS-1$ |
| 47 |
|
| 48 |
/** |
| 49 |
* The homepage. |
| 50 |
*/ |
| 51 |
private static final String HOMEPAGE = "homepage"; //$NON-NLS-1$ |
| 52 |
|
| 53 |
/** |
| 54 |
* Main files. |
| 55 |
*/ |
| 56 |
private static final String MAIN = "main"; //$NON-NLS-1$ |
| 57 |
|
| 58 |
/** |
| 59 |
* Private. |
| 60 |
*/ |
| 61 |
private static final String PRIVATE = "private"; //$NON-NLS-1$ |
| 62 |
|
| 63 |
/** |
| 64 |
* Licenses. |
| 65 |
*/ |
| 66 |
private static final String LICENSES = "licenses"; //$NON-NLS-1$ |
| 67 |
|
| 68 |
/** |
| 69 |
* Ignore. |
| 70 |
*/ |
| 71 |
private static final String IGNORE = "ignore"; //$NON-NLS-1$ |
| 72 |
|
| 73 |
/** |
| 74 |
* Authors. |
| 75 |
*/ |
| 76 |
private static final String AUTHORS = "authors"; //$NON-NLS-1$ |
| 77 |
|
| 78 |
/** |
| 79 |
* Dependencies. |
| 80 |
*/ |
| 81 |
private static final String DEPENDENCIES = "dependencies"; //$NON-NLS-1$ |
| 82 |
|
| 83 |
/** |
| 84 |
* DevDependencies. |
| 85 |
*/ |
| 86 |
private static final String DEV_DEPENDENCIES = "devDependencies"; //$NON-NLS-1$ |
| 87 |
|
| 88 |
/** |
| 89 |
* {@inheritDoc} |
| 90 |
* |
| 91 |
* @see com.google.gson.JsonDeserializer#deserialize(com.google.gson.JsonElement, java.lang.reflect.Type, |
| 92 |
* com.google.gson.JsonDeserializationContext) |
| 93 |
*/ |
| 94 |
@Override |
| 95 |
public BowerJson deserialize(JsonElement json, Type type, JsonDeserializationContext context) |
| 96 |
throws JsonParseException { |
| 97 |
BowerJson bowerJson = new BowerJson(); |
| 98 |
if (json instanceof JsonObject) { |
| 99 |
JsonObject jsonObject = (JsonObject)json; |
| 100 |
|
| 101 |
// Basic properties: string |
| 102 |
bowerJson.setName(this.getAsString(jsonObject, NAME)); |
| 103 |
bowerJson.setDescription(this.getAsString(jsonObject, DESCRIPTION)); |
| 104 |
bowerJson.setVersion(this.getAsString(jsonObject, VERSION)); |
| 105 |
bowerJson.setHomepage(this.getAsString(jsonObject, HOMEPAGE)); |
| 106 |
|
| 107 |
// Boolean |
| 108 |
bowerJson.setPrivate(this.getAsBoolean(jsonObject, PRIVATE)); |
| 109 |
|
| 110 |
// Main: string or array of strings |
| 111 |
bowerJson.setMain(this.getMain(jsonObject)); |
| 112 |
|
| 113 |
// List of string |
| 114 |
bowerJson.setLicenses(this.getAsStringList(jsonObject, LICENSES)); |
| 115 |
bowerJson.setIgnore(this.getAsStringList(jsonObject, IGNORE)); |
| 116 |
bowerJson.setAuthors(this.getAsStringList(jsonObject, AUTHORS)); |
| 117 |
|
| 118 |
// Objects |
| 119 |
bowerJson.setDependencies(this.getAsStringMap(jsonObject, DEPENDENCIES)); |
| 120 |
bowerJson.setDevDependencies(this.getAsStringMap(jsonObject, DEV_DEPENDENCIES)); |
| 121 |
} |
| 122 |
return bowerJson; |
| 123 |
} |
| 124 |
|
| 125 |
/** |
| 126 |
* Returns the value of the property with the given name in the given json object as a string. |
| 127 |
* |
| 128 |
* @param jsonObject |
| 129 |
* The json object |
| 130 |
* @param propertyName |
| 131 |
* The name of the property |
| 132 |
* @return The value of the property |
| 133 |
*/ |
| 134 |
private String getAsString(JsonObject jsonObject, String propertyName) { |
| 135 |
String propertyValue = null; |
| 136 |
JsonElement jsonElement = jsonObject.get(propertyName); |
| 137 |
if (jsonElement != null && jsonElement.isJsonPrimitive()) { |
| 138 |
propertyValue = jsonElement.getAsString(); |
| 139 |
} |
| 140 |
return propertyValue; |
| 141 |
} |
| 142 |
|
| 143 |
/** |
| 144 |
* Returns the value of the property with the given name in the given json object as a boolean. |
| 145 |
* |
| 146 |
* @param jsonObject |
| 147 |
* The json object |
| 148 |
* @param propertyName |
| 149 |
* The name of the property |
| 150 |
* @return The value of the property |
| 151 |
*/ |
| 152 |
private boolean getAsBoolean(JsonObject jsonObject, String propertyName) { |
| 153 |
boolean propertyValue = false; |
| 154 |
JsonElement jsonElement = jsonObject.get(propertyName); |
| 155 |
if (jsonElement != null && jsonElement.isJsonPrimitive()) { |
| 156 |
propertyValue = jsonElement.getAsBoolean(); |
| 157 |
} |
| 158 |
return propertyValue; |
| 159 |
} |
| 160 |
|
| 161 |
/** |
| 162 |
* Returns the value of the property main in the given json object as a string or a list of strings. |
| 163 |
* |
| 164 |
* @param jsonObject |
| 165 |
* The json object |
| 166 |
* @return The value of the property main |
| 167 |
*/ |
| 168 |
private List<String> getMain(JsonObject jsonObject) { |
| 169 |
List<String> main = new ArrayList<String>(); |
| 170 |
JsonElement mainJsonElement = jsonObject.get(MAIN); |
| 171 |
if (mainJsonElement != null) { |
| 172 |
if (mainJsonElement.isJsonArray()) { |
| 173 |
JsonArray mainJsonArray = mainJsonElement.getAsJsonArray(); |
| 174 |
for (JsonElement jsonElement : mainJsonArray) { |
| 175 |
if (jsonElement.isJsonPrimitive()) { |
| 176 |
main.add(jsonElement.getAsString()); |
| 177 |
} |
| 178 |
} |
| 179 |
} else if (mainJsonElement.isJsonPrimitive()) { |
| 180 |
main.add(mainJsonElement.getAsString()); |
| 181 |
} |
| 182 |
} |
| 183 |
return main; |
| 184 |
} |
| 185 |
|
| 186 |
/** |
| 187 |
* Returns the value of the property with the given name in the given json object as a list of strings. |
| 188 |
* |
| 189 |
* @param jsonObject |
| 190 |
* The json object |
| 191 |
* @param propertyName |
| 192 |
* The name of the property |
| 193 |
* @return The value of the property |
| 194 |
*/ |
| 195 |
private List<String> getAsStringList(JsonObject jsonObject, String propertyName) { |
| 196 |
List<String> propertyValue = new ArrayList<String>(); |
| 197 |
JsonElement jsonElement = jsonObject.get(propertyName); |
| 198 |
if (jsonElement != null && jsonElement.isJsonArray()) { |
| 199 |
JsonArray jsonArray = jsonElement.getAsJsonArray(); |
| 200 |
for (JsonElement jsonChildElement : jsonArray) { |
| 201 |
if (jsonChildElement.isJsonPrimitive()) { |
| 202 |
propertyValue.add(jsonChildElement.getAsString()); |
| 203 |
} |
| 204 |
} |
| 205 |
} |
| 206 |
return propertyValue; |
| 207 |
} |
| 208 |
|
| 209 |
/** |
| 210 |
* Returns the value of the property with the given name in the given json object as a map of strings. |
| 211 |
* |
| 212 |
* @param jsonObject |
| 213 |
* The json object |
| 214 |
* @param propertyName |
| 215 |
* The name of the property |
| 216 |
* @return The value of the property |
| 217 |
*/ |
| 218 |
private Map<String, String> getAsStringMap(JsonObject jsonObject, String propertyName) { |
| 219 |
Map<String, String> propertyValue = new HashMap<String, String>(); |
| 220 |
JsonElement jsonElement = jsonObject.get(propertyName); |
| 221 |
if (jsonElement != null && jsonElement.isJsonObject()) { |
| 222 |
JsonObject childJsonObject = jsonElement.getAsJsonObject(); |
| 223 |
Set<Entry<String, JsonElement>> entries = childJsonObject.entrySet(); |
| 224 |
for (Entry<String, JsonElement> entry : entries) { |
| 225 |
if (entry.getValue().isJsonPrimitive()) { |
| 226 |
propertyValue.put(entry.getKey(), entry.getValue().getAsString()); |
| 227 |
} |
| 228 |
} |
| 229 |
} |
| 230 |
return propertyValue; |
| 231 |
} |
| 232 |
|
| 233 |
} |