Example usage for com.google.gson GsonBuilder GsonBuilder

List of usage examples for com.google.gson GsonBuilder GsonBuilder

Introduction

In this page you can find the example usage for com.google.gson GsonBuilder GsonBuilder.

Prototype

public GsonBuilder() 

Source Link

Document

Creates a GsonBuilder instance that can be used to build Gson with various configuration settings.

Usage

From source file:com.android.build.gradle.internal.scope.InstantAppOutputScope.java

License:Apache License

public void save(@NonNull File outputDirectory) throws IOException {
    File outputFile = new File(outputDirectory, PERSISTED_FILE_NAME);
    GsonBuilder gsonBuilder = new GsonBuilder();
    Gson gson = gsonBuilder.create();//from  w w  w. j a va2 s  .c o  m
    FileUtils.write(outputFile, gson.toJson(this));
}

From source file:com.android.build.gradle.internal.scope.InstantAppOutputScope.java

License:Apache License

@Nullable
public static InstantAppOutputScope load(@NonNull File directory) {
    File input = new File(directory, PERSISTED_FILE_NAME);

    if (!input.exists()) {
        return null;
    }/*from   w  w w.j  ava 2  s .  c  o  m*/

    GsonBuilder gsonBuilder = new GsonBuilder();
    Gson gson = gsonBuilder.create();

    try {
        return gson.fromJson(new FileReader(input), InstantAppOutputScope.class);
    } catch (FileNotFoundException e) {
        return null; // This should never happen.
    }
}

From source file:com.android.build.gradle.internal.tasks.ApplicationId.java

License:Apache License

@NonNull
public static ApplicationId load(@NonNull File input) throws FileNotFoundException {
    if (!input.getName().equals(PERSISTED_FILE_NAME)) {
        throw new FileNotFoundException("No application declaration present.");
    }/*from  w  w w. jav a 2s  .co  m*/
    GsonBuilder gsonBuilder = new GsonBuilder();
    Gson gson = gsonBuilder.create();
    return gson.fromJson(new FileReader(input), ApplicationId.class);
}

From source file:com.android.build.gradle.internal.tasks.featuresplit.FeatureSplitDeclaration.java

License:Apache License

@NonNull
public static FeatureSplitDeclaration load(@NonNull File input) throws IOException {
    GsonBuilder gsonBuilder = new GsonBuilder();
    Gson gson = gsonBuilder.create();/*from  w ww  . j av  a2  s.c o  m*/
    try (FileReader fileReader = new FileReader(input)) {
        return gson.fromJson(fileReader, FeatureSplitDeclaration.class);
    }
}

From source file:com.android.build.gradle.internal.tasks.featuresplit.FeatureSplitPackageIds.java

License:Apache License

public void save(@NonNull File outputDirectory) throws IOException {
    GsonBuilder gsonBuilder = new GsonBuilder();
    Gson gson = gsonBuilder.create();/*from www. ja v  a 2s  . co m*/
    File outputFile = getOutputFile(outputDirectory);
    Files.write(gson.toJson(featureSplits), outputFile, Charsets.UTF_8);
}

From source file:com.android.build.gradle.internal.tasks.featuresplit.FeatureSplitPackageIds.java

License:Apache License

@NonNull
public static FeatureSplitPackageIds load(@NonNull File input) throws IOException {
    GsonBuilder gsonBuilder = new GsonBuilder();
    Gson gson = gsonBuilder.create();//from ww w.  j  a v a  2  s .c  om
    Type typeToken = new TypeToken<HashSet<SplitPackageId>>() {
    }.getType();
    try (FileReader fileReader = new FileReader(input)) {
        Set<SplitPackageId> featureIds = gson.fromJson(fileReader, typeToken);
        return new FeatureSplitPackageIds(featureIds);
    }
}

From source file:com.android.build.gradle.tasks.ExternalNativeBuildTaskUtils.java

License:Apache License

/**
 * Deserialize a JSON file into NativeBuildConfigValue. Emit task-specific exception if there is
 * an issue./*from w w  w .j  a va  2 s .  c o m*/
 */
@NonNull
static NativeBuildConfigValue getNativeBuildConfigValue(@NonNull File json, @NonNull String groupName)
        throws IOException {
    checkArgument(!Strings.isNullOrEmpty(groupName), "group name missing in", json);

    Gson gson = new GsonBuilder().registerTypeAdapter(File.class, new PlainFileGsonTypeAdaptor()).create();
    List<String> lines = Files.readLines(json, Charsets.UTF_8);
    NativeBuildConfigValue config = gson.fromJson(Joiner.on("\n").join(lines), NativeBuildConfigValue.class);
    if (config.libraries == null) {
        return config;
    }
    for (NativeLibraryValue library : config.libraries.values()) {
        library.groupName = groupName;
    }
    return config;
}

From source file:com.android.build.gradle.tasks.ExternalNativeBuildTaskUtils.java

License:Apache License

/**
 * Writes the given object as JSON to the given json file.
 *
 * @throws IOException I/O failure//from  ww  w  .j av  a 2  s.  com
 */
public static void writeNativeBuildConfigValueToJsonFile(@NonNull File outputJson,
        @NonNull NativeBuildConfigValue nativeBuildConfigValue) throws IOException {
    Gson gson = new GsonBuilder().registerTypeAdapter(File.class, new PlainFileGsonTypeAdaptor())
            .disableHtmlEscaping().setPrettyPrinting().create();

    FileWriter jsonWriter = new FileWriter(outputJson);
    gson.toJson(nativeBuildConfigValue, jsonWriter);
    jsonWriter.close();
}

From source file:com.android.build.gradle.tasks.factory.AndroidJavaCompile.java

License:Apache License

/** Read the processorListFile to add annotation processors used to analytics. */
@VisibleForTesting/*from  w  w  w  . ja va 2s  .  c  o m*/
void processAnalytics() {
    Gson gson = new GsonBuilder().create();
    List<String> classNames;
    try (FileReader reader = new FileReader(processorListFile.getSingleFile())) {
        classNames = gson.fromJson(reader, new TypeToken<List<String>>() {
        }.getType());
    } catch (IOException e) {
        throw new UncheckedIOException(e);
    }

    String projectPath = getProject().getPath();
    GradleBuildVariant.Builder variant = ProcessProfileWriter.getOrCreateVariant(projectPath, variantName);
    for (String processorName : classNames) {
        AnnotationProcessorInfo.Builder builder = AnnotationProcessorInfo.newBuilder();
        builder.setSpec(processorName);
        variant.addAnnotationProcessors(builder);
    }
}

From source file:com.android.build.gradle.tasks.JavaPreCompileTask.java

License:Apache License

@TaskAction
public void preCompile() throws IOException {
    boolean grandfathered = annotationProcessorOptions.getIncludeCompileClasspath() != null;
    Collection<ResolvedArtifactResult> compileProcessors = null;
    if (!grandfathered) {
        compileProcessors = collectAnnotationProcessors(compileClasspaths);
        FileCollection annotationProcessors = annotationProcessorConfiguration.getArtifactFiles();
        compileProcessors = compileProcessors.stream()
                .filter(artifact -> !annotationProcessors.contains(artifact.getFile()))
                .collect(Collectors.toList());
        if (!compileProcessors.isEmpty()) {
            String message = "Annotation processors must be explicitly declared now.  The following "
                    + "dependencies on the compile classpath are found to contain "
                    + "annotation processor.  Please add them to the " + annotationProcessorConfigurationName
                    + " configuration.\n  - "
                    + Joiner.on("\n  - ").join(convertArtifactsToNames(compileProcessors))
                    + "\nAlternatively, set "
                    + "android.defaultConfig.javaCompileOptions.annotationProcessorOptions.includeCompileClasspath = true "
                    + "to continue with previous behavior.  Note that this option "
                    + "is deprecated and will be removed in the future.\n" + "See "
                    + "https://developer.android.com/r/tools/annotation-processor-error-message.html "
                    + "for more details.";
            if (isForTesting) {
                getLogger().warn(message);
            } else {
                throw new RuntimeException(message);
            }/*from w w  w.  j  a v a2  s  . c  om*/
        }
    }

    // Get all the annotation processors for metrics collection.
    Set<String> classNames = Sets.newHashSet();

    // Add the annotation processors on classpath only when includeCompileClasspath is true.
    if (Boolean.TRUE.equals(annotationProcessorOptions.getIncludeCompileClasspath())) {
        if (compileProcessors == null) {
            compileProcessors = collectAnnotationProcessors(compileClasspaths);
        }
        classNames.addAll(convertArtifactsToNames(compileProcessors));
    }

    // Add all annotation processors on the annotation processor configuration.
    classNames.addAll(convertArtifactsToNames(collectAnnotationProcessors(annotationProcessorConfiguration)));

    // Add the explicitly declared processors.
    // For metrics purposes, we don't care how they include the processor in their build.
    classNames.addAll(annotationProcessorOptions.getClassNames());

    // Add a generic reference to data binding, if present.
    if (dataBindingEnabled) {
        classNames.add(DATA_BINDING_SPEC);
    }

    FileUtils.deleteIfExists(processorListFile);
    Gson gson = new GsonBuilder().create();
    try (FileWriter writer = new FileWriter(processorListFile)) {
        gson.toJson(classNames, writer);
    }
}