Search in sources :

Example 11 with GsonBuilder

use of com.google.gson.GsonBuilder in project che by eclipse.

the class StackLoaderTest method dtoShouldBeSerialized.

@Test
public void dtoShouldBeSerialized() {
    StackDto stackDtoDescriptor = newDto(StackDto.class).withName("nameWorkspaceConfig");
    StackComponentDto stackComponentDto = newDto(StackComponentDto.class).withName("java").withVersion("1.8");
    stackDtoDescriptor.setComponents(Collections.singletonList(stackComponentDto));
    stackDtoDescriptor.setTags(Arrays.asList("some teg1", "some teg2"));
    stackDtoDescriptor.setDescription("description");
    stackDtoDescriptor.setId("someId");
    stackDtoDescriptor.setScope("scope");
    stackDtoDescriptor.setCreator("Created in Codenvy");
    Map<String, String> attributes = new HashMap<>();
    attributes.put("attribute1", "valute attribute1");
    Link link = newDto(Link.class).withHref("some url").withMethod("get").withRel("someRel").withConsumes("consumes").withProduces("produces");
    HashMap<String, List<String>> projectMap = new HashMap<>();
    projectMap.put("test", Arrays.asList("test", "test2"));
    ProjectProblemDto projectProblem = newDto(ProjectProblemDto.class).withCode(100).withMessage("message");
    SourceStorageDto sourceStorageDto = newDto(SourceStorageDto.class).withType("some type").withParameters(attributes).withLocation("location");
    ProjectConfigDto projectConfigDto = newDto(ProjectConfigDto.class).withName("project").withPath("somePath").withAttributes(projectMap).withType("maven type").withDescription("some project description").withLinks(Collections.singletonList(link)).withMixins(Collections.singletonList("mixin time")).withProblems(Collections.singletonList(projectProblem)).withSource(sourceStorageDto);
    EnvironmentRecipeDto environmentRecipe = newDto(EnvironmentRecipeDto.class).withContent("some content").withContentType("some content type").withType("someType");
    Map<String, ServerConf2Dto> servers = new HashMap<>();
    servers.put("server1Ref", newDto(ServerConf2Dto.class).withPort("8080/tcp").withProtocol("http").withProperties(singletonMap("key", "value")));
    Map<String, ExtendedMachineDto> machines = new HashMap<>();
    machines.put("someMachineName", newDto(ExtendedMachineDto.class).withAgents(Arrays.asList("agent1", "agent2")).withServers(servers).withAttributes(singletonMap("memoryLimitBytes", "" + 512L * 1024L * 1024L)));
    EnvironmentDto environmentDto = newDto(EnvironmentDto.class).withRecipe(environmentRecipe).withMachines(machines);
    CommandDto commandDto = newDto(CommandDto.class).withType("command type").withName("command name").withCommandLine("command line");
    WorkspaceConfigDto workspaceConfigDto = newDto(WorkspaceConfigDto.class).withName("SomeWorkspaceConfig").withDescription("some workspace").withLinks(Collections.singletonList(link)).withDefaultEnv("some Default Env name").withProjects(Collections.singletonList(projectConfigDto)).withEnvironments(singletonMap("name", environmentDto)).withCommands(Collections.singletonList(commandDto));
    stackDtoDescriptor.setWorkspaceConfig(workspaceConfigDto);
    Gson GSON = new GsonBuilder().create();
    GSON.fromJson(stackDtoDescriptor.toString(), StackImpl.class);
}
Also used : HashMap(java.util.HashMap) GsonBuilder(com.google.gson.GsonBuilder) EnvironmentDto(org.eclipse.che.api.workspace.shared.dto.EnvironmentDto) ProjectConfigDto(org.eclipse.che.api.workspace.shared.dto.ProjectConfigDto) StackDto(org.eclipse.che.api.workspace.shared.dto.stack.StackDto) StackComponentDto(org.eclipse.che.api.workspace.shared.dto.stack.StackComponentDto) Gson(com.google.gson.Gson) ServerConf2Dto(org.eclipse.che.api.workspace.shared.dto.ServerConf2Dto) SourceStorageDto(org.eclipse.che.api.workspace.shared.dto.SourceStorageDto) List(java.util.List) CommandDto(org.eclipse.che.api.machine.shared.dto.CommandDto) WorkspaceConfigDto(org.eclipse.che.api.workspace.shared.dto.WorkspaceConfigDto) Link(org.eclipse.che.api.core.rest.shared.dto.Link) ProjectProblemDto(org.eclipse.che.api.workspace.shared.dto.ProjectProblemDto) EnvironmentRecipeDto(org.eclipse.che.api.workspace.shared.dto.EnvironmentRecipeDto) ExtendedMachineDto(org.eclipse.che.api.workspace.shared.dto.ExtendedMachineDto) Test(org.testng.annotations.Test)

Example 12 with GsonBuilder

use of com.google.gson.GsonBuilder in project MyDiary by erttyy8821.

the class ExportAsyncTask method outputBackupJson.

private void outputBackupJson() throws IOException {
    Writer writer = new FileWriter(backupJsonFilePath);
    Gson gson = new GsonBuilder().create();
    gson.toJson(backupManager, writer);
    writer.close();
}
Also used : GsonBuilder(com.google.gson.GsonBuilder) FileWriter(java.io.FileWriter) Gson(com.google.gson.Gson) FileWriter(java.io.FileWriter) Writer(java.io.Writer)

Example 13 with GsonBuilder

use of com.google.gson.GsonBuilder in project MVCHelper by LuckyJayce.

the class ABSTestCaseFragment method onCreateView.

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    this.inflater = inflater;
    View view = inflater.inflate(R.layout.testcase, container, false);
    taskHelper = new TaskHelper<>();
    GsonBuilder builder = new GsonBuilder();
    // 格式化输出
    builder.setPrettyPrinting();
    // builder.serializeNulls();
    builder.addSerializationExclusionStrategy(new ExclusionStrategy() {

        @Override
        public boolean shouldSkipField(FieldAttributes f) {
            // true; //按注解排除
            return false;
        }

        @Override
        public boolean shouldSkipClass(Class<?> clazz) {
            // 直接排除某个类 ,return true为排除
            return clazz == Gson.class || clazz == Bitmap.class;
        }
    }).create();
    gson = builder.create();
    recyclerView = (RecyclerView) view.findViewById(R.id.testcase2_recyclerView);
    paramsRecyclerView = (LinearLayout) view.findViewById(R.id.testcase2_params_recyclerView);
    resultTextView = (TextView) view.findViewById(R.id.testcase2_result_textView);
    runButton = (Button) view.findViewById(R.id.testcase2_run_button);
    resetButton = (Button) view.findViewById(R.id.testcase2_reset_button);
    itemRunButton = view.findViewById(R.id.testcase2_run2_button);
    resultStateTextView = (TextView) view.findViewById(R.id.testcase2_resultState_textView);
    recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
    recyclerView.setAdapter(tasksAdapter = new TasksAdapter());
    // recyclerView.addItemDecoration(new DividerItemDecoration(getContext()));
    //
    // // paramsRecyclerView.setLayoutManager(new
    // // LinearLayoutManager(getContext()));
    // // paramsRecyclerView.addItemDecoration(new
    // // DividerItemDecoration(getContext()));
    // // paramsRecyclerView.setAdapter(paramsAdapter = new ParamsAdapter());
    datas = getTestCaseDatas();
    resetButton.setOnClickListener(onClickListener);
    runButton.setOnClickListener(onClickListener);
    itemRunButton.setOnClickListener(onClickListener);
    tasksAdapter.setOnItemClickListener(onItemClickListener);
    resultTextView.setOnClickListener(onClickListener);
    updateRight();
    return view;
}
Also used : Bitmap(android.graphics.Bitmap) GsonBuilder(com.google.gson.GsonBuilder) Gson(com.google.gson.Gson) ExclusionStrategy(com.google.gson.ExclusionStrategy) LinearLayoutManager(android.support.v7.widget.LinearLayoutManager) View(android.view.View) TextView(android.widget.TextView) RecyclerView(android.support.v7.widget.RecyclerView) FieldAttributes(com.google.gson.FieldAttributes)

Example 14 with GsonBuilder

use of com.google.gson.GsonBuilder in project MinecraftForge by MinecraftForge.

the class MetadataCollection method from.

public static MetadataCollection from(@Nullable InputStream inputStream, String sourceName) {
    if (inputStream == null) {
        return new MetadataCollection();
    }
    InputStreamReader reader = new InputStreamReader(inputStream);
    try {
        MetadataCollection collection;
        Gson gson = new GsonBuilder().registerTypeAdapter(ArtifactVersion.class, new ArtifactVersionAdapter()).create();
        JsonParser parser = new JsonParser();
        JsonElement rootElement = parser.parse(reader);
        if (rootElement.isJsonArray()) {
            collection = new MetadataCollection();
            JsonArray jsonList = rootElement.getAsJsonArray();
            collection.modList = new ModMetadata[jsonList.size()];
            int i = 0;
            for (JsonElement mod : jsonList) {
                collection.modList[i++] = gson.fromJson(mod, ModMetadata.class);
            }
        } else {
            collection = gson.fromJson(rootElement, MetadataCollection.class);
        }
        collection.parseModMetadataList();
        return collection;
    } catch (JsonParseException e) {
        FMLLog.log(Level.ERROR, e, "The mcmod.info file in %s cannot be parsed as valid JSON. It will be ignored", sourceName);
        return new MetadataCollection();
    } catch (Exception e) {
        throw Throwables.propagate(e);
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) GsonBuilder(com.google.gson.GsonBuilder) Gson(com.google.gson.Gson) JsonParseException(com.google.gson.JsonParseException) JsonParseException(com.google.gson.JsonParseException) IOException(java.io.IOException) JsonArray(com.google.gson.JsonArray) ArtifactVersion(net.minecraftforge.fml.common.versioning.ArtifactVersion) JsonElement(com.google.gson.JsonElement) JsonParser(com.google.gson.JsonParser)

Example 15 with GsonBuilder

use of com.google.gson.GsonBuilder in project immutables by immutables.

the class JaxrsTest method propagateGsonAttributes.

@Test
public void propagateGsonAttributes() {
    Gson gson = new GsonBuilder().serializeNulls().disableHtmlEscaping().setPrettyPrinting().create();
    GsonOptions options = new GsonOptions(gson, true);
    JsonReader reader = new JsonReader(new StringReader(""));
    options.setReaderOptions(reader);
    check(reader.isLenient());
    JsonWriter writer = new JsonWriter(new StringWriter());
    options.setWriterOptions(writer);
    check(writer.isLenient());
    check(!writer.isHtmlSafe());
    check(writer.getSerializeNulls());
    // checks pretty printing
    check(gson.toJson(Collections.singletonMap("k", "v"))).is("{\n  \"k\": \"v\"\n}");
}
Also used : GsonOptions(org.immutables.gson.stream.GsonMessageBodyProvider.GsonOptions) StringWriter(java.io.StringWriter) GsonBuilder(com.google.gson.GsonBuilder) StringReader(java.io.StringReader) Gson(com.google.gson.Gson) JsonReader(com.google.gson.stream.JsonReader) JsonWriter(com.google.gson.stream.JsonWriter) Test(org.junit.Test)

Aggregations

GsonBuilder (com.google.gson.GsonBuilder)222 Gson (com.google.gson.Gson)135 IOException (java.io.IOException)40 Type (java.lang.reflect.Type)24 ArrayList (java.util.ArrayList)24 Map (java.util.Map)24 JsonElement (com.google.gson.JsonElement)20 HashMap (java.util.HashMap)20 Test (org.junit.Test)18 JsonObject (com.google.gson.JsonObject)15 JsonSyntaxException (com.google.gson.JsonSyntaxException)13 Date (java.util.Date)13 List (java.util.List)13 Retrofit (retrofit2.Retrofit)13 InputStream (java.io.InputStream)11 OkHttpClient (okhttp3.OkHttpClient)11 File (java.io.File)9 InputStreamReader (java.io.InputStreamReader)9 JsonParseException (com.google.gson.JsonParseException)8 URL (java.net.URL)8