Search in sources :

Example 81 with GsonBuilder

use of com.google.gson.GsonBuilder in project mobile-sdk-android by meniga.

the class GsonProvider method getGsonBuilder.

public static Gson getGsonBuilder() {
    if (gson == null) {
        GsonBuilder gsonBuilder = new GsonBuilder();
        gsonBuilder.setFieldNamingStrategy(FieldNamingPolicy.IDENTITY);
        // Register custom de/serializers.
        gsonBuilder.registerTypeAdapter(DateTime.class, new DateTimeSerializer());
        gsonBuilder.registerTypeAdapter(LocalDate.class, new LocalDateSerializer());
        gsonBuilder.registerTypeAdapter(MenigaDecimal.class, new MenigaDecimalSerializer());
        gsonBuilder.registerTypeAdapter(BudgetDate.class, new BudgetDateSerializer());
        gsonBuilder.registerTypeAdapter(MenigaCategoryScore.class, new MenigaCategoryScoreSerializer());
        gson = gsonBuilder.create();
    }
    return gson;
}
Also used : GsonBuilder(com.google.gson.GsonBuilder) MenigaCategoryScoreSerializer(com.meniga.sdk.webservices.serializers.MenigaCategoryScoreSerializer) DateTimeSerializer(com.meniga.sdk.webservices.serializers.DateTimeSerializer) LocalDateSerializer(com.meniga.sdk.webservices.serializers.LocalDateSerializer) MenigaDecimalSerializer(com.meniga.sdk.webservices.serializers.MenigaDecimalSerializer) BudgetDateSerializer(com.meniga.sdk.webservices.serializers.BudgetDateSerializer)

Example 82 with GsonBuilder

use of com.google.gson.GsonBuilder in project carbon-apimgt by wso2.

the class FileBasedApiImportExportManager method getEndpointsFromExtractedArchive.

private Set<Endpoint> getEndpointsFromExtractedArchive(String endpointLocation, String apiName, String version) throws APIMgtEntityImportExportException {
    File endpointsRootDirectory = new File(endpointLocation);
    Set<Endpoint> endpoints = new HashSet<>();
    if (endpointsRootDirectory.isDirectory()) {
        File[] endpointFiles = endpointsRootDirectory.listFiles(File::isFile);
        if (endpointFiles == null) {
            // no endpoints in the given location, can't continue
            String errorMsg = "No endpoints found at " + endpointsRootDirectory;
            log.error(errorMsg);
            throw new APIMgtEntityImportExportException(errorMsg);
        }
        Gson gson = new GsonBuilder().create();
        for (File endpointFile : endpointFiles) {
            // read everything
            String content = null;
            try {
                content = APIFileUtils.readFileContentAsText(endpointFile.getPath());
            } catch (APIMgtDAOException e) {
                String errorMsg = "Unable to read endpoints from " + endpointFile.getPath();
                log.error(errorMsg, e);
                throw new APIMgtEntityImportExportException(errorMsg, e);
            }
            endpoints.add(gson.fromJson(content, Endpoint.class));
        }
    }
    return endpoints;
}
Also used : APIMgtDAOException(org.wso2.carbon.apimgt.core.exception.APIMgtDAOException) Endpoint(org.wso2.carbon.apimgt.core.models.Endpoint) GsonBuilder(com.google.gson.GsonBuilder) Gson(com.google.gson.Gson) File(java.io.File) HashSet(java.util.HashSet) APIMgtEntityImportExportException(org.wso2.carbon.apimgt.core.exception.APIMgtEntityImportExportException)

Example 83 with GsonBuilder

use of com.google.gson.GsonBuilder in project carbon-apimgt by wso2.

the class FileBasedApiImportExportManager method getDocumentInfoFromExtractedArchive.

/**
 * Retrieves {@link DocumentInfo} instance from the directory containing docs
 *
 * @param documentImportLocation path to the directory containing docs
 * @param apiName                API name
 * @param version                API version
 * @return Set of {@link DocumentInfo} insjtaces
 */
private Set<DocumentInfo> getDocumentInfoFromExtractedArchive(String documentImportLocation, String apiName, String version) {
    Set<DocumentInfo> documents = new HashSet<>();
    File rootDocumentationDirectoryForAPI = new File(documentImportLocation);
    if (!rootDocumentationDirectoryForAPI.isDirectory()) {
        // no Docs!
        log.debug("No documentation found for API name: " + apiName + ", version: " + version);
        return documents;
    }
    File[] documentationDirectories = rootDocumentationDirectoryForAPI.listFiles(File::isDirectory);
    if (documentationDirectories == null) {
        // do docs!
        log.debug("No documents found at " + documentImportLocation);
        return documents;
    }
    for (File docDir : documentationDirectories) {
        // read the 'doc.json'
        String content;
        try {
            content = APIFileUtils.readFileContentAsText(docDir.getPath() + File.separator + DOCUMENTATION_DEFINITION_FILE);
            Gson gson = new GsonBuilder().create();
            documents.add(gson.fromJson(content, DocumentInfo.class));
        // add the doc
        } catch (APIManagementException e) {
            // no need to throw, log and continue
            log.error("Error in importing documentation from file: " + docDir.getPath() + " for API: " + apiName + ", version: " + version);
        }
    }
    return documents;
}
Also used : APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) GsonBuilder(com.google.gson.GsonBuilder) Gson(com.google.gson.Gson) File(java.io.File) DocumentInfo(org.wso2.carbon.apimgt.core.models.DocumentInfo) HashSet(java.util.HashSet)

Example 84 with GsonBuilder

use of com.google.gson.GsonBuilder in project carbon-apimgt by wso2.

the class FileBasedApplicationImportExportManager method parseApplicationFile.

/**
 * Extracts the details of an Application from a json file
 *
 * @param applicationDetailsFilePath Directory which contains the json file
 * @return an application object containing the details extracted from the json file
 * @throws APIMgtEntityImportExportException
 */
private Application parseApplicationFile(String applicationDetailsFilePath) throws APIMgtEntityImportExportException {
    String applicationDetailsString;
    try {
        applicationDetailsString = new String(Files.readAllBytes(Paths.get(applicationDetailsFilePath)), StandardCharsets.UTF_8);
    } catch (IOException e) {
        String errorMsg = "Unable to read application details from file at " + applicationDetailsFilePath;
        throw new APIMgtEntityImportExportException(errorMsg, e);
    }
    // convert to bean
    Gson gson = new GsonBuilder().create();
    // returns an application object from a json string
    Application applicationDetails = gson.fromJson(applicationDetailsString, Application.class);
    return applicationDetails;
}
Also used : GsonBuilder(com.google.gson.GsonBuilder) Gson(com.google.gson.Gson) IOException(java.io.IOException) Application(org.wso2.carbon.apimgt.core.models.Application) APIMgtEntityImportExportException(org.wso2.carbon.apimgt.core.exception.APIMgtEntityImportExportException)

Example 85 with GsonBuilder

use of com.google.gson.GsonBuilder in project Terasology by MovingBlocks.

the class AbstractEditorScreen method updateAutosave.

/**
 * Updates the autosave file with the current state of the tree.
 */
protected void updateAutosave() {
    if (!disableAutosave) {
        try (BufferedOutputStream outputStream = new BufferedOutputStream(Files.newOutputStream(getAutosaveFile()))) {
            JsonElement editorContents = JsonTreeConverter.deserialize(getEditor().getModel().getNode(0).getRoot());
            JsonObject autosaveObject = new JsonObject();
            autosaveObject.addProperty("selectedAsset", getSelectedAsset());
            autosaveObject.add("editorContents", editorContents);
            String jsonString = new GsonBuilder().setPrettyPrinting().create().toJson(autosaveObject);
            outputStream.write(jsonString.getBytes());
        } catch (IOException e) {
            logger.warn("Could not save to autosave file", e);
        }
    }
}
Also used : GsonBuilder(com.google.gson.GsonBuilder) JsonElement(com.google.gson.JsonElement) JsonObject(com.google.gson.JsonObject) IOException(java.io.IOException) BufferedOutputStream(java.io.BufferedOutputStream)

Aggregations

GsonBuilder (com.google.gson.GsonBuilder)1067 Gson (com.google.gson.Gson)803 IOException (java.io.IOException)185 Test (org.junit.Test)141 ArrayList (java.util.ArrayList)101 JsonObject (com.google.gson.JsonObject)90 File (java.io.File)80 JsonElement (com.google.gson.JsonElement)78 HashMap (java.util.HashMap)67 List (java.util.List)62 Map (java.util.Map)59 Retrofit (retrofit2.Retrofit)56 Type (java.lang.reflect.Type)52 FileNotFoundException (java.io.FileNotFoundException)42 TypeToken (com.google.gson.reflect.TypeToken)40 ResponseBody (okhttp3.ResponseBody)39 FileOutputStream (java.io.FileOutputStream)38 Call (retrofit2.Call)38 JsonSyntaxException (com.google.gson.JsonSyntaxException)37 JsonParser (com.google.gson.JsonParser)36