Example usage for com.google.gson JsonElement getAsJsonPrimitive

List of usage examples for com.google.gson JsonElement getAsJsonPrimitive

Introduction

In this page you can find the example usage for com.google.gson JsonElement getAsJsonPrimitive.

Prototype

public JsonPrimitive getAsJsonPrimitive() 

Source Link

Document

convenience method to get this element as a JsonPrimitive .

Usage

From source file:org.tsukuba_bunko.lilac.web.controller.JsonRequestParser.java

License:Open Source License

/**
 * JSON?Java?????/*ww  w .  jav a2 s.  c  o m*/
 * @param elementJSON?
 * @return Java
 */
public Object getJavaObject(JsonElement element) {
    if (element.isJsonObject()) {
        return getJavaObject(element.getAsJsonObject());
    } else if (element.isJsonArray()) {
        return getJavaObject(element.getAsJsonArray());
    } else if (element.isJsonPrimitive()) {
        return getJavaObject(element.getAsJsonPrimitive());
    } else {
        return null;
    }
}

From source file:org.tuleap.mylyn.task.core.internal.parser.AbstractTuleapDeserializer.java

License:Open Source License

/**
 * {@inheritDoc}/*  w  w  w  .j a v a  2  s .c o m*/
 *
 * @see com.google.gson.JsonDeserializer#deserialize(com.google.gson.JsonElement, java.lang.reflect.Type,
 *      com.google.gson.JsonDeserializationContext)
 */
@Override
public T deserialize(JsonElement rootJsonElement, Type type, JsonDeserializationContext context)
        throws JsonParseException {
    JsonObject jsonObject = rootJsonElement.getAsJsonObject();
    T pojo = super.deserialize(rootJsonElement, type, context);
    JsonElement jsonTrackerRef = jsonObject.get(ITuleapConstants.JSON_TRACKER);
    TuleapReference trackerRef = context.deserialize(jsonTrackerRef, TuleapReference.class);
    pojo.setTracker(trackerRef);

    JsonArray fields = jsonObject.get(ITuleapConstants.VALUES).getAsJsonArray();
    for (JsonElement field : fields) {
        JsonObject jsonField = field.getAsJsonObject();
        int fieldId = jsonField.get(ITuleapConstants.FIELD_ID).getAsInt();
        if (jsonField.has(ITuleapConstants.FIELD_VALUE)) {
            JsonElement jsonValue = jsonField.get(ITuleapConstants.FIELD_VALUE);
            if (jsonValue.isJsonPrimitive()) {
                JsonPrimitive primitive = jsonValue.getAsJsonPrimitive();
                if (primitive.isString()) {
                    pojo.addFieldValue(new LiteralFieldValue(fieldId, primitive.getAsString()));
                } else if (primitive.isNumber()) {
                    pojo.addFieldValue(new LiteralFieldValue(fieldId, String.valueOf(primitive.getAsNumber())));
                } else if (primitive.isBoolean()) {
                    pojo.addFieldValue(
                            new LiteralFieldValue(fieldId, String.valueOf(primitive.getAsBoolean())));
                }
            }
        } else if (jsonField.has(ITuleapConstants.FIELD_BIND_VALUE_ID)
                && !jsonField.get(ITuleapConstants.FIELD_BIND_VALUE_ID).isJsonNull()) {
            // sb?
            JsonElement jsonBindValueId = jsonField.get(ITuleapConstants.FIELD_BIND_VALUE_ID);
            int bindValueId = jsonBindValueId.getAsInt();
            pojo.addFieldValue(new BoundFieldValue(fieldId, Lists.newArrayList(Integer.valueOf(bindValueId))));
        } else if (jsonField.has(ITuleapConstants.FIELD_BIND_VALUE_IDS)
                && !jsonField.get(ITuleapConstants.FIELD_BIND_VALUE_IDS).isJsonNull()) {
            // sb?, msb, cb, or tbl (open list)
            JsonElement jsonBindValueIds = jsonField.get(ITuleapConstants.FIELD_BIND_VALUE_IDS);
            JsonArray jsonIds = jsonBindValueIds.getAsJsonArray();
            if (jsonIds.size() > 0) {
                JsonPrimitive firstElement = jsonIds.get(0).getAsJsonPrimitive();
                if (firstElement.isString()) {
                    // Open list (tbl)
                    List<String> bindValueIds = new ArrayList<String>();
                    for (JsonElement idElement : jsonIds) {
                        bindValueIds.add(idElement.getAsString());
                    }
                    pojo.addFieldValue(new OpenListFieldValue(fieldId, bindValueIds));
                } else {
                    List<Integer> bindValueIds = new ArrayList<Integer>();
                    for (JsonElement idElement : jsonIds) {
                        bindValueIds.add(Integer.valueOf(idElement.getAsInt()));
                    }
                    pojo.addFieldValue(new BoundFieldValue(fieldId, bindValueIds));
                }
            } else {
                pojo.addFieldValue(new BoundFieldValue(fieldId, Collections.<Integer>emptyList()));
            }
        } else if (jsonField.has(ITuleapConstants.FIELD_LINKS)
                && !jsonField.get(ITuleapConstants.FIELD_LINKS).isJsonNull()) {
            // Artifact links
            pojo.addFieldValue(
                    context.<ArtifactLinkFieldValue>deserialize(jsonField, ArtifactLinkFieldValue.class));
        } else if (jsonField.has(ITuleapConstants.FILE_DESCRIPTIONS)
                && jsonField.get(ITuleapConstants.FILE_DESCRIPTIONS).isJsonArray()) {
            AttachmentFieldValue value = context.deserialize(jsonField, AttachmentFieldValue.class);
            pojo.addFieldValue(value);
        }

    }

    return pojo;
}

From source file:org.unitime.timetable.onlinesectioning.custom.purdue.DegreeWorksPlanScraper.java

License:Apache License

protected Gson getGson() {
    return new GsonBuilder().registerTypeAdapter(DateTime.class, new JsonSerializer<DateTime>() {
        @Override//from   ww w  . j av  a  2  s . c  o  m
        public JsonElement serialize(DateTime src, Type typeOfSrc, JsonSerializationContext context) {
            return new JsonPrimitive(src.toString("yyyy-MM-dd'T'HH:mm:ss'Z'"));
        }
    }).registerTypeAdapter(DateTime.class, new JsonDeserializer<DateTime>() {
        @Override
        public DateTime deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
                throws JsonParseException {
            return new DateTime(json.getAsJsonPrimitive().getAsString(), DateTimeZone.UTC);
        }
    }).registerTypeAdapter(Date.class, new JsonSerializer<Date>() {
        @Override
        public JsonElement serialize(Date src, Type typeOfSrc, JsonSerializationContext context) {
            return new JsonPrimitive(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(src));
        }
    }).registerTypeAdapter(Date.class, new JsonDeserializer<Date>() {
        @Override
        public Date deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
                throws JsonParseException {
            try {
                return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
                        .parse(json.getAsJsonPrimitive().getAsString());
            } catch (ParseException e) {
                throw new JsonParseException(e.getMessage(), e);
            }
        }
    }).setPrettyPrinting().create();
}

From source file:org.unitime.timetable.onlinesectioning.custom.purdue.GsonRepresentation.java

License:Apache License

public GsonBuilder getBuilder() {
    if (iBuilder == null) {
        iBuilder = new GsonBuilder().registerTypeAdapter(DateTime.class, new JsonSerializer<DateTime>() {
            @Override/*from   w w  w . j av  a2s  .co  m*/
            public JsonElement serialize(DateTime src, Type typeOfSrc, JsonSerializationContext context) {
                return new JsonPrimitive(src.toString("yyyy-MM-dd'T'HH:mm:ss'Z'"));
            }
        }).registerTypeAdapter(DateTime.class, new JsonDeserializer<DateTime>() {
            @Override
            public DateTime deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
                    throws JsonParseException {
                return new DateTime(json.getAsJsonPrimitive().getAsString(), DateTimeZone.UTC);
            }
        }).registerTypeAdapter(Date.class, new JsonSerializer<Date>() {
            @Override
            public JsonElement serialize(Date src, Type typeOfSrc, JsonSerializationContext context) {
                return new JsonPrimitive(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(src));
            }
        }).registerTypeAdapter(Date.class, new JsonDeserializer<Date>() {
            @Override
            public Date deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
                    throws JsonParseException {
                try {
                    return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
                            .parse(json.getAsJsonPrimitive().getAsString());
                } catch (ParseException e) {
                    throw new JsonParseException(e.getMessage(), e);
                }
            }
        });
    }
    return iBuilder;
}

From source file:org.unitime.timetable.onlinesectioning.custom.purdue.PurdueBatchSolverValidator.java

License:Apache License

protected Gson getGson() {
    GsonBuilder builder = new GsonBuilder().registerTypeAdapter(DateTime.class, new JsonSerializer<DateTime>() {
        @Override// www  . j  a  va 2 s  .  c  o  m
        public JsonElement serialize(DateTime src, Type typeOfSrc, JsonSerializationContext context) {
            return new JsonPrimitive(src.toString("yyyy-MM-dd'T'HH:mm:ss'Z'"));
        }
    }).registerTypeAdapter(DateTime.class, new JsonDeserializer<DateTime>() {
        @Override
        public DateTime deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
                throws JsonParseException {
            return new DateTime(json.getAsJsonPrimitive().getAsString(), DateTimeZone.UTC);
        }
    });
    return builder.create();
}

From source file:org.unitime.timetable.onlinesectioning.custom.purdue.PurdueCourseRequestsValidationProvider.java

License:Apache License

protected Gson getGson(OnlineSectioningHelper helper) {
    GsonBuilder builder = new GsonBuilder().registerTypeAdapter(DateTime.class, new JsonSerializer<DateTime>() {
        @Override/* w w w  .  j ava2  s. c  om*/
        public JsonElement serialize(DateTime src, Type typeOfSrc, JsonSerializationContext context) {
            return new JsonPrimitive(src.toString("yyyy-MM-dd'T'HH:mm:ss'Z'"));
        }
    }).registerTypeAdapter(DateTime.class, new JsonDeserializer<DateTime>() {
        @Override
        public DateTime deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
                throws JsonParseException {
            return new DateTime(json.getAsJsonPrimitive().getAsString(), DateTimeZone.UTC);
        }
    }).registerTypeAdapter(Date.class, new JsonSerializer<Date>() {
        @Override
        public JsonElement serialize(Date src, Type typeOfSrc, JsonSerializationContext context) {
            return new JsonPrimitive(new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'").format(src));
        }
    }).registerTypeAdapter(Date.class, new JsonDeserializer<Date>() {
        @Override
        public Date deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
                throws JsonParseException {
            try {
                return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
                        .parse(json.getAsJsonPrimitive().getAsString());
            } catch (ParseException e) {
                throw new JsonParseException(e.getMessage(), e);
            }
        }
    });
    if (helper.isDebugEnabled())
        builder.setPrettyPrinting();
    return builder.create();
}

From source file:org.unitime.timetable.onlinesectioning.custom.purdue.XEStudentEnrollment.java

License:Open Source License

protected Gson getGson(OnlineSectioningHelper helper) {
    GsonBuilder builder = new GsonBuilder().registerTypeAdapter(DateTime.class, new JsonSerializer<DateTime>() {
        @Override//from  w w  w.java  2  s. com
        public JsonElement serialize(DateTime src, Type typeOfSrc, JsonSerializationContext context) {
            return new JsonPrimitive(src.toString("yyyy-MM-dd'T'HH:mm:ss'Z'"));
        }
    }).registerTypeAdapter(DateTime.class, new JsonDeserializer<DateTime>() {
        @Override
        public DateTime deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
                throws JsonParseException {
            return new DateTime(json.getAsJsonPrimitive().getAsString(), DateTimeZone.UTC);
        }
    });
    if (helper.isDebugEnabled())
        builder.setPrettyPrinting();
    return builder.create();
}

From source file:org.unitime.timetable.server.sectioning.PublishedSectioningSolutionsBackend.java

License:Apache License

protected Gson getGson() {
    GsonBuilder builder = new GsonBuilder().registerTypeAdapter(DateTime.class, new JsonSerializer<DateTime>() {
        @Override/*from w  w w  .j av  a 2  s.c  o  m*/
        public JsonElement serialize(DateTime src, Type typeOfSrc, JsonSerializationContext context) {
            return new JsonPrimitive(src.toString("yyyy-MM-dd'T'HH:mm:ss'Z'"));
        }
    }).registerTypeAdapter(DateTime.class, new JsonDeserializer<DateTime>() {
        @Override
        public DateTime deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
                throws JsonParseException {
            return new DateTime(json.getAsJsonPrimitive().getAsString(), DateTimeZone.UTC);
        }
    }).registerTypeAdapter(Date.class, new JsonSerializer<Date>() {
        @Override
        public JsonElement serialize(Date src, Type typeOfSrc, JsonSerializationContext context) {
            return new JsonPrimitive(new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'").format(src));
        }
    }).registerTypeAdapter(Date.class, new JsonDeserializer<Date>() {
        @Override
        public Date deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
                throws JsonParseException {
            try {
                return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
                        .parse(json.getAsJsonPrimitive().getAsString());
            } catch (ParseException e) {
                throw new JsonParseException(e.getMessage(), e);
            }
        }
    });
    builder.setPrettyPrinting();
    return builder.create();
}

From source file:org.waveprotocol.wave.communication.gson.GsonUtil.java

License:Apache License

/**
 * Unpack a JsonElement into the object type
 *
 * @param <T> The type to deserialize
 * @param object The object used to accept the pare result
 * @param valueObj The root of a tree of JsonElements or an indirection index
 * @param gson A Gson context//from   www.  j a  v a  2 s.c om
 * @param raw
 * @throws GsonException
 */
public static <T extends GsonSerializable> void extractJsonObject(T object, JsonElement valueObj, Gson gson,
        RawStringData raw) throws GsonException {
    if (valueObj.isJsonObject()) {
        object.fromGson(valueObj.getAsJsonObject(), gson, raw);
    } else if (valueObj.isJsonPrimitive()) {
        JsonPrimitive primitive = valueObj.getAsJsonPrimitive();
        String s = null;
        if (raw == null || !primitive.isString()) {
            throw new GsonException("Decoding " + valueObj + " as object " + object.getClass()
                    + " with no RawStringData given");
        }
        s = raw.getString(valueObj.getAsString());
        GsonUtil.parseJson(object, gson, s, raw);
    } else {
        throw new GsonException(
                "Cannot decode valueObject " + valueObj.getClass() + " as object " + object.getClass());
    }
}

From source file:org.wso2.ballerina.nativeimpl.lang.json.GetBoolean.java

License:Open Source License

@Override
public BValue[] execute(Context ctx) {
    String jsonPath = null;//from  w  w  w.j  a  va  2  s  .  c  o  m
    BValue result = null;
    try {
        // Accessing Parameters.
        BJSON json = (BJSON) getArgument(ctx, 0);
        jsonPath = getArgument(ctx, 1).stringValue();

        // Getting the value from JSON
        ReadContext jsonCtx = JsonPath.parse(json.value());
        JsonElement element = jsonCtx.read(jsonPath);
        if (element == null) {
            throw new BallerinaException("No matching element found for jsonpath: " + jsonPath);
        } else if (element.isJsonPrimitive()) {
            // if the resulting value is a primitive, return the respective primitive value object
            JsonPrimitive value = element.getAsJsonPrimitive();
            if (value.isBoolean()) {
                result = new BBoolean(value.getAsBoolean());
            } else {
                throw new BallerinaException("The element matching path: " + jsonPath + " is not a Boolean.");
            }
        } else {
            throw new BallerinaException(
                    "The element matching path: " + jsonPath + " is a JSON, not a Boolean.");
        }
    } catch (PathNotFoundException e) {
        ErrorHandler.handleNonExistingJsonpPath(OPERATION, jsonPath, e);
    } catch (InvalidPathException e) {
        ErrorHandler.handleInvalidJsonPath(OPERATION, e);
    } catch (JsonPathException e) {
        ErrorHandler.handleJsonPathException(OPERATION, e);
    } catch (Throwable e) {
        ErrorHandler.handleJsonPathException(OPERATION, e);
    }

    // Setting output value.
    return getBValues(result);
}