List of usage examples for com.google.gson JsonElement isJsonNull
public boolean isJsonNull()
From source file:org.apache.hadoop.hive.json.JsonShredder.java
License:Apache License
private void shredObject(String name, JsonElement json) throws IOException { if (json.isJsonPrimitive()) { JsonPrimitive primitive = (JsonPrimitive) json; if (primitive.isBoolean()) { getFile(name).append(primitive.getAsBoolean() + "\n"); } else if (primitive.isString()) { getFile(name).append(primitive.getAsString().replace("\\", "\\\\").replace("\n", "\\n") + "\n"); } else if (primitive.isNumber()) { getFile(name).append(primitive.getAsNumber() + "\n"); }/*from ww w.j a va 2 s . co m*/ } else if (json.isJsonNull()) { // just skip it } else if (json.isJsonArray()) { for (JsonElement child : ((JsonArray) json)) { shredObject(name, child); } } else { JsonObject obj = (JsonObject) json; for (Map.Entry<String, JsonElement> field : obj.entrySet()) { String fieldName = field.getKey(); shredObject(name + "." + fieldName, field.getValue()); } } }
From source file:org.apache.metamodel.elasticsearch.elastic1.rest.JestElasticSearchUtils.java
License:Apache License
private static Object getDataFromColumnType(JsonElement field, ColumnType type) { if (field == null || field.isJsonNull()) { return null; }// w ww . j av a2s.c o m if (field.isJsonObject()) { return new Gson().fromJson(field, Map.class); } if (field.isJsonArray()) { return new Gson().fromJson(field, List.class); } if (type.isNumber()) { // Pretty terrible workaround to avoid LazilyParsedNumber // (which is happily output, but not recognized by Jest/GSON). return NumberComparator.toNumber(field.getAsString()); } else if (type.isTimeBased()) { final Date valueToDate = ElasticSearchDateConverter.tryToConvert(field.getAsString()); if (valueToDate == null) { return field.getAsString(); } else { return valueToDate; } } else if (type.isBoolean()) { return field.getAsBoolean(); } else { return field.getAsString(); } }
From source file:org.apache.orc.tools.json.JsonSchemaFinder.java
License:Apache License
static HiveType pickType(JsonElement json) { if (json.isJsonPrimitive()) { JsonPrimitive prim = (JsonPrimitive) json; if (prim.isBoolean()) { return new BooleanType(); } else if (prim.isNumber()) { Matcher matcher = DECIMAL_PATTERN.matcher(prim.getAsString()); if (matcher.matches()) { int intDigits = matcher.group("int").length(); String fraction = matcher.group("fraction"); int scale = fraction == null ? 0 : fraction.length(); if (scale == 0) { if (intDigits < 19) { long value = prim.getAsLong(); if (value >= -128 && value < 128) { return new NumericType(HiveType.Kind.BYTE, intDigits, scale); } else if (value >= -32768 && value < 32768) { return new NumericType(HiveType.Kind.SHORT, intDigits, scale); } else if (value >= -2147483648 && value < 2147483648L) { return new NumericType(HiveType.Kind.INT, intDigits, scale); } else { return new NumericType(HiveType.Kind.LONG, intDigits, scale); }// w ww . ja v a 2 s . c o m } else if (intDigits == 19) { // at 19 digits, it may fit inside a long, but we need to check BigInteger val = prim.getAsBigInteger(); if (val.compareTo(MIN_LONG) >= 0 && val.compareTo(MAX_LONG) <= 0) { return new NumericType(HiveType.Kind.LONG, intDigits, scale); } } } if (intDigits + scale <= MAX_DECIMAL_DIGITS) { return new NumericType(HiveType.Kind.DECIMAL, intDigits, scale); } } double value = prim.getAsDouble(); if (value >= Float.MIN_VALUE && value <= Float.MAX_VALUE) { return new NumericType(HiveType.Kind.FLOAT, 0, 0); } else { return new NumericType(HiveType.Kind.DOUBLE, 0, 0); } } else { String str = prim.getAsString(); if (TIMESTAMP_PATTERN.matcher(str).matches()) { return new StringType(HiveType.Kind.TIMESTAMP); } else if (HEX_PATTERN.matcher(str).matches()) { return new StringType(HiveType.Kind.BINARY); } else { return new StringType(HiveType.Kind.STRING); } } } else if (json.isJsonNull()) { return new NullType(); } else if (json.isJsonArray()) { ListType result = new ListType(); result.elementType = new NullType(); for (JsonElement child : ((JsonArray) json)) { HiveType sub = pickType(child); if (result.elementType.subsumes(sub)) { result.elementType.merge(sub); } else if (sub.subsumes(result.elementType)) { sub.merge(result.elementType); result.elementType = sub; } else { result.elementType = new UnionType(result.elementType, sub); } } return result; } else { JsonObject obj = (JsonObject) json; StructType result = new StructType(); for (Map.Entry<String, JsonElement> field : obj.entrySet()) { String fieldName = field.getKey(); HiveType type = pickType(field.getValue()); result.fields.put(fieldName, type); } return result; } }
From source file:org.apache.orc.tools.json.JsonShredder.java
License:Apache License
private void shredObject(String name, JsonElement json) throws IOException { if (json.isJsonPrimitive()) { JsonPrimitive primitive = (JsonPrimitive) json; getFile(name).println(primitive.getAsString()); } else if (json.isJsonNull()) { // just skip it } else if (json.isJsonArray()) { for (JsonElement child : ((JsonArray) json)) { shredObject(name + ".list", child); }/* w w w .j a v a 2 s .com*/ } else { JsonObject obj = (JsonObject) json; for (Map.Entry<String, JsonElement> field : obj.entrySet()) { String fieldName = field.getKey(); shredObject(name + "." + fieldName, field.getValue()); } } }
From source file:org.apache.qpid.disttest.json.PropertyValueAdapter.java
License:Apache License
@Override public PropertyValue deserialize(JsonElement json, Type type, JsonDeserializationContext context) throws JsonParseException { if (json.isJsonNull()) { return null; } else if (json.isJsonPrimitive()) { Object result = null;/*from www. j a va 2 s. c o m*/ JsonPrimitive primitive = json.getAsJsonPrimitive(); if (primitive.isString()) { result = primitive.getAsString(); } else if (primitive.isNumber()) { String asString = primitive.getAsString(); if (asString.indexOf('.') != -1 || asString.indexOf('e') != -1) { result = primitive.getAsDouble(); } else { result = primitive.getAsLong(); } } else if (primitive.isBoolean()) { result = primitive.getAsBoolean(); } else { throw new JsonParseException("Unsupported primitive value " + primitive); } return new SimplePropertyValue(result); } else if (json.isJsonArray()) { JsonArray array = json.getAsJsonArray(); List<Object> result = new ArrayList<Object>(array.size()); for (JsonElement element : array) { result.add(context.deserialize(element, Object.class)); } return new SimplePropertyValue(result); } else if (json.isJsonObject()) { JsonObject object = json.getAsJsonObject(); JsonElement defElement = object.getAsJsonPrimitive(DEF_FIELD); Class<?> classInstance = null; if (defElement != null) { try { classInstance = _factory.getPropertyValueClass(defElement.getAsString()); } catch (ClassNotFoundException e) { // ignore } } if (classInstance == null) { Map<String, Object> result = new HashMap<String, Object>(); for (Map.Entry<String, JsonElement> entry : object.entrySet()) { Object value = context.deserialize(entry.getValue(), Object.class); result.put(entry.getKey(), value); } return new SimplePropertyValue(result); } else { return context.deserialize(json, classInstance); } } else { throw new JsonParseException("Unsupported JSON type " + json); } }
From source file:org.apache.sling.jms.Json.java
License:Apache License
private static <T> T toMapValue(JsonElement element) { if (element.isJsonObject()) { return (T) toMapValue(element.getAsJsonObject()); } else if (element.isJsonArray()) { return (T) toMapValue(element.getAsJsonArray()); } else if (element.isJsonNull()) { return null; } else if (element.isJsonPrimitive()) { return (T) toMapValue(element.getAsJsonPrimitive()); }//from w w w.ja v a2 s. c o m throw new IllegalArgumentException( "Encountered JsonElement that is not an object, array, primitive or null: " + element); }
From source file:org.apache.synapse.mediators.bsf.ScriptMediator.java
License:Apache License
private void processJSONPayload(MessageContext synCtx, ScriptMessageContext scriptMC) throws ScriptException { if (!(synCtx instanceof Axis2MessageContext)) { return;/*from w w w. j a va2 s . c o m*/ } org.apache.axis2.context.MessageContext messageContext = ((Axis2MessageContext) synCtx) .getAxis2MessageContext(); String jsonString = (String) messageContext.getProperty("JSON_STRING"); Object jsonObject = null; prepareForJSON(scriptMC); if (JsonUtil.hasAJsonPayload(messageContext)) { try { JsonElement o = jsonParser.parse(new JsonReader(JsonUtil.newJsonPayloadReader(messageContext))); // first, check if the stream is valid. if (o.isJsonNull()) { logger.error("#processJSONPayload. JSON stream is not valid."); return; } jsonObject = this.jsEngine.eval(JsonUtil.newJavaScriptSourceReader(messageContext)); } catch (Exception e) { handleException("Failed to get the JSON payload from the input stream. Error>>>\n" + e.getLocalizedMessage()); } } else if (jsonString != null) { String jsonPayload = jsonParser.parse(jsonString).toString(); jsonObject = this.jsEngine.eval('(' + jsonPayload + ')'); } if (jsonObject != null) { scriptMC.setJsonObject(synCtx, jsonObject); } }
From source file:org.apache.tika.eval.tokens.AnalyzerDeserializer.java
License:Apache License
private static CharFilterFactory[] buildCharFilters(JsonElement el, String analyzerName) throws IOException { if (el == null || el.isJsonNull()) { return null; }/* w ww. ja v a2s .co m*/ if (!el.isJsonArray()) { throw new IllegalArgumentException( "Expecting array for charfilters, but got:" + el.toString() + " for " + analyzerName); } JsonArray jsonArray = (JsonArray) el; List<CharFilterFactory> ret = new LinkedList<CharFilterFactory>(); for (JsonElement filterMap : jsonArray) { if (!(filterMap instanceof JsonObject)) { throw new IllegalArgumentException( "Expecting a map with \"factory\" string and \"params\" map in char filter factory;" + " not: " + filterMap.toString() + " in " + analyzerName); } JsonElement factoryEl = ((JsonObject) filterMap).get(FACTORY); if (factoryEl == null || !factoryEl.isJsonPrimitive()) { throw new IllegalArgumentException( "Expecting value for factory in char filter factory builder in:" + analyzerName); } String factoryName = factoryEl.getAsString(); factoryName = factoryName.replaceAll("oala.", "org.apache.lucene.analysis."); JsonElement paramsEl = ((JsonObject) filterMap).get(PARAMS); Map<String, String> params = mapify(paramsEl); String spiName = ""; for (String s : CharFilterFactory.availableCharFilters()) { Class clazz = CharFilterFactory.lookupClass(s); if (clazz.getName().equals(factoryName)) { spiName = s; break; } } if (spiName.equals("")) { throw new IllegalArgumentException( "A SPI class of type org.apache.lucene.analysis.util.CharFilterFactory with name" + "'" + factoryName + "' does not exist."); } try { CharFilterFactory charFilterFactory = CharFilterFactory.forName(spiName, params); if (charFilterFactory instanceof ResourceLoaderAware) { ((ResourceLoaderAware) charFilterFactory) .inform(new ClasspathResourceLoader(AnalyzerDeserializer.class)); } ret.add(charFilterFactory); } catch (IllegalArgumentException e) { throw new IllegalArgumentException("While trying to load " + analyzerName + ": " + e.getMessage(), e); } } if (ret.size() == 0) { return new CharFilterFactory[0]; } return ret.toArray(new CharFilterFactory[ret.size()]); }
From source file:org.apache.tika.eval.tokens.AnalyzerDeserializer.java
License:Apache License
private static TokenFilterFactory[] buildTokenFilterFactories(JsonElement el, String analyzerName) throws IOException { if (el == null || el.isJsonNull()) { return null; }/*from w w w.j av a 2 s . c o m*/ if (!el.isJsonArray()) { throw new IllegalArgumentException( "Expecting array for tokenfilters, but got:" + el.toString() + " in " + analyzerName); } JsonArray jsonArray = (JsonArray) el; List<TokenFilterFactory> ret = new LinkedList<>(); for (JsonElement filterMap : jsonArray) { if (!(filterMap instanceof JsonObject)) { throw new IllegalArgumentException( "Expecting a map with \"factory\" string and \"params\" map in token filter factory;" + " not: " + filterMap.toString() + " in " + analyzerName); } JsonElement factoryEl = ((JsonObject) filterMap).get(FACTORY); if (factoryEl == null || !factoryEl.isJsonPrimitive()) { throw new IllegalArgumentException( "Expecting value for factory in token filter factory builder in " + analyzerName); } String factoryName = factoryEl.getAsString(); factoryName = factoryName.startsWith("oala.") ? factoryName.replaceFirst("oala.", "org.apache.lucene.analysis.") : factoryName; JsonElement paramsEl = ((JsonObject) filterMap).get(PARAMS); Map<String, String> params = mapify(paramsEl); String spiName = ""; for (String s : TokenFilterFactory.availableTokenFilters()) { Class clazz = TokenFilterFactory.lookupClass(s); if (clazz.getName().equals(factoryName)) { spiName = s; break; } } if (spiName.equals("")) { throw new IllegalArgumentException( "A SPI class of type org.apache.lucene.analysis.util.TokenFilterFactory with name" + "'" + factoryName + "' does not exist."); } try { TokenFilterFactory tokenFilterFactory = TokenFilterFactory.forName(spiName, params); if (tokenFilterFactory instanceof ResourceLoaderAware) { ((ResourceLoaderAware) tokenFilterFactory) .inform(new ClasspathResourceLoader(AnalyzerDeserializer.class)); } ret.add(tokenFilterFactory); } catch (IllegalArgumentException e) { throw new IllegalArgumentException("While loading " + analyzerName, e); } } if (ret.size() == 0) { return new TokenFilterFactory[0]; } return ret.toArray(new TokenFilterFactory[ret.size()]); }
From source file:org.apache.tika.eval.tokens.AnalyzerDeserializer.java
License:Apache License
private static Map<String, String> mapify(JsonElement paramsEl) { if (paramsEl == null || paramsEl.isJsonNull()) { return Collections.EMPTY_MAP; }/* ww w . ja v a 2s. com*/ if (!paramsEl.isJsonObject()) { throw new IllegalArgumentException("Expecting map, not: " + paramsEl.toString()); } Map<String, String> params = new HashMap<>(); for (Map.Entry<String, JsonElement> e : ((JsonObject) paramsEl).entrySet()) { JsonElement value = e.getValue(); if (!value.isJsonPrimitive()) { throw new IllegalArgumentException( "Expecting parameter to have primitive value: " + value.toString()); } String v = e.getValue().getAsString(); params.put(e.getKey(), v); } return params; }