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.external.cmake.server.ServerProtocolV1.java

License:Apache License

@NonNull
@Override/* w  w  w  .  j a v a 2 s.  c  o  m*/
public HandshakeResult handshake(@NonNull HandshakeRequest handshakeRequest) throws IOException {
    if (!connected) {
        throw new RuntimeException("Not connected to Cmake server.");
    }
    writeMessage(new GsonBuilder().setPrettyPrinting().create().toJson(handshakeRequest));
    return decodeResponse(HandshakeResult.class);
}

From source file:com.android.build.gradle.external.cmake.server.ServerProtocolV1.java

License:Apache License

@NonNull
@Override//from   w ww. j  av a 2 s  . com
public ConfigureCommandResult configure(@NonNull String... cacheArguments) throws IOException {
    if (!connected) {
        throw new RuntimeException("Not connected to Cmake server.");
    }

    ConfigureRequest configureRequest = new ConfigureRequest();

    // Insert a blank element to work around a bug in Cmake 3.7.1 where the first element is
    // ignored.
    configureRequest.cacheArguments = new String[cacheArguments.length + 1];
    configureRequest.cacheArguments[0] = "";
    System.arraycopy(cacheArguments, 0, configureRequest.cacheArguments, 1, cacheArguments.length);

    writeMessage(new GsonBuilder().setPrettyPrinting().create().toJson(configureRequest));
    configureMessages = new ArrayList<>();
    ConfigureResult configureResult = decodeResponse(ConfigureResult.class, configureMessages);
    configured = ServerUtils.isConfigureResultValid(configureResult);

    return new ConfigureCommandResult(configureResult,
            !configureMessages.isEmpty() ? getInteractiveMessagesAsString(configureMessages) : "");
}

From source file:com.android.build.gradle.external.cmake.server.ServerProtocolV1.java

License:Apache License

@NonNull
@Override//from  w w  w .ja v a  2s.  c o m
public CacheResult cache() throws IOException {
    if (!connected) {
        throw new RuntimeException("Not connected to Cmake server.");
    }

    CacheRequest request = new CacheRequest();
    writeMessage(new GsonBuilder().setPrettyPrinting().create().toJson(request));
    return decodeResponse(CacheResult.class);
}

From source file:com.android.build.gradle.external.cmake.server.ServerProtocolV1.java

License:Apache License

private <T> T decodeResponse(Class<T> clazz, List<InteractiveMessage> interactiveMessages) throws IOException {
    Gson gson = new GsonBuilder().create();
    String message = readMessage();
    String messageType = gson.fromJson(message, TypeOfMessage.class).type;

    final List supportedTypes = Arrays.asList("message", "progress", "signal");
    // Process supported interactive messages.
    // For a given command, the CMake server would respond with message types
    // 0 or more of (message | progress | signal)
    // and finally terminates with a message with message types
    // (hello | reply | error)
    // More info:
    // https://cmake.org/cmake/help/v3.7/manual/cmake-server.7.html#general-message-layout
    while (supportedTypes.contains(messageType)) {
        switch (messageType) {
        case "message":
            if (serverReceiver.getMessageReceiver() != null) {
                InteractiveMessage interactiveMessage = gson.fromJson(message, InteractiveMessage.class);
                serverReceiver.getMessageReceiver().receive(interactiveMessage);
                // Record the interactive messages only if need be.
                if (interactiveMessages != null) {
                    serverReceiver.getMessageReceiver().receive(interactiveMessage);
                    interactiveMessages.add(interactiveMessage);
                }//w  w w.  ja  v  a  2  s .co  m
            }
            break;
        case "progress":
            if (serverReceiver.getProgressReceiver() != null) {
                serverReceiver.getProgressReceiver().receive(gson.fromJson(message, InteractiveProgress.class));
                break;
            }
            break;
        case "signal":
            if (serverReceiver.getProgressReceiver() != null) {
                serverReceiver.getProgressReceiver().receive(gson.fromJson(message, InteractiveProgress.class));
                break;
            }
        }
        message = readMessage();
        messageType = gson.fromJson(message, TypeOfMessage.class).type;
    }

    // Process the final message.
    switch (messageType) {
    case "hello":
    case "reply":
        if (serverReceiver.getDeserializationMonitor() != null) {
            serverReceiver.getDeserializationMonitor().receive(message, clazz);
        }
        return gson.fromJson(message, clazz);
    case "error":
        if (serverReceiver.getMessageReceiver() != null) {
            InteractiveMessage interactiveMessage = gson.fromJson(message, InteractiveMessage.class);
            serverReceiver.getMessageReceiver().receive(interactiveMessage);
        }
        return null;
    default:
        throw new RuntimeException("Unsupported message type " + messageType + " received from CMake server.");
    }
}

From source file:com.android.build.gradle.external.cmake.server.ServerUtils.java

License:Apache License

/**
 * Parses the compile commands json to create the compilation database given the compile
 * commands json file./* w w w  . ja v a2s  . co m*/
 *
 * @param compileCommandsFile - json file with compile commands info generated by Cmake
 * @return list of compilation database present in the json file
 * @throws IOException I/O failure
 */
@NonNull
public static List<CompileCommand> getCompilationDatabase(@NonNull File compileCommandsFile)
        throws IOException, JsonSyntaxException {
    final String text = new String(Files.readAllBytes(compileCommandsFile.toPath()), StandardCharsets.UTF_8);
    Gson gson = new GsonBuilder().create();

    return Arrays.asList(gson.fromJson(text, CompileCommand[].class));
}

From source file:com.android.build.gradle.internal.ExtraModelInfo.java

License:Apache License

public ExtraModelInfo(@NonNull Project project, boolean isLibrary) {
    super(computeModelQueryMode(project));
    this.project = project;
    this.isLibrary = isLibrary;
    errorFormatMode = computeErrorFormatMode(project);
    if (errorFormatMode == ErrorFormatMode.MACHINE_PARSABLE) {
        GsonBuilder gsonBuilder = new GsonBuilder();
        MessageJsonSerializer.registerTypeAdapters(gsonBuilder);
        mGson = gsonBuilder.create();//from www.  ja v a2s  . co  m
    } else {
        mGson = null;
    }
}

From source file:com.android.build.gradle.internal.pipeline.SubStream.java

License:Apache License

public static Collection<SubStream> loadSubStreams(@NonNull File rootFolder) {
    final File jsonFile = new File(rootFolder, FN_FOLDER_CONTENT);
    if (!jsonFile.isFile()) {
        return ImmutableList.of();
    }//from  www .  j  a v a  2s  .c  o  m

    try (FileReader reader = new FileReader(jsonFile)) {

        GsonBuilder gsonBuilder = new GsonBuilder();
        gsonBuilder.registerTypeAdapter(SubStream.class, new SubStreamAdapter());
        Gson gson = gsonBuilder.create();

        Type recordType = new TypeToken<List<SubStream>>() {
        }.getType();
        return gson.fromJson(reader, recordType);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

From source file:com.android.build.gradle.internal.pipeline.SubStream.java

License:Apache License

public static void save(@NonNull Collection<SubStream> subStreams, @NonNull File rootFolder)
        throws IOException {
    GsonBuilder gsonBuilder = new GsonBuilder();
    gsonBuilder.registerTypeAdapter(SubStream.class, new SubStreamAdapter());
    Gson gson = gsonBuilder.create();//from   w  ww .j  av a2s.co m

    // just in case
    FileUtils.mkdirs(rootFolder);
    Files.write(gson.toJson(subStreams), new File(rootFolder, FN_FOLDER_CONTENT), Charsets.UTF_8);
}

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

License:Apache License

/**
 * Persists the passed output types and split output to a {@link String} using gson.
 *
 * @param projectPath path to relativize output file paths against.
 * @param outputTypes the output types to persist.
 * @param splitOutputs the outputs organized per output type
 * @return a json String.//w  ww  .j  a  va 2s  .c  o  m
 */
public static String persist(Path projectPath, ImmutableList<VariantScope.OutputType> outputTypes,
        SetMultimap<VariantScope.OutputType, BuildOutput> splitOutputs) {
    GsonBuilder gsonBuilder = new GsonBuilder();
    gsonBuilder.registerTypeAdapter(ApkInfo.class, new BuildOutputs.ApkInfoAdapter());
    gsonBuilder.registerTypeAdapter(VariantScope.TaskOutputType.class,
            new BuildOutputs.OutputTypeTypeAdapter());
    gsonBuilder.registerTypeAdapter(VariantScope.AnchorOutputType.class,
            new BuildOutputs.OutputTypeTypeAdapter());
    Gson gson = gsonBuilder.create();
    // flatten and relativize the file paths to be persisted.
    List<BuildOutput> buildOutputs = outputTypes.stream().map(splitOutputs::get).flatMap(Collection::stream)
            .map(buildOutput -> new BuildOutput(buildOutput.getType(), buildOutput.getApkInfo(),
                    projectPath.relativize(buildOutput.getOutputPath()), buildOutput.getProperties()))
            .collect(Collectors.toList());
    return gson.toJson(buildOutputs);
}

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

License:Apache License

@NonNull
private static Collection<BuildOutput> load(@NonNull Path projectPath, @NonNull Reader reader) {
    GsonBuilder gsonBuilder = new GsonBuilder();

    gsonBuilder.registerTypeAdapter(ApkInfo.class, new ApkInfoAdapter());
    gsonBuilder.registerTypeAdapter(VariantScope.OutputType.class, new OutputTypeTypeAdapter());
    Gson gson = gsonBuilder.create();/*from www . j  a  v a2s  . co  m*/
    Type recordType = new TypeToken<List<BuildOutput>>() {
    }.getType();
    Collection<BuildOutput> buildOutputs = gson.fromJson(reader, recordType);
    // resolve the file path to the current project location.
    return buildOutputs.stream()
            .map(buildOutput -> new BuildOutput(buildOutput.getType(), buildOutput.getApkInfo(),
                    projectPath.resolve(buildOutput.getOutputPath()), buildOutput.getProperties()))
            .collect(Collectors.toList());
}