List of usage examples for com.google.gson JsonElement isJsonPrimitive
public boolean isJsonPrimitive()
From source file:com.relayrides.pushy.apns.DateAsSecondsSinceEpochTypeAdapter.java
License:Open Source License
@Override public Date deserialize(final JsonElement json, final Type typeOfT, final JsonDeserializationContext context) throws JsonParseException { final Date date; if (json.isJsonPrimitive()) { date = new Date(json.getAsLong() * 1000); } else if (json.isJsonNull()) { date = null;//from w w w . j av a 2s . co m } else { throw new JsonParseException( "Dates represented as seconds since the epoch must either be numbers or null."); } return date; }
From source file:com.relayrides.pushy.apns.DateAsTimeSinceEpochTypeAdapter.java
License:Open Source License
@Override public Date deserialize(final JsonElement json, final Type typeOfT, final JsonDeserializationContext context) throws JsonParseException { final Date date; if (json.isJsonPrimitive()) { date = new Date(this.timeUnit.toMillis(json.getAsLong())); } else if (json.isJsonNull()) { date = null;//w w w . j ava 2s . c om } else { throw new JsonParseException( "Dates represented as time since the epoch must either be numbers or null."); } return date; }
From source file:com.rw.legion.input.JsonRecordReader.java
License:Apache License
/** * Recursively traverses all levels of a JSON object and adds their contents * to the <code>LegionRecord</code>. * // w w w .j av a2 s .c o m * @param location The JSON path leading up to the current depth level. * @param element An element that appears at the current depth level. */ private void traverseJson(String location, JsonElement element) { if (element.isJsonNull()) { record.setField(location, ""); } else if (element.isJsonPrimitive()) { record.setField(location, element.getAsString()); } else if (element.isJsonObject()) { for (Map.Entry<String, JsonElement> entry : element.getAsJsonObject().entrySet()) { traverseJson(location + "." + entry.getKey(), entry.getValue()); } } else if (element.isJsonArray()) { for (int i = 0; i < element.getAsJsonArray().size(); i++) { traverseJson(location + "[" + new Integer(i).toString() + "]", element.getAsJsonArray().get(i)); } } }
From source file:com.ryanantkowiak.jOptionsHouseAPI.JsonUtilities.java
License:Open Source License
/** * Recursively print the contents of a GSON JSON element. * //from w w w . j a va 2s .co m * @param element the GSON JSON element to be printed * @param prefix output will be prefixed with this string */ public static void printJson(JsonElement element, String prefix) { if (null == prefix || prefix.isEmpty()) { prefix = ""; } if (null == element || element.isJsonNull()) { System.out.println(prefix + " [null]"); return; } else if (element.isJsonPrimitive()) { JsonPrimitive p = element.getAsJsonPrimitive(); if (p.isBoolean()) { System.out.println(prefix + " [bool=" + p.getAsBoolean() + "]"); } else if (p.isString()) { System.out.println(prefix + " [string='" + p.getAsString() + "']"); } else if (p.isNumber()) { System.out.println(prefix + " [number=" + p.getAsDouble() + "]"); } } else if (element.isJsonArray()) { System.out.println(prefix + " [array]"); for (int i = 0; i < element.getAsJsonArray().size(); ++i) { String newPrefix = prefix + "[" + i + "]"; printJson(element.getAsJsonArray().get(i), newPrefix); } } else if (element.isJsonObject()) { JsonObject obj = element.getAsJsonObject(); for (Map.Entry<String, JsonElement> entry : obj.entrySet()) { String key = entry.getKey(); JsonElement value = entry.getValue(); String newPrefix = prefix + "." + key; printJson(value, newPrefix); } } }
From source file:com.samsung.memoryanalysis.traceparser.TraceAnalysisRunner.java
License:Open Source License
private FreeVariables buildFVMap(File dir) throws IOException { File fvFile = new File(dir, "freevars.json"); FreeVariables res = new FreeVariables(); if (fvFile.exists()) { JsonObject json = null;/*from w w w . j av a 2 s. co m*/ try { json = parser.parse(new BufferedReader(new FileReader(fvFile))).getAsJsonObject();// parser.parse(js.get()); } catch (JsonParseException ex) { throw new IOException("Invalid free-variables map file: " + ex.getMessage()); } for (Map.Entry<String, JsonElement> entry : json.entrySet()) { JsonElement o = entry.getValue(); if (o.isJsonPrimitive() && o.getAsJsonPrimitive().isString()) res.put(Integer.parseInt(entry.getKey()), FreeVariables.ANY); else { JsonArray a = o.getAsJsonArray(); final Set<String> freeVars = HashSetFactory.make(); for (int i = 0; i < a.size(); i++) { final JsonElement jsonElement = a.get(i); freeVars.add(jsonElement.getAsString()); } res.put(Integer.parseInt(entry.getKey()), freeVars); } } } return res; }
From source file:com.savoirtech.json.rules.impl.RegexMatchingRule.java
License:Apache License
private String getStringForComparison(JsonElement ele) { if (ele.isJsonPrimitive()) { return ele.getAsString(); }/*from www.ja va 2 s .com*/ return ele.toString(); }
From source file:com.seleritycorp.common.base.config.ConfigUtils.java
License:Apache License
/** * Adds a JSON element to a Config.//w w w . j a v a2 s. co m * * @param element The element to add * @param config The config instance to add the element to * @param key The key in the config space */ private static void loadJson(JsonElement element, ConfigImpl config, String key) { if (element.isJsonObject()) { loadJson(element.getAsJsonObject(), config, key); } else if (element.isJsonArray()) { loadJson(element.getAsJsonArray(), config, key); } else if (element.isJsonPrimitive()) { loadJson(element.getAsJsonPrimitive(), config, key); } else if (element.isJsonNull()) { // null does not need a dedicated representation, so we // skip this case. } else { throw new UnsupportedOperationException("Unimplemented " + "JsonElement state"); } }
From source file:com.shazam.shazamcrest.FieldsIgnorer.java
License:Apache License
private static void findPath(JsonElement jsonElement, String pathToFind, final List<String> pathSegments) { String field = headOf(pathSegments); if (pathSegments.size() == 1) { if (jsonElement.isJsonPrimitive()) { throw new IllegalArgumentException(); }/*from w w w. j a v a 2s . c o m*/ ignorePath(jsonElement, pathToFind); } else { JsonElement child = jsonElement.getAsJsonObject().get(field); List<String> tail = pathSegments.subList(1, pathSegments.size()); if (child == null) { return; } if (child.isJsonArray()) { Iterator<JsonElement> iterator = child.getAsJsonArray().iterator(); while (iterator.hasNext()) { findPath((JsonElement) iterator.next(), pathToFind, tail); } } else { findPath(child, pathToFind, tail); } } }
From source file:com.shazam.shazamcrest.matcher.DiagnosingCustomisableMatcher.java
License:Apache License
private void appendFieldJsonSnippet(Object actual, Description mismatchDescription, Gson gson) { JsonElement jsonTree = gson.toJsonTree(actual); if (!jsonTree.isJsonPrimitive() && !jsonTree.isJsonNull()) { mismatchDescription.appendText("\n" + gson.toJson(actual)); }/* w w w. ja v a 2 s . c om*/ }
From source file:com.simiacryptus.mindseye.layers.aparapi.ConvolutionLayer.java
License:Apache License
/** * Instantiates a new Convolution key.// w w w . j a va 2 s. co m * * @param json the json * @param resources the resources */ protected ConvolutionLayer(@Nonnull final JsonObject json, Map<CharSequence, byte[]> resources) { super(json); kernel = Tensor.fromJson(json.get("filter"), resources); JsonElement paddingX = json.get("paddingX"); if (null != paddingX && paddingX.isJsonPrimitive()) this.setPaddingX((paddingX.getAsInt())); JsonElement paddingY = json.get("paddingY"); if (null != paddingY && paddingY.isJsonPrimitive()) this.setPaddingY((paddingY.getAsInt())); }