List of usage examples for com.google.gson JsonElement isJsonPrimitive
public boolean isJsonPrimitive()
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"); }/* w w w.j ava2s . c o 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.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); }//from www . j av a2s .c om } 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); }/*from w ww .j a v a 2 s . c o m*/ } 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 v a 2s . 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()); }// ww w .ja v a 2s . com throw new IllegalArgumentException( "Encountered JsonElement that is not an object, array, primitive or null: " + element); }
From source file:org.apache.sling.query.mock.json.JsonToResource.java
License:Apache License
private static Resource parseResource(JsonElement object, String name, Resource parent) { if (object.isJsonArray()) { return parseResource(object.getAsJsonArray(), name, parent); } else if (object.isJsonPrimitive()) { return parseResource(object.getAsJsonPrimitive(), name, parent); } else if (object.isJsonObject()) { return parseResource(object.getAsJsonObject(), name, parent); } else {//from w ww.j av a 2s .co m return null; } }
From source file:org.apache.synapse.util.xpath.SynapseJsonPath.java
License:Apache License
/** * JayWay json-path response have additional elements like "members"(for objects) and "elements"(for arrays) * This method will correct such strings by removing additional elements. * * @param input input jsonElement./*from w w w .j a va 2 s. c o m*/ * @return corrected jsonObject. */ private Object formatJsonPathResponse(Object input) { JsonElement jsonElement = (JsonElement) input; if (jsonElement.isJsonPrimitive()) { return jsonElement.getAsString(); } else if (jsonElement.isJsonObject()) { JsonObject jsonObject = jsonElement.getAsJsonObject(); if (jsonObject.has("members")) { return jsonObject.get("members"); } else if (jsonObject.has("elements")) { return jsonObject.get("elements"); } return jsonObject.toString(); } return jsonElement.isJsonArray() ? jsonElement : null; }
From source file:org.apache.tika.eval.tokens.AnalyzerDeserializer.java
License:Apache License
private static TokenizerFactory buildTokenizerFactory(JsonElement map, String analyzerName) throws IOException { if (!(map instanceof JsonObject)) { throw new IllegalArgumentException("Expecting a map with \"factory\" string and " + "\"params\" map in tokenizer factory;" + " not: " + map.toString() + " in " + analyzerName); }//from w w w . jav a 2s. c o m JsonElement factoryEl = ((JsonObject) map).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.startsWith("oala.") ? factoryName.replaceFirst("oala.", "org.apache.lucene.analysis.") : factoryName; JsonElement paramsEl = ((JsonObject) map).get(PARAMS); Map<String, String> params = mapify(paramsEl); String spiName = ""; for (String s : TokenizerFactory.availableTokenizers()) { Class clazz = TokenizerFactory.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.TokenizerFactory with name" + "'" + factoryName + "' does not exist."); } try { TokenizerFactory tokenizerFactory = TokenizerFactory.forName(spiName, params); if (tokenizerFactory instanceof ResourceLoaderAware) { ((ResourceLoaderAware) tokenizerFactory) .inform(new ClasspathResourceLoader(AnalyzerDeserializer.class)); } return tokenizerFactory; } catch (IllegalArgumentException e) { throw new IllegalArgumentException("While working on " + analyzerName, e); } }
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; }//from ww w . j a va 2s . c o 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 . ja v a2 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()]); }