Java Code Examples for com.google.gson.JsonPrimitive#isNumber()

The following examples show how to use com.google.gson.JsonPrimitive#isNumber() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ParameterAdapter.java    From activitystreams with Apache License 2.0 6 votes vote down vote up
private Object deserialize(
  JsonDeserializationContext context,
  JsonElement el) {
    if (el.isJsonArray()) {
      return context.deserialize(el, Iterable.class);
    } else if (el.isJsonObject()) {
      return context.deserialize(el, ASObject.class);
    } else if (el.isJsonPrimitive()) {
      JsonPrimitive p = el.getAsJsonPrimitive();
      if (p.isBoolean())
        return p.getAsBoolean();
      else if (p.isNumber())
        return p.getAsNumber();
      else
        return p.getAsString();
    } else return null;
}
 
Example 2
Source File: MapTypeAdapterFactory.java    From framework with GNU Affero General Public License v3.0 6 votes vote down vote up
private String keyToString(JsonElement keyElement) {
  if (keyElement.isJsonPrimitive()) {
    JsonPrimitive primitive = keyElement.getAsJsonPrimitive();
    if (primitive.isNumber()) {
      return String.valueOf(primitive.getAsNumber());
    } else if (primitive.isBoolean()) {
      return Boolean.toString(primitive.getAsBoolean());
    } else if (primitive.isString()) {
      return primitive.getAsString();
    } else {
      throw new AssertionError();
    }
  } else if (keyElement.isJsonNull()) {
    return "null";
  } else {
    throw new AssertionError();
  }
}
 
Example 3
Source File: MapTypeAdapterFactory.java    From letv with Apache License 2.0 6 votes vote down vote up
private String keyToString(JsonElement keyElement) {
    if (keyElement.isJsonPrimitive()) {
        JsonPrimitive primitive = keyElement.getAsJsonPrimitive();
        if (primitive.isNumber()) {
            return String.valueOf(primitive.getAsNumber());
        }
        if (primitive.isBoolean()) {
            return Boolean.toString(primitive.getAsBoolean());
        }
        if (primitive.isString()) {
            return primitive.getAsString();
        }
        throw new AssertionError();
    } else if (keyElement.isJsonNull()) {
        return "null";
    } else {
        throw new AssertionError();
    }
}
 
Example 4
Source File: GsonJsonRpcUnmarshaller.java    From che with Eclipse Public License 2.0 6 votes vote down vote up
private Object getInnerItem(JsonElement jsonElement) {
  if (jsonElement.isJsonNull()) {
    return null;
  }

  if (jsonElement.isJsonObject()) {
    return jsonElement.getAsJsonObject();
  }

  if (jsonElement.isJsonPrimitive()) {
    JsonPrimitive jsonPrimitive = jsonElement.getAsJsonPrimitive();
    if (jsonPrimitive.isNumber()) {
      return jsonPrimitive.getAsDouble();
    } else if (jsonPrimitive.isString()) {
      return jsonPrimitive.getAsString();
    } else {
      return jsonPrimitive.getAsBoolean();
    }
  }

  throw new IllegalStateException("Unexpected json element type");
}
 
Example 5
Source File: JsonUtils.java    From kurento-java with Apache License 2.0 6 votes vote down vote up
public Object toPrimitiveObject(JsonElement element) {

    JsonPrimitive primitive = (JsonPrimitive) element;
    if (primitive.isBoolean()) {
      return Boolean.valueOf(primitive.getAsBoolean());
    } else if (primitive.isNumber()) {
      Number number = primitive.getAsNumber();
      double value = number.doubleValue();
      if ((int) value == value) {
        return Integer.valueOf((int) value);
      } else if ((long) value == value) {
        return Long.valueOf((long) value);
      } else if ((float) value == value) {
        return Float.valueOf((float) value);
      } else {
        return Double.valueOf((double) value);
      }
    } else if (primitive.isString()) {
      return primitive.getAsString();
    } else {
      throw new JsonRpcException("Unrecognized JsonPrimitive: " + primitive);
    }
  }
 
Example 6
Source File: JsonConverter.java    From iotplatform with Apache License 2.0 6 votes vote down vote up
public static List<KvEntry> parseValues(JsonObject valuesObject) {
  List<KvEntry> result = new ArrayList<>();
  for (Entry<String, JsonElement> valueEntry : valuesObject.entrySet()) {
    JsonElement element = valueEntry.getValue();
    if (element.isJsonPrimitive()) {
      JsonPrimitive value = element.getAsJsonPrimitive();
      if (value.isString()) {
        result.add(new StringDataEntry(valueEntry.getKey(), value.getAsString()));
      } else if (value.isBoolean()) {
        result.add(new BooleanDataEntry(valueEntry.getKey(), value.getAsBoolean()));
      } else if (value.isNumber()) {
        if (value.getAsString().contains(".")) {
          result.add(new DoubleDataEntry(valueEntry.getKey(), value.getAsDouble()));
        } else {
          result.add(new LongDataEntry(valueEntry.getKey(), value.getAsLong()));
        }
      } else {
        throw new JsonSyntaxException("Can't parse value: " + value);
      }
    } else {
      throw new JsonSyntaxException("Can't parse value: " + element);
    }
  }
  return result;
}
 
Example 7
Source File: MainView.java    From HiJson with Apache License 2.0 6 votes vote down vote up
private void formatJsonPrimitive(String key, JsonPrimitive pri, DefaultMutableTreeNode pNode) {
    if(pri.isJsonNull()){
        pNode.add(Kit.nullNode(key));
    }else if (pri.isNumber()) {
        pNode.add(Kit.numNode(key ,pri.getAsString()));
    }else if (pri.isBoolean()) {
        pNode.add(Kit.boolNode(key,pri.getAsBoolean()));
    }else if (pri.isString()) {
        pNode.add(Kit.strNode(key, pri.getAsString()));
    }else if(pri.isJsonArray()){
        createJsonArray(pri.getAsJsonArray(),pNode,key);
    }else if(pri.isJsonObject()){
            JsonObject child = pri.getAsJsonObject();
            DefaultMutableTreeNode node = Kit.objNode(key);
            createJsonObject(child,node);
            pNode.add(node);
    }else if(pri.isJsonPrimitive()){
        formatJsonPrimitive(key,pri.getAsJsonPrimitive(),pNode);
    }
}
 
Example 8
Source File: LwM2mNodeDeserializer.java    From SI with BSD 2-Clause "Simplified" License 5 votes vote down vote up
private org.eclipse.leshan.core.model.ResourceModel.Type getTypeFor(JsonPrimitive val) {
    if (val.isBoolean())
        return org.eclipse.leshan.core.model.ResourceModel.Type.BOOLEAN;
    if (val.isString())
        return org.eclipse.leshan.core.model.ResourceModel.Type.STRING;
    if (val.isNumber()) {
        if (val.getAsDouble() == (double) val.getAsLong()) {
            return org.eclipse.leshan.core.model.ResourceModel.Type.INTEGER;
        } else {
            return org.eclipse.leshan.core.model.ResourceModel.Type.FLOAT;
        }
    }
    // use string as default value
    return org.eclipse.leshan.core.model.ResourceModel.Type.STRING;
}
 
Example 9
Source File: GsonObjectDeserializer.java    From qaf with MIT License 5 votes vote down vote up
public static Object read(JsonElement in) {

		if (in.isJsonArray()) {
			List<Object> list = new ArrayList<Object>();
			JsonArray arr = in.getAsJsonArray();
			for (JsonElement anArr : arr) {
				list.add(read(anArr));
			}
			return list;
		} else if (in.isJsonObject()) {
			Map<String, Object> map = new LinkedTreeMap<String, Object>();
			JsonObject obj = in.getAsJsonObject();
			Set<Map.Entry<String, JsonElement>> entitySet = obj.entrySet();
			for (Map.Entry<String, JsonElement> entry : entitySet) {
				map.put(entry.getKey(), read(entry.getValue()));
			}
			return map;
		} else if (in.isJsonPrimitive()) {
			JsonPrimitive prim = in.getAsJsonPrimitive();
			if (prim.isBoolean()) {
				return prim.getAsBoolean();
			} else if (prim.isString()) {
				return prim.getAsString();
			} else if (prim.isNumber()) {
				if (prim.getAsString().contains("."))
					return prim.getAsDouble();
				else {
					return prim.getAsLong();
				}
			}
		}
		return null;
	}
 
Example 10
Source File: JsonUtil.java    From olca-app with Mozilla Public License 2.0 5 votes vote down vote up
private static boolean equal(JsonPrimitive e1, JsonPrimitive e2) {
	if (e1.isBoolean() && e2.isBoolean())
		return e1.getAsBoolean() == e2.getAsBoolean();
	if (e1.isNumber() && e2.isNumber())
		return e1.getAsNumber().doubleValue() == e2.getAsNumber().doubleValue();
	return e1.getAsString().equals(e2.getAsString());
}
 
Example 11
Source File: JsonConverter.java    From external-resources with Apache License 2.0 5 votes vote down vote up
private Resource get(JsonPrimitive primitive) {
  if (primitive.isBoolean()) {
    return new Resource(primitive.getAsBoolean());
  } else if (primitive.isNumber()) {
    return new Resource(primitive.getAsNumber());
  } else {
    return new Resource(primitive.getAsString());
  }
}
 
Example 12
Source File: JsonUtil.java    From java-trader with Apache License 2.0 5 votes vote down vote up
public static Object json2value(JsonElement json) {
    Object result = null;
    if ( json.isJsonNull() ) {
        result = null;
    }else if ( json.isJsonObject() ) {
        JsonObject json0 = (JsonObject)json;
        LinkedHashMap<String, Object> map = new LinkedHashMap<>();
        for(String key:json0.keySet()) {
            map.put(key, json2value(json0.get(key)));
        }
        result = map;
    }else if ( json.isJsonArray() ) {
        JsonArray arr = (JsonArray)json;
        ArrayList<Object> list = new ArrayList<>(arr.size());
        for(int i=0;i<arr.size();i++) {
            list.add(json2value(arr.get(i)));
        }
        result = list;
    } else if ( json.isJsonPrimitive() ) {
        JsonPrimitive p = (JsonPrimitive)json;
        if ( p.isBoolean() ) {
            result = p.getAsBoolean();
        }else if ( p.isNumber() ) {
            result = p.getAsDouble();
        }else if ( p.isString()) {
            result = p.getAsString();
        }else {
            result = p.getAsString();
        }
    }
    return result;
}
 
Example 13
Source File: VectorColumnFiller.java    From secor with Apache License 2.0 5 votes vote down vote up
private JsonType getJsonType(JsonPrimitive value) {
    if (value.isBoolean()) {
        return JsonType.BOOLEAN;
    } else if (value.isNumber()) {
        return JsonType.NUMBER;
    } else if (value.isString()) {
        return JsonType.STRING;
    } else {
        throw new UnsupportedOperationException();
    }
}
 
Example 14
Source File: BetterJsonObject.java    From Hyperium with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * The optional int method, returns the default value if
 * the key is null, empty or the data does not contain
 * the key. This will also return the default value if
 * the data value is not a number
 *
 * @param key the key the value will be loaded from
 * @return the value in the json data set or the default if the key cannot be found
 */
public int optInt(String key, int value) {
    if (key == null || key.isEmpty() || !has(key)) return value;

    JsonPrimitive primitive = asPrimitive(get(key));

    try {
        if (primitive != null && primitive.isNumber()) return primitive.getAsInt();
    } catch (NumberFormatException ignored) {
    }

    return value;
}
 
Example 15
Source File: LuaJsonElement.java    From Lukkit with MIT License 5 votes vote down vote up
private LuaValue getValueFromElement(JsonElement element) {
    if (element.isJsonArray() || element.isJsonObject()) {
        return this.getTableFromElement(element);
    } else if (element.isJsonNull()) {
        return LuaValue.NIL;
    } else if (element.isJsonPrimitive()) {
        JsonPrimitive primitiveValue = element.getAsJsonPrimitive();

        if (primitiveValue.isBoolean()) {
            return LuaValue.valueOf(primitiveValue.getAsBoolean());
        } else if (primitiveValue.isString()) {
            return LuaValue.valueOf(primitiveValue.getAsString());
        } else if (primitiveValue.isNumber()) {
            Number numberValue = primitiveValue.getAsNumber();

            if (numberValue instanceof Double)       return LuaValue.valueOf(numberValue.doubleValue());
            else if (numberValue instanceof Integer) return LuaValue.valueOf(numberValue.intValue());
            else if (numberValue instanceof Short)   return LuaValue.valueOf(numberValue.shortValue());
            else if (numberValue instanceof Long)    return LuaValue.valueOf(numberValue.longValue());
            else if (numberValue instanceof Float)   return LuaValue.valueOf(numberValue.floatValue());
            else if (numberValue instanceof Byte)    return LuaValue.valueOf(numberValue.byteValue());
        }
    } else {
        LuaError error = new LuaError("A LuaJsonElement object was passed an unsupported value other than that supported by LuaJ. Value: " + element.toString());
        LuaEnvironment.addError(error);
        error.printStackTrace();
    }

    return LuaValue.NIL;
}
 
Example 16
Source File: JsonUtil.java    From olca-app with Mozilla Public License 2.0 5 votes vote down vote up
private static JsonPrimitive deepCopy(JsonPrimitive element) {
	if (element.isBoolean())
		return new JsonPrimitive(element.getAsBoolean());
	if (element.isNumber())
		return new JsonPrimitive(element.getAsNumber());
	return new JsonPrimitive(element.getAsString());
}
 
Example 17
Source File: MobileServiceTableBase.java    From azure-mobile-apps-android-client with Apache License 2.0 5 votes vote down vote up
/**
 * @return the numeric value represented by the object.
 *
 * @param o the object for which numberic value is to be extracted
 */
protected long getNumericValue(Object o) {
    long result;

    if (o instanceof Integer) {
        result = (Integer) o;
    } else if (o instanceof Long) {
        result = (Long) o;
    } else if (o instanceof JsonElement) {
        JsonElement json = (JsonElement) o;

        if (json.isJsonPrimitive()) {
            JsonPrimitive primitive = json.getAsJsonPrimitive();

            if (primitive.isNumber()) {
                result = primitive.getAsLong();
            } else {
                throw new IllegalArgumentException("Object does not represent a string value.");
            }
        } else {
            throw new IllegalArgumentException("Object does not represent a string value.");
        }
    } else {
        throw new IllegalArgumentException("Object does not represent a string value.");
    }

    return result;
}
 
Example 18
Source File: TypeAdapters.java    From letv with Apache License 2.0 5 votes vote down vote up
public void write(JsonWriter out, JsonElement value) throws IOException {
    if (value == null || value.isJsonNull()) {
        out.nullValue();
    } else if (value.isJsonPrimitive()) {
        JsonPrimitive primitive = value.getAsJsonPrimitive();
        if (primitive.isNumber()) {
            out.value(primitive.getAsNumber());
        } else if (primitive.isBoolean()) {
            out.value(primitive.getAsBoolean());
        } else {
            out.value(primitive.getAsString());
        }
    } else if (value.isJsonArray()) {
        out.beginArray();
        i$ = value.getAsJsonArray().iterator();
        while (i$.hasNext()) {
            write(out, (JsonElement) i$.next());
        }
        out.endArray();
    } else if (value.isJsonObject()) {
        out.beginObject();
        for (Entry<String, JsonElement> e : value.getAsJsonObject().entrySet()) {
            out.name((String) e.getKey());
            write(out, (JsonElement) e.getValue());
        }
        out.endObject();
    } else {
        throw new IllegalArgumentException("Couldn't write " + value.getClass());
    }
}
 
Example 19
Source File: EventDeserializer.java    From easypost-java with MIT License 5 votes vote down vote up
private Object deserializeJsonPrimitive(JsonPrimitive element) {
	if (element.isBoolean()) {
		return element.getAsBoolean();
	} else if (element.isNumber()) {
		return element.getAsNumber();
	} else {
		return element.getAsString();
	}
}
 
Example 20
Source File: JavaModelFactory.java    From swellrt with Apache License 2.0 4 votes vote down vote up
@Override
public Object traverseJsonObject(Object o, String path) {

  if (o == null)
    return null;

  JsonElement e = (JsonElement) o;

  if (path == null || path.isEmpty()) {

    if (e.isJsonPrimitive()) {
      JsonPrimitive p = (JsonPrimitive) e;

      if (p.isBoolean())
        return new Boolean(p.getAsBoolean());
      else if (p.isNumber())
        return new Double(p.getAsDouble());
      else if (p.isString())
        return new String(p.getAsString());
      else
        return null;

    } else
      return e;
  }

  String propName = path.indexOf(".") != -1 ? path.substring(0, path.indexOf(".")) : path;
  String restPath = path.indexOf(".") != -1 ? path.substring(path.indexOf(".") + 1) : null;

  JsonElement propValue = null;
  if (e.isJsonObject()) {

    JsonObject object = (JsonObject) e;
    propValue = object.get(propName);
    return traverseJsonObject(propValue, restPath);

  } else if (e.isJsonArray()) {
    try {
      int index = Integer.parseInt(propName);
      JsonArray array = (JsonArray) e;
      return traverseJsonObject(array.get(index), restPath);
    } catch (NumberFormatException ex) {
      return null;
    }

  } else if (e.isJsonPrimitive()) {
    return null;
  }


  return null;
}