Search in sources :

Example 1 with JsonSyntaxException

use of com.google.gson.JsonSyntaxException in project che by eclipse.

the class FactoryServiceTest method shouldThrowBadRequestExceptionWhenInvalidFactorySectionProvided.

@Test
public void shouldThrowBadRequestExceptionWhenInvalidFactorySectionProvided() throws Exception {
    doThrow(new JsonSyntaxException("Invalid json")).when(factoryBuilderSpy).build(any(InputStream.class));
    final Response response = given().auth().basic(ADMIN_USER_NAME, ADMIN_USER_PASSWORD).multiPart("factory", "invalid content", FACTORY_IMAGE_MIME_TYPE).expect().statusCode(400).when().post(SERVICE_PATH);
    final ServiceError err = getFromResponse(response, ServiceError.class);
    assertEquals(err.getMessage(), "Invalid JSON value of the field 'factory' provided");
}
Also used : Response(com.jayway.restassured.response.Response) ServiceError(org.eclipse.che.api.core.rest.shared.dto.ServiceError) JsonSyntaxException(com.google.gson.JsonSyntaxException) InputStream(java.io.InputStream) Test(org.testng.annotations.Test)

Example 2 with JsonSyntaxException

use of com.google.gson.JsonSyntaxException in project MinecraftForge by MinecraftForge.

the class ModListHelper method parseListFile.

private static void parseListFile(String listFile) {
    File f;
    try {
        if (listFile.startsWith("absolute:"))
            f = new File(listFile.substring(9)).getCanonicalFile();
        else
            f = new File(mcDirectory, listFile).getCanonicalFile();
    } catch (IOException e2) {
        FMLRelaunchLog.log(Level.INFO, e2, "Unable to canonicalize path %s relative to %s", listFile, mcDirectory.getAbsolutePath());
        return;
    }
    if (!f.exists()) {
        FMLRelaunchLog.info("Failed to find modList file %s", f.getAbsolutePath());
        return;
    }
    if (visitedFiles.contains(f)) {
        FMLRelaunchLog.severe("There appears to be a loop in the modListFile hierarchy. You shouldn't do this!");
        throw new RuntimeException("Loop detected, impossible to load modlistfile");
    }
    String json;
    try {
        json = Files.asCharSource(f, Charsets.UTF_8).read();
    } catch (IOException e1) {
        FMLRelaunchLog.log(Level.INFO, e1, "Failed to read modList json file %s.", listFile);
        return;
    }
    Gson gsonParser = new Gson();
    JsonModList modList;
    try {
        modList = gsonParser.fromJson(json, JsonModList.class);
    } catch (JsonSyntaxException e) {
        FMLRelaunchLog.log(Level.INFO, e, "Failed to parse modList json file %s.", listFile);
        return;
    }
    visitedFiles.add(f);
    // We visit parents before children, so the additionalMods list is sorted from parent to child
    if (modList.parentList != null) {
        parseListFile(modList.parentList);
    }
    File repoRoot = new File(modList.repositoryRoot);
    if (!repoRoot.exists()) {
        FMLRelaunchLog.info("Failed to find the specified repository root %s", modList.repositoryRoot);
        return;
    }
    for (String s : modList.modRef) {
        StringBuilder fileName = new StringBuilder();
        StringBuilder genericName = new StringBuilder();
        String[] parts = s.split(":");
        fileName.append(parts[0].replace('.', File.separatorChar));
        genericName.append(parts[0]);
        fileName.append(File.separatorChar);
        fileName.append(parts[1]).append(File.separatorChar);
        genericName.append(":").append(parts[1]);
        fileName.append(parts[2]).append(File.separatorChar);
        fileName.append(parts[1]).append('-').append(parts[2]);
        if (parts.length == 4) {
            fileName.append('-').append(parts[3]);
            genericName.append(":").append(parts[3]);
        }
        fileName.append(".jar");
        tryAddFile(fileName.toString(), repoRoot, genericName.toString());
    }
}
Also used : JsonSyntaxException(com.google.gson.JsonSyntaxException) Gson(com.google.gson.Gson) IOException(java.io.IOException) File(java.io.File)

Example 3 with JsonSyntaxException

use of com.google.gson.JsonSyntaxException in project SneakerBot by Penor.

the class Config method load.

public static ArrayList<ConfigObject> load(String name) {
    File file = new File(name);
    if (!file.exists()) {
        System.out.println(name + " does not exist; One has been created for you.");
        create(name);
        return null;
    }
    Type type = new TypeToken<ArrayList<ConfigObject>>() {
    }.getType();
    try {
        return new GsonBuilder().create().fromJson(new FileReader(name), type);
    } catch (JsonIOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (JsonSyntaxException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return null;
}
Also used : Type(java.lang.reflect.Type) JsonSyntaxException(com.google.gson.JsonSyntaxException) JsonIOException(com.google.gson.JsonIOException) GsonBuilder(com.google.gson.GsonBuilder) ArrayList(java.util.ArrayList) FileNotFoundException(java.io.FileNotFoundException) FileReader(java.io.FileReader) File(java.io.File)

Example 4 with JsonSyntaxException

use of com.google.gson.JsonSyntaxException in project SneakerBot by Penor.

the class Credentials method load.

public static HashMap<String, CredentialObject> load(String name) {
    File file = new File(name);
    if (!file.exists()) {
        System.out.println(name + " does not exist; One has been created for you.");
        create(name);
        return null;
    }
    Type type = new TypeToken<HashMap<String, CredentialObject>>() {
    }.getType();
    try {
        return new GsonBuilder().create().fromJson(new FileReader(name), type);
    } catch (JsonIOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (JsonSyntaxException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return null;
}
Also used : Type(java.lang.reflect.Type) JsonSyntaxException(com.google.gson.JsonSyntaxException) JsonIOException(com.google.gson.JsonIOException) HashMap(java.util.HashMap) GsonBuilder(com.google.gson.GsonBuilder) FileNotFoundException(java.io.FileNotFoundException) FileReader(java.io.FileReader) File(java.io.File)

Example 5 with JsonSyntaxException

use of com.google.gson.JsonSyntaxException in project pratilipi by Pratilipi.

the class GenericApi method executeApi.

final Object executeApi(GenericApi api, Method apiMethod, JsonObject requestPayloadJson, Class<? extends GenericRequest> apiMethodParameterType, HttpServletRequest request) {
    try {
        GenericRequest apiRequest = new Gson().fromJson(requestPayloadJson, apiMethodParameterType);
        if (apiRequest instanceof GenericFileUploadRequest) {
            GenericFileUploadRequest gfuRequest = (GenericFileUploadRequest) apiRequest;
            try {
                ServletFileUpload upload = new ServletFileUpload();
                FileItemIterator iterator = upload.getItemIterator(request);
                while (iterator.hasNext()) {
                    FileItemStream fileItemStream = iterator.next();
                    if (!fileItemStream.isFormField()) {
                        gfuRequest.setName(fileItemStream.getName());
                        gfuRequest.setData(IOUtils.toByteArray(fileItemStream.openStream()));
                        gfuRequest.setMimeType(fileItemStream.getContentType());
                        break;
                    }
                }
            } catch (IOException | FileUploadException e) {
                throw new UnexpectedServerException();
            }
        }
        JsonObject errorMessages = apiRequest.validate();
        if (errorMessages.entrySet().size() > 0)
            return new InvalidArgumentException(errorMessages);
        else
            return apiMethod.invoke(api, apiRequest);
    } catch (JsonSyntaxException e) {
        logger.log(Level.SEVERE, "Invalid JSON in request body.", e);
        return new InvalidArgumentException("Invalid JSON in request body.");
    } catch (UnexpectedServerException e) {
        return e;
    } catch (InvocationTargetException e) {
        Throwable te = e.getTargetException();
        if (te instanceof InvalidArgumentException || te instanceof InsufficientAccessException || te instanceof UnexpectedServerException) {
            return te;
        } else {
            logger.log(Level.SEVERE, "Failed to execute API.", te);
            return new UnexpectedServerException();
        }
    } catch (IllegalAccessException | IllegalArgumentException e) {
        logger.log(Level.SEVERE, "Failed to execute API.", e);
        return new UnexpectedServerException();
    }
}
Also used : GenericFileUploadRequest(com.pratilipi.api.shared.GenericFileUploadRequest) Gson(com.google.gson.Gson) JsonObject(com.google.gson.JsonObject) IOException(java.io.IOException) InsufficientAccessException(com.pratilipi.common.exception.InsufficientAccessException) InvocationTargetException(java.lang.reflect.InvocationTargetException) ServletFileUpload(org.apache.commons.fileupload.servlet.ServletFileUpload) UnexpectedServerException(com.pratilipi.common.exception.UnexpectedServerException) InvalidArgumentException(com.pratilipi.common.exception.InvalidArgumentException) JsonSyntaxException(com.google.gson.JsonSyntaxException) FileItemStream(org.apache.commons.fileupload.FileItemStream) GenericRequest(com.pratilipi.api.shared.GenericRequest) FileItemIterator(org.apache.commons.fileupload.FileItemIterator) FileUploadException(org.apache.commons.fileupload.FileUploadException)

Aggregations

JsonSyntaxException (com.google.gson.JsonSyntaxException)379 Gson (com.google.gson.Gson)169 IOException (java.io.IOException)83 HashMap (java.util.HashMap)68 JsonElement (com.google.gson.JsonElement)62 JsonObject (com.google.gson.JsonObject)58 ArrayList (java.util.ArrayList)46 JsonParser (com.google.gson.JsonParser)42 GsonBuilder (com.google.gson.GsonBuilder)38 Cursor (android.database.Cursor)33 InputStreamReader (java.io.InputStreamReader)30 Map (java.util.Map)30 BadRequestException (co.cask.cdap.common.BadRequestException)28 JsonArray (com.google.gson.JsonArray)28 Path (javax.ws.rs.Path)28 Reader (java.io.Reader)26 Type (java.lang.reflect.Type)23 JsonIOException (com.google.gson.JsonIOException)21 FileReader (java.io.FileReader)21 File (java.io.File)19