List of usage examples for com.google.gson JsonObject get
public JsonElement get(String memberName)
From source file:com.balajeetm.mystique.core.MystiqueFactory.java
License:Open Source License
/** * Gets the mystique./*from ww w. j a va 2 s . c o m*/ * * @param turn the turn * @return the mystique */ public MystTurn getMystTurn(JsonObject turn) { MystTurn mystique = null; if (jsonLever.isNull(turn)) { mystique = turns.get(CopyMystTurn.class); } else { String turnType = jsonLever.asString(turn.get(MystiqueConstants.TYPE), MystiqueConstants.EMPTY); if (StringUtils.isEmpty(turnType) || StringUtils.equalsIgnoreCase(turnType, MysType.copy.name())) { mystique = turns.get(CopyMystTurn.class); } else if (StringUtils.equalsIgnoreCase(turnType, MysType.mystique.name())) { mystique = turns.get(MystiqueMystTurn.class); } else if (StringUtils.equalsIgnoreCase(turnType, MysType.custom.name())) { String bean = jsonLever.asString(turn.get(MystiqueConstants.VALUE), MystiqueConstants.EMPTY); try { mystique = turns.get(Class.forName(bean)); } catch (ClassNotFoundException | ClassCastException e) { logger.error(String.format("Invalid mystique. Error while getting mystique %s : %s", turn, e.getMessage()), e); } } else if (StringUtils.equalsIgnoreCase(turnType, MysType.constant.name())) { mystique = turns.get(ConstantMystTurn.class); } else if (StringUtils.equalsIgnoreCase(turnType, MysType.concat.name())) { mystique = turns.get(ConcatMystTurn.class); } else if (StringUtils.equalsIgnoreCase(turnType, MysType.arrayToMap.name())) { mystique = turns.get(ArrayToMapMystTurn.class); } else if (StringUtils.equalsIgnoreCase(turnType, MysType.getFromDeps.name())) { mystique = turns.get(GetFromDepsMystTurn.class); } else if (StringUtils.equalsIgnoreCase(turnType, MysType.condition.name())) { mystique = turns.get(ConditionMystTurn.class); } else if (StringUtils.equalsIgnoreCase(turnType, MysType.dateConvertor.name())) { mystique = turns.get(DateMystTurn.class); } else if (StringUtils.equalsIgnoreCase(turnType, MysType.stringUtils.name())) { mystique = turns.get(StringUtilsMystTurn.class); } else if (StringUtils.equalsIgnoreCase(turnType, MysType.toggle.name())) { mystique = turns.get(ToggleMystTurn.class); } else if (StringUtils.equalsIgnoreCase(turnType, MysType.chain.name())) { mystique = turns.get(ChainMystTurn.class); } else { logger.error(String.format("Invalid mystique %s", turn)); } } return mystique; }
From source file:com.balajeetm.mystique.core.MystiqueMystTurn.java
License:Open Source License
@Override public JsonElement transmute(List<JsonElement> source, JsonObject deps, JsonObject aces, JsonObject turn) { JsonElement transform = null;/* w ww. j a v a2 s .co m*/ JsonElement elementSource = mystiqueLever.getFirst(source); if (null != elementSource) { turn = mystiqueLever.asJsonObject(turn, new JsonObject()); String value = mystiqueLever.asString(turn.get(MystiqueConstants.VALUE)); if (null != value) { JsonElement granularSource = getGranularSource(elementSource, turn, deps, aces); transform = jsonMystique().transform(granularSource, value, deps); } } return transform; }
From source file:com.balajeetm.mystique.core.NowFunction.java
License:Open Source License
@Override public JsonElement execute(JsonElement source, JsonObject turn) { turn = jsonLever.asJsonObject(turn, new JsonObject()); String outFormat = jsonLever.asString(turn.get(MystiqueConstants.OUTFORMAT), MystiqueConstants.LONG); return TransformFunction.getInstance().getFormattedDate(new Date(), outFormat); }
From source file:com.balajeetm.mystique.core.StringUtilsMystTurn.java
License:Open Source License
/** * Execute.//from w ww . j a v a 2 s .c o m * * @param input the input * @param turn the turn * @return the json primitive */ private JsonPrimitive execute(String input, JsonObject turn) { JsonPrimitive transform = null; String action = mystiqueLever.asString(turn.get(MystiqueConstants.ACTION), MystiqueConstants.TRIM); switch (action) { case MystiqueConstants.TRIM: transform = new JsonPrimitive(StringUtils.trim(input)); break; case MystiqueConstants.TRIM_TO_EMPTY: transform = new JsonPrimitive(StringUtils.trimToEmpty(input)); break; case MystiqueConstants.SUBSTRING_AFTER_LAST: String separator = mystiqueLever.asString(turn.get(MystiqueConstants.SEPARATOR), MystiqueConstants.EMPTY); transform = new JsonPrimitive(StringUtils.substringAfterLast(input, separator)); break; default: break; } return transform; }
From source file:com.balajeetm.mystique.core.ToggleMystTurn.java
License:Open Source License
@Override public JsonElement transform(List<JsonElement> source, JsonObject deps, JsonObject aces, JsonObject turn, JsonObject resultWrapper) {//from w ww . ja va 2 s . c o m JsonElement transform = JsonNull.INSTANCE; turn = jsonLever.asJsonObject(turn, new JsonObject()); JsonArray turnArray = jsonLever.asJsonArray(turn.get(MystiqueConstants.TURNS), new JsonArray()); for (JsonElement turnObject : turnArray) { if (jsonLever.isObject(turnObject)) { JsonObject asJsonObject = turnObject.getAsJsonObject(); MystTurn mystique = factory.getMystTurn(asJsonObject); if (null != mystique) { transform = mystique.transform(source, deps, aces, asJsonObject, resultWrapper); } if (jsonLever.isNotNull(transform)) { break; } } } return transform; }
From source file:com.balajeetm.mystique.core.TransformFunction.java
License:Open Source License
@Override public JsonElement execute(JsonElement source, JsonObject turn) { JsonElement result = JsonNull.INSTANCE; if (jsonLever.isNotNull(source)) { turn = jsonLever.asJsonObject(turn, new JsonObject()); String formatIn = jsonLever.asString(turn.get(MystiqueConstants.INFORMAT), MystiqueConstants.DEFAULT); Date inputDate = null;/* w ww .j a v a 2 s . c o m*/ try { switch (formatIn) { case MystiqueConstants.DEFAULT: { Long time = jsonLever.asLong(source); time = time == null ? Long.valueOf(jsonLever.asString(source)) : time; inputDate = new Date(time); break; } case MystiqueConstants.LONG: { Long time = jsonLever.asLong(source); inputDate = new Date(time); break; } case MystiqueConstants.STRING: { String time = jsonLever.asString(source); inputDate = new Date(Long.valueOf(time)); break; } default: inputDate = new SimpleDateFormat(formatIn).parse(jsonLever.asString(source)); break; } } catch (ParseException e) { logger.warn(String.format("Error while parsing input date %s for format %s : %s", source, formatIn, e.getMessage()), e); } String outFormat = jsonLever.asString(turn.get(MystiqueConstants.OUTFORMAT), MystiqueConstants.LONG); result = getFormattedDate(inputDate, outFormat); } return result; }
From source file:com.balajeetm.mystique.util.gson.lever.JsonComparator.java
License:Open Source License
/** * Checks if is subset.//from ww w .ja v a2 s . com * * @param tag the tag * @param subset the subset * @param actual the actual * @param result the result * @return the myst result */ private Comparison isSubset(String tag, JsonElement subset, JsonElement actual, Comparison result) { subset = jsonLever.asJsonElement(subset, JsonNull.INSTANCE); actual = jsonLever.asJsonElement(actual, JsonNull.INSTANCE); if (jsonLever.isNotNull(subset) && jsonLever.isNull(actual)) { result.setResult(Boolean.FALSE); result.addMsg(String.format("The field %s of actual is null", tag)); } else if (!subset.getClass().getCanonicalName().equals(actual.getClass().getCanonicalName())) { result.setResult(Boolean.FALSE); result.addMsg(String.format("The field %s of expected and actual are not of the same type", tag)); } else { if (subset.isJsonObject()) { JsonObject subJson = jsonLever.asJsonObject(subset); JsonObject actJson = jsonLever.asJsonObject(actual); Set<Entry<String, JsonElement>> entrySet = subJson.entrySet(); for (Entry<String, JsonElement> entry : entrySet) { String key = entry.getKey(); JsonElement value = entry.getValue(); JsonElement actualValue = actJson.get(key); isSubset(key, value, actualValue, result); } } else if (subset.isJsonArray()) { JsonArray subJson = jsonLever.asJsonArray(subset); JsonArray actJson = jsonLever.asJsonArray(actual); if (subJson.size() != actJson.size()) { result.setResult(Boolean.FALSE); result.addMsg(String.format("The field %s of expected and actual are not of same size", tag)); } else { for (int i = 0; i < subJson.size(); i++) { isSubset(tag, subJson.get(i), actJson.get(i), result); } } } else { if (!subset.equals(actual)) { result.setResult(Boolean.FALSE); result.addMsg(String.format("The field %s of expected and actual are not same", tag)); } } } return result; }
From source file:com.balajeetm.mystique.util.gson.lever.JsonLever.java
License:Open Source License
/** * A recursive merge of two json elements. * * @param source1 the first json element * @param source2 the second json element * @param mergeArray the flag to denote if arrays should be merged * @return the recursively merged json element *///from www . jav a2 s . c o m public JsonElement merge(JsonElement source1, JsonElement source2, Boolean mergeArray) { mergeArray = null == mergeArray ? Boolean.FALSE : mergeArray; JsonElement result = JsonNull.INSTANCE; source1 = asJsonElement(source1, JsonNull.INSTANCE); source2 = asJsonElement(source2, JsonNull.INSTANCE); if (source1.getClass().equals(source2.getClass())) { if (source1.isJsonObject()) { JsonObject obj1 = asJsonObject(source1); JsonObject obj2 = asJsonObject(source2); result = obj1; JsonObject resultObj = result.getAsJsonObject(); for (Entry<String, JsonElement> entry : obj1.entrySet()) { String key = entry.getKey(); JsonElement value1 = entry.getValue(); JsonElement value2 = obj2.get(key); JsonElement merge = merge(value1, value2, mergeArray); resultObj.add(key, merge); } for (Entry<String, JsonElement> entry : obj2.entrySet()) { String key = entry.getKey(); if (!resultObj.has(key)) { resultObj.add(key, entry.getValue()); } } } else if (source1.isJsonArray()) { result = new JsonArray(); JsonArray resultArray = result.getAsJsonArray(); JsonArray array1 = asJsonArray(source1); JsonArray array2 = asJsonArray(source2); int index = 0; int a1size = array1.size(); int a2size = array2.size(); if (!mergeArray) { for (; index < a1size && index < a2size; index++) { resultArray.add(merge(array1.get(index), array2.get(index), mergeArray)); } } for (; index < a1size; index++) { resultArray.add(array1.get(index)); } index = mergeArray ? 0 : index; for (; index < a2size; index++) { resultArray.add(array2.get(index)); } } else { result = source1 != null ? source1 : source2; } } else { result = isNotNull(source1) ? source1 : source2; } return result; }
From source file:com.balajeetm.mystique.util.gson.lever.JsonLever.java
License:Open Source License
/** * Replete json.//ww w .j a v a2s . co m * * @param source the source * @param fieldName the field name * @param type the type * @return the json object */ private JsonObject repleteJson(JsonObject source, String fieldName, Class<? extends JsonElement> type) { source = isNull(source) ? new JsonObject() : source; JsonElement field = source.get(fieldName); if (isNull(field) || !field.getClass().equals(type)) { source.add(fieldName, getNewElement(type)); } return source; }
From source file:com.balajeetm.mystique.util.gson.lever.JsonQuery.java
License:Open Source License
public Flux<JsonElement> queryAsync(JsonObject query) { return Flux.just(query).map(q -> validate(q)).flatMap(s -> { Flux<JsonElement> obs = null; if ("valid".equals(s)) { JsonElement from = query.get("from"); obs = jsonLever.isArray(from) ? queryAsync(from.getAsJsonArray(), query) : queryAsync(from.getAsJsonObject(), query); } else {//from w w w . j a v a2 s .c o m Exceptions.propagate(new Throwable(s)); } return obs; }); }