Search in sources :

Example 56 with JsonSyntaxException

use of com.google.gson.JsonSyntaxException in project lite-apps by chimbori.

the class TagsCollector method updateTagsJson.

public static void updateTagsJson() throws IOException {
    Gson gson = GsonInstance.getPrettyPrinter();
    // Read the list of all known tags from the tags.json file. In case we discover any new tags,
    // we will add them to this file, taking care not to overwrite those that already exist.
    LibraryTagsList tagsGson = LibraryTagsList.fromGson(gson, new FileReader(FilePaths.LITE_APPS_TAGS_JSON));
    Map<String, LibraryTag> globalTags = new HashMap<>();
    File[] liteAppDirs = FilePaths.LITE_APPS_SRC_DIR.listFiles();
    for (File liteAppDirectory : liteAppDirs) {
        if (!liteAppDirectory.isDirectory()) {
            // Probably a temporary file, like .DS_Store.
            continue;
        }
        File manifestJsonFile = new File(liteAppDirectory, FilePaths.MANIFEST_JSON_FILE_NAME);
        if (!manifestJsonFile.exists()) {
            throw new MissingManifestException(liteAppDirectory.getName());
        }
        Manifest manifest;
        try {
            manifest = gson.fromJson(new FileReader(manifestJsonFile), Manifest.class);
        } catch (JsonSyntaxException e) {
            System.err.println("Failed to parse JSON: " + liteAppDirectory.getName());
            throw e;
        }
        // For all tags applied to this manifest, check if they exist in the global tags list.
        if (manifest.tags != null) {
            for (String tagName : manifest.tags) {
                LibraryTag tag = globalTags.get(tagName);
                if (tag == null) {
                    // If this is the first time we are seeing this tag, create a new JSONArray to hold its contents.
                    LibraryTag newTag = new LibraryTag(tagName);
                    globalTags.put(tagName, newTag);
                    tagsGson.addTag(newTag);
                }
            }
        }
    }
    // Write the tags to JSON
    FileUtils.writeFile(FilePaths.LITE_APPS_TAGS_JSON, tagsGson.toJson(gson));
}
Also used : LibraryTag(com.chimbori.hermitcrab.schema.library.LibraryTag) JsonSyntaxException(com.google.gson.JsonSyntaxException) HashMap(java.util.HashMap) Gson(com.google.gson.Gson) FileReader(java.io.FileReader) LibraryTagsList(com.chimbori.hermitcrab.schema.library.LibraryTagsList) Manifest(com.chimbori.hermitcrab.schema.manifest.Manifest) File(java.io.File)

Example 57 with JsonSyntaxException

use of com.google.gson.JsonSyntaxException in project coastal-hazards by USGS-CIDA.

the class MetadataResource method getMetadataSummaryByAttribtueUsingItemID.

@GET
@Path("/summarize/itemid/{itemid}/attribute/{attr}")
@Produces(MediaType.APPLICATION_JSON)
public Response getMetadataSummaryByAttribtueUsingItemID(@PathParam("itemid") String itemId, @PathParam("attr") String attr) throws URISyntaxException {
    Response response;
    try (ItemManager itemManager = new ItemManager()) {
        Item item = itemManager.load(itemId);
        String jsonSummary = MetadataUtil.getSummaryFromWPS(getMetadataUrl(item), attr);
        Summary summary = GsonUtil.getDefault().fromJson(jsonSummary, Summary.class);
        response = Response.ok(GsonUtil.getDefault().toJson(summary, Summary.class), MediaType.APPLICATION_JSON_TYPE).build();
    } catch (IOException | ParserConfigurationException | SAXException | JsonSyntaxException ex) {
        Map<String, String> err = new HashMap<>();
        err.put("message", ex.getMessage());
        response = Response.serverError().entity(GsonUtil.getDefault().toJson(err, HashMap.class)).build();
    }
    return response;
}
Also used : Response(javax.ws.rs.core.Response) Item(gov.usgs.cida.coastalhazards.model.Item) JsonSyntaxException(com.google.gson.JsonSyntaxException) ItemManager(gov.usgs.cida.coastalhazards.jpa.ItemManager) Summary(gov.usgs.cida.coastalhazards.model.summary.Summary) IOException(java.io.IOException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) HashMap(java.util.HashMap) Map(java.util.Map) SAXException(org.xml.sax.SAXException) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Example 58 with JsonSyntaxException

use of com.google.gson.JsonSyntaxException in project edx-app-android by edx.

the class ISO8601DateTypeAdapter method read.

@Override
@NonNull
public Date read(final JsonReader in) throws IOException {
    final String date = in.nextString();
    final ParsePosition parsePosition = new ParsePosition(0);
    try {
        return ISO8601Utils.parse(date, parsePosition);
    } catch (ParseException e) {
        throw new JsonSyntaxException(date, e);
    }
}
Also used : JsonSyntaxException(com.google.gson.JsonSyntaxException) ParseException(java.text.ParseException) ParsePosition(java.text.ParsePosition) NonNull(android.support.annotation.NonNull)

Example 59 with JsonSyntaxException

use of com.google.gson.JsonSyntaxException in project edx-app-android by edx.

the class LoginAPI method register.

@NonNull
private void register(Bundle parameters) throws Exception {
    final Map<String, String> parameterMap = new HashMap<>();
    for (String key : parameters.keySet()) {
        parameterMap.put(key, parameters.getString(key));
    }
    Response<ResponseBody> response = loginService.register(parameterMap).execute();
    if (!response.isSuccessful()) {
        final int errorCode = response.code();
        final String errorBody = response.errorBody().string();
        if ((errorCode == HttpStatus.BAD_REQUEST || errorCode == HttpStatus.CONFLICT) && !android.text.TextUtils.isEmpty(errorBody)) {
            try {
                final FormFieldMessageBody body = gson.fromJson(errorBody, FormFieldMessageBody.class);
                if (body != null && body.size() > 0) {
                    throw new RegistrationException(body);
                }
            } catch (JsonSyntaxException ex) {
            // Looks like the response does not contain form validation errors.
            }
        }
        throw new HttpStatusException(response);
    }
}
Also used : JsonSyntaxException(com.google.gson.JsonSyntaxException) HashMap(java.util.HashMap) HttpStatusException(org.edx.mobile.http.HttpStatusException) FormFieldMessageBody(org.edx.mobile.model.api.FormFieldMessageBody) ResponseBody(okhttp3.ResponseBody) NonNull(android.support.annotation.NonNull)

Example 60 with JsonSyntaxException

use of com.google.gson.JsonSyntaxException in project java-sdk by watson-developer-cloud.

the class CredentialUtils method getVCAPServices.

/**
 * Gets the <b>VCAP_SERVICES</b> environment variable and return it as a {@link JsonObject}.
 *
 * @return the VCAP_SERVICES as a {@link JsonObject}.
 */
private static JsonObject getVCAPServices() {
    final String envServices = services != null ? services : System.getenv(VCAP_SERVICES);
    if (envServices == null) {
        return null;
    }
    JsonObject vcapServices = null;
    try {
        final JsonParser parser = new JsonParser();
        vcapServices = (JsonObject) parser.parse(envServices);
    } catch (final JsonSyntaxException e) {
        log.log(Level.INFO, "Error parsing VCAP_SERVICES", e);
    }
    return vcapServices;
}
Also used : JsonSyntaxException(com.google.gson.JsonSyntaxException) JsonObject(com.google.gson.JsonObject) JsonParser(com.google.gson.JsonParser)

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