Android JsonPrimitive - top ranked examples from Open Source projects

These code examples were ranked by Codota’s semantic indexing as the best open source examples for Android JsonPrimitive class.

This code example shows how to use the following methods:
public class UserSerializer implements JsonSerializer<User> { 
 
    @Override 
    public JsonElement serialize(final User src, final Type type, final JsonSerializationContext jsonSerializationContext) { 
        final String j = GsonFactory.getInstance().toJson(src); 
        return new JsonPrimitive(j); 
    } 
} 
7
CodeRank
Connect your IDE to all the code out there  Get Codota for Java
This code example shows how to use the following methods:getAsString, isBoolean, isString
		return map;
	} 
	 
	 
	public Object json2JavaPrimitive(JsonPrimitive prim) {
		if(prim.isBoolean()) {
			return prim.getAsBoolean();
		} else if (prim.isString()) {
			return prim.getAsString();
		} else if (prim.isNumber()) {
			return prim.getAsNumber();
		} else { 
			throw new IllegalStateException();
		} 
	} 
 
} 
7
CodeRank
Connect your IDE to all the code out there  Get Codota for Java
This code example shows how to use the following methods:
public class ValueSerializer implements JsonSerializer<Value> { 
 
    @Override 
    public JsonElement serialize(Value src, Type typeOfSrc, JsonSerializationContext context) { 
        final String j = GsonFactory.getSimpleInstance().toJson(src); 
        return new JsonPrimitive(j); 
    } 
} 
7
CodeRank
Connect your IDE to all the code out there  Get Codota for Java
This code example shows how to use the following methods:
public class EntitySerializer implements JsonSerializer<Entity> { 
 
    @Override 
    public JsonElement serialize(final Entity src, final Type type, final JsonSerializationContext jsonSerializationContext) { 
        final String j = GsonFactory.getSimpleInstance().toJson(src); 
        return new JsonPrimitive(j); 
    } 
} 
7
CodeRank
Connect your IDE to all the code out there  Get Codota for Java
This code example shows how to use the following methods:getAsString, isBoolean, isString
		else 
			return handleObject(json.getAsJsonObject(), context);
	} 
 
	private Object handlePrimitive(JsonPrimitive json) {
		if (json.isBoolean())
			return json.getAsBoolean();
		else if (json.isString())
			return json.getAsString();
		else { 
			BigDecimal bigDec = json.getAsBigDecimal();
			// Find out if it is an int type 
			try { 
				bigDec.toBigIntegerExact();
				try { 
					return bigDec.intValueExact();
				} catch (ArithmeticException e) {
				} 
				return bigDec.longValue();
			} catch (ArithmeticException e) {
7
CodeRank
Connect your IDE to all the code out there  Get Codota for Java
This code example shows how to use the following methods:
public class AccessKeySerializer implements JsonSerializer<AccessKey> { 
 
    @Override 
    public JsonElement serialize(final AccessKey src, final Type type, final JsonSerializationContext jsonSerializationContext) { 
        final String j = GsonFactory.getSimpleInstance().toJson(src); 
        return new JsonPrimitive(j); 
    } 
} 
7
CodeRank
Connect your IDE to all the code out there  Get Codota for Java
This code example shows how to use the following methods:
  public static void getAPILevel(final ForgeTask task) { 
    task.success(new JsonPrimitive(android.os.Build.VERSION.SDK_INT)); 
  } 
 
  public static void getVersionString(final ForgeTask task) { 
    task.success(new JsonPrimitive(android.os.Build.VERSION.RELEASE)); 
  } 
 
  public static void getDeviceName(final ForgeTask task) { 
    task.success(new JsonPrimitive(android.os.Build.MODEL)); 
  } 
} 
6
CodeRank
Connect your IDE to all the code out there  Get Codota for Java
This code example shows how to use the following methods:isBoolean, isNumber, isString
			if (element.isJsonNull()) {
				return true; 
			} 
			if (element.isJsonPrimitive()) {
				JsonPrimitive primitive = element.getAsJsonPrimitive();
				if (primitive.isBoolean()) {
					return !primitive.getAsBoolean();
				} 
				if (primitive.isNumber()) {
					return primitive.getAsBigDecimal().compareTo(
							BigDecimal.ZERO) == 0;
				} 
				if (primitive.isString()) {
					return primitive.getAsString().length() == 0;
				} 
			} 
		} 
		if (obj instanceof Boolean) {
			return !(Boolean) obj;
		} 
6
CodeRank
Connect your IDE to all the code out there  Get Codota for Java
This code example shows how to use the following methods:getAsString, isBoolean, isString
	    else if(json.isJsonPrimitive()) return handlePrimitive(json.getAsJsonPrimitive());
	    else if(json.isJsonArray()) return handleArray(json.getAsJsonArray(), context);
	    else return handleObject(json.getAsJsonObject(), context);
	  } 
	  private Object handlePrimitive(JsonPrimitive json) {
	    if(json.isBoolean())
	      return json.getAsBoolean();
	    else if(json.isString())
	      return json.getAsString();
	    else { 
	      BigDecimal bigDec = json.getAsBigDecimal();
	      // Find out if it is an int type 
	      try { 
	        bigDec.toBigIntegerExact();
	        try { return bigDec.intValueExact(); }
	        catch(ArithmeticException e) {}
	        return bigDec.longValue();
	      } catch(ArithmeticException e) {}
	      // Just return it as a double 
	      return bigDec.doubleValue();
6
CodeRank
Connect your IDE to all the code out there  Get Codota for Java
This code example shows how to use the following methods:getAsNumber, isBoolean, isNumber
		return (T)parse(element.getAsJsonObject());
	} 
	 
	@SuppressWarnings("unchecked") 
	private <T> T parse(JsonPrimitive element){
		if(element.isBoolean()) return (T) deserializer.fromJson(element, Boolean.class);
		if(element.isNumber()) return (T) element.getAsNumber();
		if(element.isString()) return (T) deserializer.fromJson(element, String.class);
		return null; 
	} 
	 
	@SuppressWarnings("unchecked") 
	private <T> T parse(JsonArray element){
		//String className = cleanClassName(element.get(element.size()-1)); 
		return (T) deserializer.fromJson(element, Collection.class);
	} 
	 
	@SuppressWarnings("unchecked") 
	private <T> T parse(JsonObject element){
		String className = cleanClassName(element.remove("class"));
5
CodeRank
Connect your IDE to all the code out there  Get Codota for Java

Related Android JsonPrimitive Questions & Answers:

See Code Examples for Android JsonPrimitive Methods: