Search in sources :

Example 21 with JsonParseException

use of com.google.gson.JsonParseException in project jphp by jphp-compiler.

the class JsonFunctions method json_decode.

public static Memory json_decode(Environment env, String json, boolean assoc, int depth) {
    MemoryDeserializer memoryDeserializer = new MemoryDeserializer();
    memoryDeserializer.setEnv(env);
    GsonBuilder gsonBuilder = JsonExtension.createGsonBuilderForDecode(memoryDeserializer);
    memoryDeserializer.setAssoc(assoc);
    memoryDeserializer.setMaxDepth(depth);
    Gson gson = gsonBuilder.create();
    try {
        env.setUserValue(JsonFunctions.class.getName() + "#error", null);
        Memory r = gson.fromJson(json, Memory.class);
        if (r == null)
            return Memory.NULL;
        else
            return assoc ? r.toImmutable() : r;
    } catch (MemoryDeserializer.MaxDepthException e) {
        env.setUserValue(JsonFunctions.class.getName() + "#error", JsonConstants.JSON_ERROR_DEPTH);
    } catch (JsonSyntaxException e) {
        env.setUserValue(JsonFunctions.class.getName() + "#error", JsonConstants.JSON_ERROR_SYNTAX);
    } catch (JsonParseException e) {
        env.setUserValue(JsonFunctions.class.getName() + "#error", JsonConstants.JSON_ERROR_STATE_MISMATCH);
    }
    return Memory.NULL;
}
Also used : JsonSyntaxException(com.google.gson.JsonSyntaxException) GsonBuilder(com.google.gson.GsonBuilder) Memory(php.runtime.Memory) StringMemory(php.runtime.memory.StringMemory) Gson(com.google.gson.Gson) JsonParseException(com.google.gson.JsonParseException) MemoryDeserializer(org.develnext.jphp.json.gson.MemoryDeserializer)

Example 22 with JsonParseException

use of com.google.gson.JsonParseException in project cdap by caskdata.

the class IdTypeAdapter method deserialize.

@Override
public Id deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
    JsonObject map = json.getAsJsonObject();
    JsonElement elementTypeJson = map.get("elementType");
    if (elementTypeJson == null) {
        throw new JsonParseException("Expected elementType in Id JSON");
    }
    String elementTypeString = elementTypeJson.getAsString();
    EntityType type = EntityType.valueOf(elementTypeString);
    if (type == null) {
        throw new JsonParseException("Invalid elementType: " + elementTypeString);
    }
    return context.deserialize(json, type.getIdClass());
}
Also used : EntityType(co.cask.cdap.proto.element.EntityType) JsonElement(com.google.gson.JsonElement) JsonObject(com.google.gson.JsonObject) JsonParseException(com.google.gson.JsonParseException)

Example 23 with JsonParseException

use of com.google.gson.JsonParseException in project Mvp-Rxjava-Retrofit-dagger2 by pengMaster.

the class ErrorListenerImpl method handleResponseError.

@Override
public void handleResponseError(Context context, Throwable t) {
    Timber.tag("Catch-Error").w(t.getMessage());
    // 这里不光是只能打印错误,还可以根据不同的错误作出不同的逻辑处理
    String msg = "未知错误";
    if (t instanceof UnknownHostException) {
        msg = "网络不可用";
    } else if (t instanceof SocketTimeoutException) {
        msg = "请求网络超时";
    } else if (t instanceof HttpException) {
        HttpException httpException = (HttpException) t;
        msg = convertStatusCode(httpException);
    } else if (t instanceof JsonParseException || t instanceof ParseException || t instanceof JSONException) {
        msg = "数据解析错误";
    }
    // ArmsUtils.snackbarText(msg);
    ToastUtils.showShort(msg);
}
Also used : SocketTimeoutException(java.net.SocketTimeoutException) UnknownHostException(java.net.UnknownHostException) JSONException(org.json.JSONException) HttpException(retrofit2.HttpException) JsonParseException(com.google.gson.JsonParseException) ParseException(android.net.ParseException) JsonParseException(com.google.gson.JsonParseException)

Example 24 with JsonParseException

use of com.google.gson.JsonParseException in project bayou by capergroup.

the class RuntimeTypeAdapterFactory method create.

public <R> TypeAdapter<R> create(Gson gson, TypeToken<R> type) {
    if (type.getRawType() != baseType) {
        return null;
    }
    final Map<String, TypeAdapter<?>> labelToDelegate = new LinkedHashMap<String, TypeAdapter<?>>();
    final Map<Class<?>, TypeAdapter<?>> subtypeToDelegate = new LinkedHashMap<Class<?>, TypeAdapter<?>>();
    for (Map.Entry<String, Class<?>> entry : labelToSubtype.entrySet()) {
        TypeAdapter<?> delegate = gson.getDelegateAdapter(this, TypeToken.get(entry.getValue()));
        labelToDelegate.put(entry.getKey(), delegate);
        subtypeToDelegate.put(entry.getValue(), delegate);
    }
    return new TypeAdapter<R>() {

        @Override
        public R read(JsonReader in) throws IOException {
            JsonElement jsonElement = Streams.parse(in);
            JsonElement labelJsonElement = jsonElement.getAsJsonObject().remove(typeFieldName);
            if (labelJsonElement == null) {
                throw new JsonParseException("cannot deserialize " + baseType + " because it does not define a field named " + typeFieldName);
            }
            String label = labelJsonElement.getAsString();
            // registration requires that subtype extends T
            @SuppressWarnings("unchecked") TypeAdapter<R> delegate = (TypeAdapter<R>) labelToDelegate.get(label);
            if (delegate == null) {
                throw new JsonParseException("cannot deserialize " + baseType + " subtype named " + label + "; did you forget to register a subtype?");
            }
            return delegate.fromJsonTree(jsonElement);
        }

        @Override
        public void write(JsonWriter out, R value) throws IOException {
            Class<?> srcType = value.getClass();
            String label = subtypeToLabel.get(srcType);
            // registration requires that subtype extends T
            @SuppressWarnings("unchecked") TypeAdapter<R> delegate = (TypeAdapter<R>) subtypeToDelegate.get(srcType);
            if (delegate == null) {
                throw new JsonParseException("cannot serialize " + srcType.getName() + "; did you forget to register a subtype?");
            }
            JsonObject jsonObject = delegate.toJsonTree(value).getAsJsonObject();
            if (jsonObject.has(typeFieldName)) {
                throw new JsonParseException("cannot serialize " + srcType.getName() + " because it already defines a field named " + typeFieldName);
            }
            JsonObject clone = new JsonObject();
            clone.add(typeFieldName, new JsonPrimitive(label));
            for (Map.Entry<String, JsonElement> e : jsonObject.entrySet()) {
                clone.add(e.getKey(), e.getValue());
            }
            Streams.write(clone, out);
        }
    }.nullSafe();
}
Also used : JsonPrimitive(com.google.gson.JsonPrimitive) JsonObject(com.google.gson.JsonObject) IOException(java.io.IOException) JsonParseException(com.google.gson.JsonParseException) JsonWriter(com.google.gson.stream.JsonWriter) LinkedHashMap(java.util.LinkedHashMap) JsonElement(com.google.gson.JsonElement) TypeAdapter(com.google.gson.TypeAdapter) JsonReader(com.google.gson.stream.JsonReader) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 25 with JsonParseException

use of com.google.gson.JsonParseException in project Gaspunk by Ladysnake.

the class GasDeserializer method parseAgents.

private void parseAgents(JsonReader in, Gas.Builder builder) throws IOException {
    in.beginArray();
    while (in.hasNext()) {
        IGasAgent agent = null;
        double potency = 0;
        in.beginObject();
        while (in.hasNext()) {
            String name = in.nextName();
            switch(name) {
                case "agent":
                    String id = in.nextString();
                    agent = GasAgents.getAgent(new ResourceLocation(id));
                    if (agent == null)
                        throw new JsonParseException("Invalid agent provided: " + id + " " + in.getPath());
                    break;
                case "potency":
                    potency = in.nextDouble();
                    break;
            }
        }
        in.endObject();
        builder.addAgent(Objects.requireNonNull(agent, "no agent type provided"), (float) potency);
    }
    in.endArray();
}
Also used : IGasAgent(ladysnake.gaspunk.api.IGasAgent) ResourceLocation(net.minecraft.util.ResourceLocation) JsonParseException(com.google.gson.JsonParseException)

Aggregations

JsonParseException (com.google.gson.JsonParseException)210 JsonObject (com.google.gson.JsonObject)81 JsonElement (com.google.gson.JsonElement)55 IOException (java.io.IOException)54 Gson (com.google.gson.Gson)34 InputStreamReader (java.io.InputStreamReader)24 JsonArray (com.google.gson.JsonArray)21 InputStream (java.io.InputStream)20 GsonBuilder (com.google.gson.GsonBuilder)18 JsonParser (com.google.gson.JsonParser)18 JsonPrimitive (com.google.gson.JsonPrimitive)18 Type (java.lang.reflect.Type)17 Map (java.util.Map)17 JsonReader (com.google.gson.stream.JsonReader)16 ArrayList (java.util.ArrayList)14 HttpUrl (okhttp3.HttpUrl)13 Request (okhttp3.Request)13 Response (okhttp3.Response)13 JsonSyntaxException (com.google.gson.JsonSyntaxException)10 SocketTimeoutException (java.net.SocketTimeoutException)8