Package com.google.gson

Examples of com.google.gson.JsonParseException


    private final DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    public Timestamp deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
          throws JsonParseException {
      if (!(json instanceof JsonPrimitive)) {
        throw new JsonParseException("The date should be a string value");
      }

      try {
        Date date = format.parse(json.getAsString());
        return new Timestamp(date.getTime());
      } catch (ParseException e) {
        throw new JsonParseException(e);
      }
    }
View Full Code Here


    static String decode(JsonElement json)
        throws JsonParseException
    {
      if (!json.isJsonPrimitive()) {
        throw new JsonParseException("The value for a Base64String must be a valid String");
      }
      JsonPrimitive primitive = (JsonPrimitive) json;
      if (!primitive.isString()) {
        throw new JsonParseException("The value for a Base64String must be a valid String");
      }
      return Base64.decodeToString(primitive.getAsString());
    }
View Full Code Here

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

  @Override
  public JsonRpcResponse deserialize(JsonElement json, Type typeOfT,
      JsonDeserializationContext context) throws JsonParseException {

    if (!(json instanceof JsonObject)) {
      throw new JsonParseException("JonObject expected, found "
          + json.getClass().getSimpleName());
    }

    JsonObject jObject = (JsonObject) json;

    if (!jObject.has("jsonrpc")) {
      throw new JsonParseException(
          "Invalid JsonRpc response lacking version 'jsonrpc' field");
    }

    if (!jObject.get("jsonrpc").getAsString()
        .equals(JsonRpcConstants.JSON_RPC_VERSION)) {
      throw new JsonParseException("Invalid JsonRpc version");
    }

    if (!jObject.has("id")) {
      throw new JsonParseException(
          "Invalid JsonRpc response lacking 'id' field");
    }

    if (jObject.has("result")) {
      return new JsonRpcResponse(
          (JsonRpcResponseResult) context.deserialize(
              jObject.get("result").getAsJsonObject(),
              JsonRpcResponseResult.class), jObject.get("id")
              .getAsInt());
    } else if (jObject.has("error")) {
      return new JsonRpcResponse(
          (JsonRpcResponseError) context.deserialize(
              jObject.get("error").getAsJsonObject(),
              JsonRpcResponseError.class), jObject.get("id")
              .getAsInt());
    } else {
      throw new JsonParseException(
          "Invalid JsonRpc response lacking 'result' and 'error' fields");
    }

  }
View Full Code Here

  @Override
  public JsonRpcRequest deserialize(JsonElement json, Type typeOfT,
      JsonDeserializationContext context) throws JsonParseException {

    if (!(json instanceof JsonObject)) {
      throw new JsonParseException(
          "Invalid JsonRpc request showning JsonElement type "
              + json.getClass().getSimpleName());
    }

    JsonObject jObject = (JsonObject) json;

    if (!jObject.has("jsonrpc")) {
      throw new JsonParseException(
          "Invalid JsonRpc request lacking version 'jsonrpc' field");
    }

    if (!jObject.get("jsonrpc").getAsString()
        .equals(JsonRpcConstants.JSON_RPC_VERSION)) {
      throw new JsonParseException("Invalid JsonRpc version");
    }

    if (!jObject.has("method")) {
      throw new JsonParseException(
          "Invalid JsonRpc request lacking 'method' field");
    }

    if (!jObject.has("params")) {
      throw new JsonParseException(
          "Invalid JsonRpc request lacking 'params' field");
    }

    if (!jObject.has("id")) {
      throw new JsonParseException(
          "Invalid JsonRpc request lacking 'id' field");
    }

    return new JsonRpcRequest(jObject.get("method").getAsString(),
        (JsonRpcRequestParams) context.deserialize(jObject
View Full Code Here

  @Override
  public Response<?> deserialize(JsonElement json, Type typeOfT,
      JsonDeserializationContext context) throws JsonParseException {

    if (!(json instanceof JsonObject)) {
      throw new JsonParseException("JonObject expected, found "
          + json.getClass().getSimpleName());
    }

    JsonObject jObject = (JsonObject) json;

    if (!jObject.has(JSON_RPC_PROPERTY)) {
      throw new JsonParseException(
          "Invalid JsonRpc response lacking version '"
              + JSON_RPC_PROPERTY + "' field");
    }

    if (!jObject.get(JSON_RPC_PROPERTY).getAsString()
        .equals(JsonRpcConstants.JSON_RPC_VERSION)) {
      throw new JsonParseException("Invalid JsonRpc version");
    }

    Integer id;
    try {
      id = Integer.valueOf(jObject.get(ID_PROPERTY).getAsInt());
    } catch (Exception e) {
      throw new JsonParseException(
          "Invalid JsonRpc response. It lacks a valid '"
              + ID_PROPERTY + "' field");
    }

    if (jObject.has(RESULT_PROPERTY)) {

      ParameterizedType parameterizedType = (ParameterizedType) typeOfT;

      return new Response<>(id, context.deserialize(
          jObject.get(RESULT_PROPERTY),
          parameterizedType.getActualTypeArguments()[0]));

    } else if (jObject.has(ERROR_PROPERTY)) {

      return new Response<>(id, (ResponseError) context.deserialize(
          jObject.get(ERROR_PROPERTY), ResponseError.class));

    } else {
      throw new JsonParseException("Invalid JsonRpc response lacking '"
          + RESULT_PROPERTY + "' and '" + ERROR_PROPERTY
          + "' fields. " + json);
    }

  }
View Full Code Here

  @Override
  public Request<?> deserialize(JsonElement json, Type typeOfT,
      JsonDeserializationContext context) throws JsonParseException {

    if (!(json instanceof JsonObject)) {
      throw new JsonParseException(
          "Invalid JsonRpc request showning JsonElement type "
              + json.getClass().getSimpleName());
    }

    JsonObject jObject = (JsonObject) json;

    if (!jObject.has(JSON_RPC_PROPERTY)) {
      throw new JsonParseException(
          "Invalid JsonRpc request lacking version '"
              + JSON_RPC_PROPERTY + "' field");
    }

    if (!jObject.get("jsonrpc").getAsString().equals(JSON_RPC_VERSION)) {
      throw new JsonParseException("Invalid JsonRpc version");
    }

    if (!jObject.has(METHOD_PROPERTY)) {
      throw new JsonParseException("Invalid JsonRpc request lacking '"
          + METHOD_PROPERTY + "' field");
    }

    Integer id = null;
    if (jObject.has(ID_PROPERTY)) {
View Full Code Here

  @Override
  public Props deserialize(JsonElement json, Type typeOfT,
      JsonDeserializationContext context) throws JsonParseException {

    if (!(json instanceof JsonObject)) {
      throw new JsonParseException("Cannot convert " + json
          + " to Props object");
    }

    JsonObject jObject = (JsonObject) json;
View Full Code Here

     JsonObject deflated = projectElement.getAsJsonObject();
    
     // check for the unique identifier <idNum> field.
     if(!deflated.has("idNum"))
     {
       throw new JsonParseException("The serialized Project did not contain the required idNum field.");
     }
        
     // for all other attributes: instantiate as null, fill in if given.
    
     //int idNum = deflated.get("idNum").getAsInt();
View Full Code Here

      JsonDeserializationContext context) throws JsonParseException {
     JsonObject deflated = userElement.getAsJsonObject();
    
     if(!deflated.has("username"))
     {
       throw new JsonParseException("The serialized User did not contain the required username field.");
     }
    
     // for all other attributes: instantiate as null, fill in if given.
    
     int idNum = 0;
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.