本文整理汇总了Java中com.google.gson.JsonElement类的典型用法代码示例。如果您正苦于以下问题:Java JsonElement类的具体用法?Java JsonElement怎么用?Java JsonElement使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
JsonElement类属于com.google.gson包,在下文中一共展示了JsonElement类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getAllClients
import com.google.gson.JsonElement; //导入依赖的package包/类
public static Observable<List<Client>> getAllClients() {
ClientsService service = ServiceGenerator.createService(ClientsService.class);
return service.getAllClients(UrlManager.getAllClientsURL())
.flatMap(new Function<JsonElement, Observable<List<Client>>>() {
@Override
public Observable<List<Client>> apply(JsonElement jsonElement) throws Exception {
if(jsonElement != null) {
Log.i("Get All Clients" , "JSON: "+jsonElement.toString());
if(jsonElement.isJsonArray()) {
List<Client> clients = Client.ClientsListParser.fromJsonArray(jsonElement.getAsJsonArray());
return Observable.just(clients);
} else {
return Observable.error(new Exception("Expected a JSON Array"));
}
} else {
return Observable.just((List<Client>) new ArrayList<Client>());
}
}
}).observeOn(AndroidSchedulers.mainThread());
}
示例2: handleMergeConflict
import com.google.gson.JsonElement; //导入依赖的package包/类
private static void handleMergeConflict(String key, JsonObject leftObj, JsonElement leftVal, JsonElement rightVal, ConflictStrategy conflictStrategy)
throws JsonObjectExtensionConflictException {
{
switch (conflictStrategy) {
case PREFER_FIRST_OBJ:
break;//do nothing, the right val gets thrown out
case PREFER_SECOND_OBJ:
leftObj.add(key, rightVal);//right side auto-wins, replace left val with its val
break;
case PREFER_NON_NULL:
//check if right side is not null, and left side is null, in which case we use the right val
if (leftVal.isJsonNull() && !rightVal.isJsonNull()) {
leftObj.add(key, rightVal);
}//else do nothing since either the left value is non-null or the right value is null
break;
case THROW_EXCEPTION:
throw new JsonObjectExtensionConflictException("Key " + key + " exists in both objects and the conflict resolution strategy is " + conflictStrategy);
default:
throw new UnsupportedOperationException("The conflict strategy " + conflictStrategy + " is unknown and cannot be processed");
}
}
}
示例3: getTextures
import com.google.gson.JsonElement; //导入依赖的package包/类
private Map<String, String> getTextures(JsonObject object)
{
Map<String, String> map = Maps.<String, String>newHashMap();
if (object.has("textures"))
{
JsonObject jsonobject = object.getAsJsonObject("textures");
for (Entry<String, JsonElement> entry : jsonobject.entrySet())
{
map.put(entry.getKey(), ((JsonElement)entry.getValue()).getAsString());
}
}
return map;
}
示例4: toList
import com.google.gson.JsonElement; //导入依赖的package包/类
static <T> List<T> toList(PickleTable dataTable, Type itemType, JsonElement testData) {
List<T> result = new ArrayList<T>();
List<String> keys = convertTopCellsToFieldNames(raw(dataTable.getRows().get(0)));
int count = dataTable.getRows().size();
for (int i = 1; i < count; i++) {
List<String> valueRow = raw(dataTable.getRows().get(i));
T item = (T) SuperReflect.on((Class) itemType).create().get();
int j = 0;
for (String cell : valueRow) {
SuperReflect.on(item).set(keys.get(j), cell);
j++;
}
result.add(item);
}
return Collections.unmodifiableList(result);
}
示例5: deserialize
import com.google.gson.JsonElement; //导入依赖的package包/类
public SetAttributes deserialize(JsonObject object, JsonDeserializationContext deserializationContext, LootCondition[] conditionsIn)
{
JsonArray jsonarray = JsonUtils.getJsonArray(object, "modifiers");
SetAttributes.Modifier[] asetattributes$modifier = new SetAttributes.Modifier[jsonarray.size()];
int i = 0;
for (JsonElement jsonelement : jsonarray)
{
asetattributes$modifier[i++] = SetAttributes.Modifier.deserialize(JsonUtils.getJsonObject(jsonelement, "modifier"), deserializationContext);
}
if (asetattributes$modifier.length == 0)
{
throw new JsonSyntaxException("Invalid attribute modifiers array; cannot be empty");
}
else
{
return new SetAttributes(conditionsIn, asetattributes$modifier);
}
}
示例6: getSpecimenMappingAttributes
import com.google.gson.JsonElement; //导入依赖的package包/类
public static SpecimenFhirMapping getSpecimenMappingAttributes (String filePath) throws Exception
{
SpecimenFhirMapping specimenAttributeMapping=new SpecimenFhirMapping();
Gson gson=new Gson();
try {
JsonElement json=gson.fromJson(new FileReader(filePath), JsonElement.class);
String jsonAttributeMappingString=((JsonObject)json).get("specimen_attribute_mapping").toString();
specimenAttributeMapping=gson.fromJson(jsonAttributeMappingString,SpecimenFhirMapping.class);
//System.out.print(0);
}
catch (Exception exc)
{
throw new Exception(exc.toString());
}
return specimenAttributeMapping;
}
示例7: LoadPersonen
import com.google.gson.JsonElement; //导入依赖的package包/类
public void LoadPersonen(String path) throws FileNotFoundException {
parser = new JsonParser();
Object obj = parser.parse(new FileReader(path));
JsonArray personen = (JsonArray) obj;
for (JsonElement j : personen) {
JsonObject jsonObject = j.getAsJsonObject();
PersonenSammelung.Personen.add(
new Person(
jsonObject.get("id").getAsString(),
jsonObject.get("name").getAsString(),
jsonObject.get("vorname").getAsString(),
jsonObject.get("email").getAsString(),
jsonObject.get("telefonnummer").getAsString()
)
);
}
}
示例8: makeConfigParam
import com.google.gson.JsonElement; //导入依赖的package包/类
private ConfigurationParam makeConfigParam(String key, JsonElement value) {
if (value.isJsonNull()) {
throw new NullPointerException("value for key '" + key + "' is null!");
}
ConfigurationParam param = new ConfigurationParam();
param.setKey(key);
String valueStr;
if (value.isJsonPrimitive() && value.getAsJsonPrimitive().isString()) {
// toString would return the string with quotes around it which we don't want
valueStr = value.getAsString();
} else {
valueStr = value.toString();
}
param.setValue(valueStr);
return param;
}
示例9: fromJsonElement
import com.google.gson.JsonElement; //导入依赖的package包/类
public static Branch fromJsonElement(JsonElement jsonElement) {
Branch branch = new Branch();
if(!JsonHelper.isNull(jsonElement ,"brCode")) {
branch.setCodeBr(jsonElement.getAsJsonObject().get("brCode").getAsInt());
}
if(!JsonHelper.isNull(jsonElement ,"brName")) {
branch.setNameBr(jsonElement.getAsJsonObject().get("brName").getAsString());
}
if(!JsonHelper.isNull(jsonElement ,"brTel")) {
branch.setTelBr(jsonElement.getAsJsonObject().get("brTel").getAsString());
}
if(!JsonHelper.isNull(jsonElement ,"brAddress")) {
branch.setAddressBr(jsonElement.getAsJsonObject().get("brAddress").getAsString());
}
return branch;
}
示例10: execute
import com.google.gson.JsonElement; //导入依赖的package包/类
public void execute() throws IOException, URISyntaxException, AuthenticationException {
String pa = restRequest.getParameterValue("project-area");
Map<String, JsonElement> typeMap = new TreeMap<String, JsonElement>();
try {
IProjectAreaHandle targetArea = ProjectAreaHelpers.getProjectArea(pa, parentService);
if(targetArea == null) {
response.setStatus(400);
return;
}
IWorkItemServer serverService = parentService.getService(IWorkItemServer.class);
List<IWorkItemType> types = WorkItemTypeHelpers.getWorkItemTypes(targetArea, serverService, new NullProgressMonitor());
for(IWorkItemType type : types) {
JsonObject typeObject = new JsonObject();
typeObject.addProperty("id", type.getIdentifier());
typeObject.addProperty("name", type.getDisplayName());
typeMap.put(type.getDisplayName(), typeObject);
}
} catch (TeamRepositoryException e) {
response.setStatus(500);
}
response.getWriter().write(new Gson().toJson(typeMap.values()));
}
示例11: mapToJson
import com.google.gson.JsonElement; //导入依赖的package包/类
/**
* Method of converting hashmap to JSON
*
* @param wordweights a map from related metadata to weights
* @param num the number of converted elements
* @return converted JSON object
*/
protected JsonElement mapToJson(Map<String, Double> wordweights, int num) {
Gson gson = new Gson();
List<JsonObject> nodes = new ArrayList<>();
Set<String> words = wordweights.keySet();
int i = 0;
for (String wordB : words) {
JsonObject node = new JsonObject();
node.addProperty("name", wordB);
node.addProperty("weight", wordweights.get(wordB));
nodes.add(node);
i += 1;
if (i >= num) {
break;
}
}
String nodesJson = gson.toJson(nodes);
JsonElement nodesElement = gson.fromJson(nodesJson, JsonElement.class);
return nodesElement;
}
示例12: findType
import com.google.gson.JsonElement; //导入依赖的package包/类
private String findType(JsonElement value) throws ValidationException
{
if (!value.isJsonPrimitive()){
throw new ValidationException("value is an invalid type");
}
JsonPrimitive primitiveValue = (JsonPrimitive) value;
if (primitiveValue.isNumber() || (primitiveValue.isString() && Util.isNumber(value.getAsString())))
{
String v = value.getAsString();
if (!v.contains("."))
{
return "long";
}
else
{
return "double";
}
}
else
return "string";
}
示例13: deserialize
import com.google.gson.JsonElement; //导入依赖的package包/类
@Override
public VKApiComment deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
JsonObject root = json.getAsJsonObject();
VKApiComment dto = new VKApiComment();
dto.id = optInt(root, "id");
dto.from_id = optInt(root, "from_id");
if(dto.from_id == 0){
dto.from_id = optInt(root, "owner_id");
}
dto.date = optLong(root, "date");
dto.text = optString(root, "text");
dto.reply_to_user = optInt(root, "reply_to_user");
dto.reply_to_comment = optInt(root, "reply_to_comment");
if(root.has("attachments")){
dto.attachments = context.deserialize(root.get("attachments"), VkApiAttachments.class);
}
if(root.has("likes")){
JsonObject likesRoot = root.getAsJsonObject("likes");
dto.likes = optInt(likesRoot, "count");
dto.user_likes = optIntAsBoolean(likesRoot, "user_likes");
dto.can_like = optIntAsBoolean(likesRoot, "can_like");
}
dto.can_edit = optIntAsBoolean(root, "can_edit");
return dto;
}
示例14: deserialize
import com.google.gson.JsonElement; //导入依赖的package包/类
@Override
public PointDto deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) {
PointDto point = new PointDto();
JsonObject asJsonObject = json.getAsJsonObject();
JsonElement jsonElement = asJsonObject.get("coordinates");
PositionDto positionDto = context.deserialize(jsonElement, PositionDto.class);
point.setPosition(positionDto);
point.setBbox(BoundingBoxParser.parseBbox(asJsonObject, context));
return point;
}
示例15: fromJson
import com.google.gson.JsonElement; //导入依赖的package包/类
@Override
public DataManager fromJson(final JsonElement json)
{
DataManager dt = new DataManager();
dt.fromJson(json);
return dt;
}