Search in sources :

Example 71 with JsonParseException

use of com.google.gson.JsonParseException in project Bookshelf by Darkhax-Minecraft.

the class SerializerBlockState method read.

@Override
public BlockState read(JsonElement json) {
    if (json.isJsonObject()) {
        final JsonObject obj = json.getAsJsonObject();
        final Block block = Serializers.BLOCK.read(obj, "block");
        BlockState state = block.defaultBlockState();
        if (obj.has("properties")) {
            final JsonElement properties = obj.get("properties");
            for (final Entry<String, JsonElement> property : properties.getAsJsonObject().entrySet()) {
                state = this.readProperty(state, property.getKey(), property.getValue());
            }
        }
        return state;
    } else {
        throw new JsonParseException("Expected properties to be an object. Recieved " + JSONUtils.getType(json));
    }
}
Also used : BlockState(net.minecraft.block.BlockState) JsonElement(com.google.gson.JsonElement) JsonObject(com.google.gson.JsonObject) Block(net.minecraft.block.Block) JsonParseException(com.google.gson.JsonParseException)

Example 72 with JsonParseException

use of com.google.gson.JsonParseException in project intellij-plugins by StepicOrg.

the class ReplyDeserializer method deserialize.

@Override
public Reply deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
    if (json == null || !(json instanceof JsonObject)) {
        return null;
    }
    Reply reply = new Reply();
    JsonObject object = json.getAsJsonObject();
    String language = getString(object, "language");
    reply.setLanguage(language);
    String code = getString(object, "code");
    reply.setCode(code);
    String formula = getString(object, "formula");
    reply.setFormula(formula);
    String text = getString(object, "text");
    reply.setText(text);
    String number = getString(object, "number");
    reply.setNumber(number);
    JsonArray ordering = getJsonArray(object, "ordering");
    if (ordering != null) {
        List<Integer> intOrdering = new ArrayList<>();
        ordering.forEach(item -> intOrdering.add(item.getAsInt()));
        reply.setOrdering(intOrdering);
    }
    reply.setAttachments(getList(object, "attachments", (jsonElement) -> context.deserialize(jsonElement, Attachment.class)));
    reply.setFiles(getStringList(object, "files"));
    JsonArray choices = getJsonArray(object, "choices");
    if (choices != null && choices.size() > 0) {
        if (choices.get(0).isJsonPrimitive()) {
            List<Boolean> list = new ArrayList<>();
            choices.forEach(item -> list.add(item.getAsBoolean()));
            reply.setChoices(list);
        } else {
            List<Choice> list = new ArrayList<>();
            choices.forEach(item -> list.add(context.deserialize(item, Choice.class)));
            reply.setChoices(list);
        }
    }
    reply.setBlanks(getStringList(object, "blanks"));
    return reply;
}
Also used : Utils.getJsonArray(org.stepik.api.Utils.getJsonArray) JsonArray(com.google.gson.JsonArray) JsonParseException(com.google.gson.JsonParseException) JsonObject(com.google.gson.JsonObject) Utils.getList(org.stepik.api.Utils.getList) JsonDeserializationContext(com.google.gson.JsonDeserializationContext) ArrayList(java.util.ArrayList) Utils.getJsonArray(org.stepik.api.Utils.getJsonArray) JsonElement(com.google.gson.JsonElement) JsonArray(com.google.gson.JsonArray) List(java.util.List) Type(java.lang.reflect.Type) Utils.getString(org.stepik.api.Utils.getString) Utils.getStringList(org.stepik.api.Utils.getStringList) JsonDeserializer(com.google.gson.JsonDeserializer) ArrayList(java.util.ArrayList) JsonObject(com.google.gson.JsonObject) Utils.getString(org.stepik.api.Utils.getString)

Example 73 with JsonParseException

use of com.google.gson.JsonParseException in project ninja by ninjaframework.

the class ApiControllerDocTesterTest method getGsonWithLongToDateParsing.

private Gson getGsonWithLongToDateParsing() {
    // Creates the json object which will manage the information received
    GsonBuilder builder = new GsonBuilder();
    // Register an adapter to manage the date types as long values
    builder.registerTypeAdapter(Date.class, new JsonDeserializer<Date>() {

        @Override
        public Date deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
            return new Date(json.getAsJsonPrimitive().getAsLong());
        }
    });
    Gson gson = builder.create();
    return gson;
}
Also used : Type(java.lang.reflect.Type) JsonDeserializationContext(com.google.gson.JsonDeserializationContext) GsonBuilder(com.google.gson.GsonBuilder) JsonElement(com.google.gson.JsonElement) Gson(com.google.gson.Gson) JsonParseException(com.google.gson.JsonParseException) Date(java.util.Date)

Example 74 with JsonParseException

use of com.google.gson.JsonParseException in project ninja by ninjaframework.

the class ApiControllerTest method getGsonWithLongToDateParsing.

private Gson getGsonWithLongToDateParsing() {
    // Creates the json object which will manage the information received
    GsonBuilder builder = new GsonBuilder();
    // Register an adapter to manage the date types as long values
    builder.registerTypeAdapter(Date.class, new JsonDeserializer<Date>() {

        @Override
        public Date deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
            return new Date(json.getAsJsonPrimitive().getAsLong());
        }
    });
    Gson gson = builder.create();
    return gson;
}
Also used : Type(java.lang.reflect.Type) JsonDeserializationContext(com.google.gson.JsonDeserializationContext) GsonBuilder(com.google.gson.GsonBuilder) JsonElement(com.google.gson.JsonElement) Gson(com.google.gson.Gson) JsonParseException(com.google.gson.JsonParseException) Date(java.util.Date)

Example 75 with JsonParseException

use of com.google.gson.JsonParseException in project ninja by ninjaframework.

the class ApiControllerDocTest method getGsonWithLongToDateParsing.

private Gson getGsonWithLongToDateParsing() {
    // Creates the json object which will manage the information received
    GsonBuilder builder = new GsonBuilder();
    // Register an adapter to manage the date types as long values
    builder.registerTypeAdapter(Date.class, new JsonDeserializer<Date>() {

        @Override
        public Date deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
            return new Date(json.getAsJsonPrimitive().getAsLong());
        }
    });
    Gson gson = builder.create();
    return gson;
}
Also used : Type(java.lang.reflect.Type) JsonDeserializationContext(com.google.gson.JsonDeserializationContext) GsonBuilder(com.google.gson.GsonBuilder) JsonElement(com.google.gson.JsonElement) Gson(com.google.gson.Gson) JsonParseException(com.google.gson.JsonParseException) Date(java.util.Date)

Aggregations

JsonParseException (com.google.gson.JsonParseException)267 JsonObject (com.google.gson.JsonObject)105 IOException (java.io.IOException)78 JsonElement (com.google.gson.JsonElement)71 Gson (com.google.gson.Gson)39 JsonArray (com.google.gson.JsonArray)30 JsonReader (com.google.gson.stream.JsonReader)28 InputStreamReader (java.io.InputStreamReader)28 JsonParser (com.google.gson.JsonParser)25 JsonPrimitive (com.google.gson.JsonPrimitive)25 Map (java.util.Map)25 InputStream (java.io.InputStream)23 GsonBuilder (com.google.gson.GsonBuilder)20 ArrayList (java.util.ArrayList)20 Type (java.lang.reflect.Type)17 JsonWriter (com.google.gson.stream.JsonWriter)15 StringReader (java.io.StringReader)13 HashMap (java.util.HashMap)13 LinkedHashMap (java.util.LinkedHashMap)13 HttpUrl (okhttp3.HttpUrl)13