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;
}
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;
}
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;
}
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;
}
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);
}
}
}
Aggregations