com.google.gson.JsonPrimitive#getAsString ( )源码实例Demo

下面列出了com.google.gson.JsonPrimitive#getAsString ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

源代码1 项目: che   文件: GsonJsonRpcUnmarshaller.java
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");
}
 
源代码2 项目: the-hallow   文件: FluidRecipeSerializer.java
/**
 * Attempts to extract a {@link Item} from the given JsonObject.
 * Assumes we're inside the top level of a result block:
 *
 *  "result": {
 *    "item": "minecraft:cobblestone",
 *    "count": 2
 *  }
 *
 * If the Item does not exist in {@link Registry#ITEM}, an exception is thrown and {@link Items#AIR} is returned.
 *
 * @param itemJson JsonObject to extract Item from
 * @return Item extracted from Json
 */
private Item getItem(JsonObject itemJson) {
	Item result;

	if(itemJson.get(ITEM_KEY).isJsonPrimitive()) {
		JsonPrimitive itemPrimitive = itemJson.getAsJsonPrimitive(ITEM_KEY);

		if(itemPrimitive.isString()) {
			Identifier itemIdentifier = new Identifier(itemPrimitive.getAsString());
			
			Optional<Item> opt = Registry.ITEM.getOrEmpty(itemIdentifier);
			if(opt.isPresent()) {
				result = opt.get();
			} else {
				throw new IllegalArgumentException("Item registry does not contain " + itemIdentifier.toString() + "!" + "\n" + prettyPrintJson(itemJson));
			}
		} else {
			throw new IllegalArgumentException("Expected JsonPrimitive to be a String, got " + itemPrimitive.getAsString() + "\n" + prettyPrintJson(itemJson));
		}
	} else {
		throw new InvalidJsonException("\"" + ITEM_KEY + "\" needs to be a String JsonPrimitive, found " + itemJson.getClass() + "!\n" + prettyPrintJson(itemJson));
	}

	return result;
}
 
源代码3 项目: kurento-java   文件: BasicJsonUtils.java
private static Object convertValue(JsonElement value) {
  if (value.isJsonNull()) {
    return null;
  } else if (value.isJsonPrimitive()) {
    JsonPrimitive prim = value.getAsJsonPrimitive();
    if (prim.isBoolean()) {
      return prim.getAsBoolean();
    } else if (prim.isNumber()) {
      Number n = prim.getAsNumber();
      if (n.doubleValue() == n.intValue()) {
        return n.intValue();
      } else {
        return n.doubleValue();
      }
    } else if (prim.isString()) {
      return prim.getAsString();
    } else {
      throw new RuntimeException("Unrecognized value: " + value);
    }
  } else {
    return value.toString();
  }
}
 
源代码4 项目: java-cloudant   文件: ReplicatorDocument.java
private String getEndpointUrl(JsonElement element) {
    if (element == null) {
        return null;
    }
    JsonPrimitive urlString = null;
    if (element.isJsonPrimitive()) {
        urlString = element.getAsJsonPrimitive();
    } else {
        JsonObject replicatorEndpointObject = element.getAsJsonObject();
        urlString = replicatorEndpointObject.getAsJsonPrimitive("url");
    }
    if (urlString == null) {
        return null;
    }
    return urlString.getAsString();
}
 
源代码5 项目: kurento-java   文件: JsonUtils.java
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);
    }
  }
 
源代码6 项目: activitystreams   文件: ParameterAdapter.java
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;
}
 
源代码7 项目: letv   文件: MapTypeAdapterFactory.java
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();
    }
}
 
源代码8 项目: QuickShop-Reremake   文件: AssetJson.java
@Nullable
public String getLanguageHash(@NotNull String languageCode) {
    languageCode = languageCode.replace("-", "_").toLowerCase().trim();
    JsonObject json = new JsonParser().parse(this.gameAssets).getAsJsonObject();
    if (json == null || json.isJsonNull()) {
        Util.debugLog("Cannot parse the json: " + this.gameAssets);
        return null;
    }
    JsonElement obje = json.get("objects");
    if (obje == null) {
        Util.debugLog("Json element is null for json " + this.gameAssets);
        return null;
    }
    JsonObject objs = obje.getAsJsonObject();
    if (objs == null || objs.isJsonNull()) {
        Util.debugLog("Json object is null.");
        return null;
    }
    JsonObject langObj = objs.getAsJsonObject(MsgUtil.fillArgs(pathTemplate, languageCode));
    if (langObj == null || langObj.isJsonNull()) {
        Util.debugLog("Cannot find request path.");
        Util.debugLog(this.gameAssets);
        return null;
    }
    JsonPrimitive hashObj = langObj.getAsJsonPrimitive("hash");
    if (hashObj == null || hashObj.isJsonNull()) {
        Util.debugLog("Cannot get hash.");
        return null;
    }
    return hashObj.getAsString();
}
 
源代码9 项目: smarthome   文件: ConfigurationDeserializer.java
private Object deserialize(JsonPrimitive primitive) {
    if (primitive.isString()) {
        return primitive.getAsString();
    } else if (primitive.isNumber()) {
        return primitive.getAsBigDecimal();
    } else if (primitive.isBoolean()) {
        return primitive.getAsBoolean();
    } else {
        throw new IllegalArgumentException("Unsupported primitive: " + primitive);
    }
}
 
源代码10 项目: flutter-intellij   文件: DaemonEvent.java
/**
 * Parses an event and sends it to the listener.
 */
static void dispatch(@NotNull JsonObject obj, @NotNull Listener listener) {
  final JsonPrimitive primEvent = obj.getAsJsonPrimitive("event");
  if (primEvent == null) {
    LOG.info("Missing event field in JSON from flutter process: " + obj);
    return;
  }

  final String eventName = primEvent.getAsString();
  if (eventName == null) {
    LOG.info("Unexpected event field in JSON from flutter process: " + obj);
    return;
  }

  final JsonObject params = obj.getAsJsonObject("params");
  if (params == null) {
    LOG.info("Missing parameters in event from flutter process: " + obj);
    return;
  }

  final DaemonEvent event = create(eventName, params);
  if (event == null) {
    return; // Drop unknown event.
  }

  event.accept(listener);
}
 
源代码11 项目: qaf   文件: GsonObjectDeserializer.java
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;
	}
 
源代码12 项目: yawp   文件: QueryOptions.java
private Object getJsonObjectValue(JsonElement jsonElement) {
    if (jsonElement.isJsonArray()) {
        return getJsonObjectValueForArrays(jsonElement);
    }
    if (jsonElement.isJsonNull()) {
        return null;
    }

    JsonPrimitive jsonPrimitive = jsonElement.getAsJsonPrimitive();

    if (jsonPrimitive.isNumber()) {
        if (jsonPrimitive.getAsString().indexOf(".") != -1) {
            return jsonPrimitive.getAsDouble();
        }
        return jsonPrimitive.getAsLong();
    }

    if (jsonPrimitive.isString()) {
        return jsonPrimitive.getAsString();
    }

    if (jsonPrimitive.isBoolean()) {
        return jsonPrimitive.getAsBoolean();
    }

    // TODO timestamp
    throw new RuntimeException("Invalid json value: " + jsonPrimitive.getAsString());
}
 
源代码13 项目: activitystreams   文件: ASObjectAdapter.java
@Override
protected Object doForward(JsonPrimitive b) {
  if (b.isBoolean())
    return b.getAsBoolean();
  else if (b.isNumber())
    return b.getAsNumber();
  else 
    return b.getAsString();
}
 
源代码14 项目: java-trader   文件: JsonUtil.java
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;
}
 
源代码15 项目: helper   文件: AbstractGsonConverter.java
@Override
public Object unwarpPrimitive(JsonPrimitive primitive) {
    if (primitive.isBoolean()) {
        return primitive.getAsBoolean();
    } else if (primitive.isNumber()) {
        return primitive.getAsNumber();
    } else if (primitive.isString()) {
        return primitive.getAsString();
    } else {
        throw new IllegalArgumentException("Unknown primitive type: " + primitive);
    }
}
 
源代码16 项目: cs-actions   文件: ConvertJsonToXmlService.java
private String getJsonPrimitiveValue(final JsonPrimitive jsonPrimitive) {
    if (jsonPrimitive.isNumber()) {
        return jsonPrimitive.getAsNumber().toString();
    } else if (jsonPrimitive.isBoolean()) {
        return Boolean.toString(jsonPrimitive.getAsBoolean());
    }
    return jsonPrimitive.getAsString();
}
 
源代码17 项目: reasonml-idea-plugin   文件: BsConfigReader.java
@Nullable
private static String parseSourceItem(@NotNull JsonObject obj, @NotNull String type) {
    JsonPrimitive srcProp = obj.getAsJsonPrimitive("dir");
    if (srcProp != null) {
        JsonPrimitive typeProp = obj.getAsJsonPrimitive("type");
        String typeValue = typeProp != null && typeProp.isString() ? typeProp.getAsString() : "";
        if (type.equals(typeValue)) {
            if (srcProp.isString()) {
                return srcProp.getAsString();
            }
        }
    }
    return null;
}
 
源代码18 项目: swellrt   文件: JavaModelFactory.java
@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;
}
 
源代码19 项目: v20-java   文件: TransactionAdapter.java
@Override
public Transaction deserialize(JsonElement json, Type arg1,
        JsonDeserializationContext context) throws JsonParseException {
    JsonObject jsonObject =  json.getAsJsonObject();
    JsonPrimitive prim = (JsonPrimitive) jsonObject.get("type");
    String typestring = prim.getAsString();
    TransactionType type = TransactionType.valueOf(typestring);

    switch (type) {
    case MARKET_ORDER:
        return context.deserialize(json, MarketOrderTransaction.class);
    case ORDER_FILL:
        return context.deserialize(json, OrderFillTransaction.class);
    case ORDER_CANCEL:
        return context.deserialize(json, OrderCancelTransaction.class);
    case MARKET_ORDER_REJECT:
        return context.deserialize(json, MarketOrderRejectTransaction.class);
    case TRADE_CLIENT_EXTENSIONS_MODIFY:
        return context.deserialize(json, TradeClientExtensionsModifyTransaction.class);
    case TRADE_CLIENT_EXTENSIONS_MODIFY_REJECT:
        return context.deserialize(json, TradeClientExtensionsModifyRejectTransaction.class);
    case TAKE_PROFIT_ORDER:
        return context.deserialize(json, TakeProfitOrderTransaction.class);
    case STOP_LOSS_ORDER:
        return context.deserialize(json, StopLossOrderTransaction.class);
    case TRAILING_STOP_LOSS_ORDER:
        return context.deserialize(json, TrailingStopLossOrderTransaction.class);
    case ORDER_CANCEL_REJECT:
        return context.deserialize(json, OrderCancelRejectTransaction.class);
    case TAKE_PROFIT_ORDER_REJECT:
        return context.deserialize(json, TakeProfitOrderRejectTransaction.class);
    case STOP_LOSS_ORDER_REJECT:
        return context.deserialize(json, StopLossOrderRejectTransaction.class);
    case TRAILING_STOP_LOSS_ORDER_REJECT:
        return context.deserialize(json, TrailingStopLossOrderRejectTransaction.class);
    case CLIENT_CONFIGURE:
        return context.deserialize(json, ClientConfigureTransaction.class);
    case CLIENT_CONFIGURE_REJECT:
        return context.deserialize(json, ClientConfigureRejectTransaction.class);
    case CREATE:
        return context.deserialize(json, CreateTransaction.class);
    case CLOSE:
        return context.deserialize(json, CloseTransaction.class);
    case REOPEN:
        return context.deserialize(json, ReopenTransaction.class);
    case TRANSFER_FUNDS:
        return context.deserialize(json, TransferFundsTransaction.class);
    case TRANSFER_FUNDS_REJECT:
        return context.deserialize(json, TransferFundsRejectTransaction.class);
    case FIXED_PRICE_ORDER:
        return context.deserialize(json, FixedPriceOrderTransaction.class);
    case LIMIT_ORDER:
        return context.deserialize(json, LimitOrderTransaction.class);
    case LIMIT_ORDER_REJECT:
        return context.deserialize(json, LimitOrderRejectTransaction.class);
    case STOP_ORDER:
        return context.deserialize(json, StopOrderTransaction.class);
    case STOP_ORDER_REJECT:
        return context.deserialize(json, StopOrderRejectTransaction.class);
    case MARKET_IF_TOUCHED_ORDER:
        return context.deserialize(json, MarketIfTouchedOrderTransaction.class);
    case MARKET_IF_TOUCHED_ORDER_REJECT:
        return context.deserialize(json, MarketIfTouchedOrderRejectTransaction.class);
    case ORDER_CLIENT_EXTENSIONS_MODIFY:
        return context.deserialize(json, OrderClientExtensionsModifyTransaction.class);
    case ORDER_CLIENT_EXTENSIONS_MODIFY_REJECT:
        return context.deserialize(json, OrderClientExtensionsModifyRejectTransaction.class);
    case MARGIN_CALL_ENTER:
        return context.deserialize(json, MarginCallEnterTransaction.class);
    case MARGIN_CALL_EXTEND:
        return context.deserialize(json, MarginCallExtendTransaction.class);
    case MARGIN_CALL_EXIT:
        return context.deserialize(json, MarginCallExitTransaction.class);
    case DELAYED_TRADE_CLOSURE:
        return context.deserialize(json, DelayedTradeClosureTransaction.class);
    case DAILY_FINANCING:
        return context.deserialize(json, DailyFinancingTransaction.class);
    case RESET_RESETTABLE_PL:
        return context.deserialize(json, ResetResettablePLTransaction.class);
    }
    return null;
}
 
源代码20 项目: Hyperium   文件: BetterJsonObject.java
/**
 * The optional string 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 string
 *
 * @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 String optString(String key, String value) {
    if (key == null || key.isEmpty() || !has(key)) return value;
    JsonPrimitive primitive = asPrimitive(get(key));
    return primitive != null && primitive.isString() ? primitive.getAsString() : value;
}