Package com.google.gson

Examples of com.google.gson.JsonParseException


   */
  public static String parsePassword(String serializedUser)
  {
    if(!serializedUser.contains("password"))
    {
      throw new JsonParseException("The given JSON string did not contain a password field.");
    }
   
    int fieldStartIndex = serializedUser.indexOf("password");
    int separator = serializedUser.indexOf(':', fieldStartIndex);
    int startIndex = serializedUser.indexOf('"', separator) + 1;
View Full Code Here


   */
  public static String parseFieldFromJSON(String serializedModel, String FieldName)
  {
    if(!serializedModel.contains(FieldName))
    {
      throw new JsonParseException("The given JSON string did not contain specified field.");
    }
   
    int fieldStartIndex = serializedModel.indexOf(FieldName);
    int separator = serializedModel.indexOf(':', fieldStartIndex);
    int startIndex = serializedModel.indexOf('"', separator) + 1;
View Full Code Here

  public DefectEvent deserialize(JsonElement element, Type type,
      JsonDeserializationContext context) throws JsonParseException {
    // we need to switch on the type field to figure out the concrete class to instantiate
    JsonObject object = element.getAsJsonObject();
    if(!object.has("type")) {
      throw new JsonParseException("DefectEvent does not have type information");
    }
    EventType eType = context.deserialize(object.get("type"), EventType.class);
    if(eType != null) { // type could be any garbage string, eType null if not in enum
      switch(eType) {
      case CHANGESET:
        return context.deserialize(element, DefectChangeset.class);
      case COMMENT:
        return context.deserialize(element, Comment.class);
      }
    }
    throw new JsonParseException("DefectEvent type is unrecognized");
  }
View Full Code Here

  {
    logger.log(Level.FINE, "Attempting username parsing...");
   
    if(serializedUser == null || !serializedUser.contains("username"))
    {
      throw new JsonParseException("The given JSON string did not contain a username field.");
    }
   
    int fieldStartIndex = serializedUser.indexOf("username");
    int separator = serializedUser.indexOf(':', fieldStartIndex);
    int startIndex = serializedUser.indexOf('"', separator) + 1;
View Full Code Here

     
      // return the DefectChangeset
      return retVal;
    }
    else {
      throw new JsonParseException("DefectChangeset type is unrecognized");
    }
  }
View Full Code Here

   
    private class CharacterDeserializer implements JsonDeserializer<Character> {
        @Override
        public Character deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
            if (!json.isJsonPrimitive())
                throw new JsonParseException("Expected character primitive");
            JsonPrimitive prim = json.getAsJsonPrimitive();
            String str = prim.getAsString();
            if (str.length() == 0)
                return Character.valueOf('\0');
            else if (str.length() == 1)
                return Character.valueOf(str.charAt(0));
            else
                throw new JsonParseException("Expected a single character");
        }
View Full Code Here

   
    private class NumberDeserializer implements JsonDeserializer<Number> {
        @Override
        public Number deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
            if (!json.isJsonPrimitive())
                throw new JsonParseException("Expected primitive of type " + typeOfT);

            String text = json.getAsString();
            if (typeOfT.equals(Double.class))
                return Double.parseDouble(text);
            else if (typeOfT.equals(Float.class))
View Full Code Here

       
        @Override
        @SuppressWarnings({ "unchecked", "rawtypes" })
        public DtoContainer<?> deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
            if (!json.isJsonObject())
                throw new JsonParseException("Expected top-level json to be an object");
           
            JsonObject root = json.getAsJsonObject();
            if (root.entrySet().size() != 1)
                throw new JsonParseException("Expected top-level json to have a single property");

            Entry<String, JsonElement> child = root.entrySet().iterator().next();
            if (child.getValue().isJsonArray()) {
                // assuming we're dealing with multiples, expect plural name
                JsonArray array = child.getValue().getAsJsonArray();
                Class<? extends Dto> type = pluralDtoMap.get(child.getKey());
                if (type == null)
                    throw new JsonParseException("Unexpected property name: " + child.getKey());
               
                List<Dto> dtos = new ArrayList<Dto>(array.size());
                for (JsonElement el: array)
                    dtos.add((Dto) context.deserialize(el, type));
               
                return new DtoContainer(type, dtos);
            } else if (child.getValue().isJsonObject()) {
                // assume we're dealing with a single object
                Class<? extends Dto> type = singleDtoMap.get(child.getKey());
                if (type == null)
                    throw new JsonParseException("Unexpected property name: " + child.getKey());
               
                Dto wrapped = context.deserialize(child.getValue(), type);
                return new DtoContainer(type, wrapped);
            }
           
            throw new JsonParseException("Expected top-level json to have a single property that's an object or array");
        }
View Full Code Here

      groupName = name;
    }
    Stage s = new Stage(name, toDatabaseFile(libraryId));
    Map<String, Object> config = SerializationUtils.fromJson(jsonConfig);
    if (null == config) {
      throw new JsonException(new JsonParseException("Configuration was empty"));
    } else if (!config.containsKey("stageClass")) {
      throw new JsonException(new JsonParseException("Required configuration parameter 'stageClass' missing"));
    }
    config.put("stageName", name);
    config.put("stageGroup", groupName);
    config.put("libId", libraryId);
    s.setProperties(config);
View Full Code Here

        try {
          descriptor = Class.forName(workerType);
        } catch (Exception ex) {
          ex.printStackTrace();
          // TODO Use a better logger.
          throw new JsonParseException(ex.getMessage(), ex.getCause());
        }
        // Descriptor shall not be null by now.
        JsonElement dataElement = object.get(SYMBOL_DATA);
        if (dataElement != null) {
          result = context.deserialize(dataElement, descriptor);
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.