Package com.google.gson

Examples of com.google.gson.JsonParseException


      assert json != null;
      assert context != null;
      assert typeOfT != null;

      if( !json.isJsonPrimitive() ) {
        throw new JsonParseException( "The value for a Date must be a valid number");
      }
     
      assert json instanceof JsonPrimitive; // To please Findugs!
      JsonPrimitive primitivejson = (JsonPrimitive)json;
      if( !primitivejson.isNumber()) {
        throw new JsonParseException( "The value for a Date must be a valid number");
      }
      return new Date( primitivejson.getAsLong() );
    }
View Full Code Here


  @Override
  public ResourceAssignment deserialize(JsonElement json, Type typeOfT,
                                        JsonDeserializationContext context) throws JsonParseException {
    if (!json.isJsonObject()) {
      throw new JsonParseException("Expect a json object, got " + json);
    }

    JsonObject jsonObj = json.getAsJsonObject();
    String name = jsonObj.get("name").getAsString();

    Multimap<Discoverable, PartitionReplica> assignments = TreeMultimap.create(DiscoverableComparator.COMPARATOR,
                                                                               PartitionReplica.COMPARATOR);
    JsonArray assignmentsJson = context.deserialize(jsonObj.get("assignments"), JsonArray.class);
    for (JsonElement element : assignmentsJson) {
      if (!element.isJsonArray()) {
        throw new JsonParseException("Expect a json array, got " + element);
      }

      JsonArray entryJson = element.getAsJsonArray();
      if (entryJson.size() != 2) {
        throw new JsonParseException("Expect json array of size = 2, got " + entryJson.size());
      }
      Discoverable key = context.deserialize(entryJson.get(0), Discoverable.class);
      PartitionReplica value = context.deserialize(entryJson.get(1), PartitionReplica.class);
      assignments.put(key, value);
    }
View Full Code Here

        Map<String,Primitive> prelimMap;
        prelimMap= (Map<String,Primitive>) gsonMap.parse(s.getString());
        PrimitiveOrMap sm = new PrimitiveOrMap(new HashMap<String,PrimitiveOrMap>());
       
        if(prelimMap == null)
          throw new JsonParseException("Null prelimMap");
        for(String key:prelimMap.keySet()){
          Primitive val = prelimMap.get(key);
          PrimitiveOrMap valSM;
          valSM = process(val);
          sm.getSecond().put(key, valSM);
View Full Code Here

 
    @Override
    public Trie<String, HashedFileObject> deserialize(final JsonElement json,
      final Type typeOfT, final JsonDeserializationContext context) {
      if(!(json instanceof JsonObject)) {
        throw new JsonParseException("Dood! The Trie to " +
            "deserialize should be an object!");
      }
      final Trie<String, HashedFileObject> trie =
        new PatriciaTrie<String, HashedFileObject>(CHAR);
      for(final Map.Entry<String, JsonElement> entry :
View Full Code Here

    private final DateFormat iso8601Format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");

    @Override
    public Date deserialize(JsonElement json, Type type, JsonDeserializationContext context) throws JsonParseException {
        if (!(json instanceof JsonPrimitive)) {
            throw new JsonParseException("The date " + json + " is not a string!");
        }
        if (type != Date.class) {
            throw new IllegalArgumentException(getClass() + " cannot deserialize to " + type);
        }
        String value = json.getAsString();
View Full Code Here

    @Override
    public Color read(JsonReader reader) throws IOException {
        reader.beginObject();
        String next = reader.nextName();
        if (!next.equalsIgnoreCase("value")) {
            throw new JsonParseException("Key " + next + " isnt a valid key");
        }
        String hex = reader.nextString();
        reader.endObject();
        int[] rgb = toRGB(clamp(hex.substring(1)));
        return new Color(rgb[0], rgb[1], rgb[2]);
View Full Code Here

        @Override
        public NatRule deserialize(final JsonElement jsonElement, final Type type, final JsonDeserializationContext context) throws JsonParseException {
            final JsonObject jsonObject = jsonElement.getAsJsonObject();

            if (!jsonObject.has("type")) {
                throw new JsonParseException("Deserializing as a NatRule, but no type present in the json object");
            }

            final String natRuleType = jsonObject.get("type").getAsString();
            if ("SourceNatRule".equals(natRuleType)) {
                return context.deserialize(jsonElement, SourceNatRule.class);
            } else if ("DestinationNatRule".equals(natRuleType)) {
                return context.deserialize(jsonElement, DestinationNatRule.class);
            }

            throw new JsonParseException("Failed to deserialize type \"" + natRuleType + "\"");
        }
View Full Code Here

        @Override
        public RoutingConfig deserialize(final JsonElement jsonElement, final Type type, final JsonDeserializationContext context) throws JsonParseException {
            final JsonObject jsonObject = jsonElement.getAsJsonObject();

            if (!jsonObject.has("type")) {
                throw new JsonParseException("Deserializing as a RoutingConfig, but no type present in the json object");
            }

            final String routingConfigType = jsonObject.get("type").getAsString();
            if (SINGLE_DEFAULT_ROUTE_IMPLICIT_ROUTING_CONFIG.equals(routingConfigType)) {
                return context.deserialize(jsonElement, SingleDefaultRouteImplicitRoutingConfig.class);
            } else if (ROUTING_TABLE_ROUTING_CONFIG.equals(routingConfigType)) {
                return context.deserialize(jsonElement, RoutingTableRoutingConfig.class);
            }

            throw new JsonParseException("Failed to deserialize type \"" + routingConfigType + "\"");
        }
View Full Code Here

                String msg = obj.get("msg").getAsString();
                Constructor<Throwable> constructor = clazz.getConstructor(String.class, Throwable.class);
                Throwable th = constructor.newInstance(msg, cause);
                return th;
            } catch (ClassNotFoundException e) {
                throw new JsonParseException("Unable to find " + className);
            } catch (NoSuchMethodException e) {
                throw new JsonParseException("Unable to find constructor for " + className);
            } catch (SecurityException e) {
                throw new JsonParseException("Unable to get over security " + className);
            } catch (InstantiationException e) {
                throw new JsonParseException("Unable to instantiate " + className);
            } catch (IllegalAccessException e) {
                throw new JsonParseException("Illegal access to " + className, e);
            } catch (IllegalArgumentException e) {
                throw new JsonParseException("Illegal argument to " + className, e);
            } catch (InvocationTargetException e) {
                throw new JsonParseException("Cannot invoke " + className, e);
            }
        }
View Full Code Here

    private static class MapDecoder implements JsonDeserializer<Map<String, String>> {
        @Override
        public Map<String, String> deserialize(JsonElement arg0, Type arg1, JsonDeserializationContext arg2) throws JsonParseException {
            if (!arg0.isJsonObject()) {
                throw new JsonParseException(arg0.toString() + " is not Json Object, cannot convert to map");
            }
            JsonObject objs = arg0.getAsJsonObject();
            Map<String, String> map = new HashMap<String, String>();
            for (Entry<String, JsonElement> e : objs.entrySet()) {
                if (!e.getValue().isJsonPrimitive()) {
                    throw new JsonParseException(e.getValue().toString() + " is not a Json primitive," + arg0 + " can not convert to map");
                }
                map.put(e.getKey(), e.getValue().getAsString());
            }
            return map;
        }
View Full Code Here

TOP

Related Classes of com.google.gson.JsonParseException

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.