Search in sources :

Example 1 with Gson

use of com.google.gson.Gson 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 2 with Gson

use of com.google.gson.Gson in project buck by facebook.

the class XctoolOutputParsing method streamOutputFromReader.

/**
   * Decodes a stream of JSON objects as produced by {@code xctool -reporter json-stream}
   * and invokes the callbacks in {@code eventCallback} with each event in the stream.
   */
public static void streamOutputFromReader(Reader reader, XctoolEventCallback eventCallback) {
    Gson gson = new Gson();
    JsonStreamParser streamParser = new JsonStreamParser(reader);
    try {
        while (streamParser.hasNext()) {
            dispatchEventCallback(gson, streamParser.next(), eventCallback);
        }
    } catch (JsonParseException e) {
        LOG.warn(e, "Couldn't parse xctool JSON stream");
    }
}
Also used : Gson(com.google.gson.Gson) JsonParseException(com.google.gson.JsonParseException) JsonStreamParser(com.google.gson.JsonStreamParser)

Example 3 with Gson

use of com.google.gson.Gson in project cw-omnibus by commonsguy.

the class PresoRoster method load.

void load(Context ctxt) {
    Gson gson = new Gson();
    AssetManager assets = ctxt.getAssets();
    for (String presoDir : PRESO_ASSET_DIRS) {
        PresoContents c = loadPreso(gson, assets, presoDir);
        if (c != null) {
            c.id = presos.size();
            presos.add(c);
        }
    }
}
Also used : AssetManager(android.content.res.AssetManager) Gson(com.google.gson.Gson)

Example 4 with Gson

use of com.google.gson.Gson in project elastic-job by dangdangdotcom.

the class GsonFactoryTest method assertRegisterTypeAdapter.

@Test
public void assertRegisterTypeAdapter() {
    Gson beforeRegisterGson = GsonFactory.getGson();
    GsonFactory.registerTypeAdapter(GsonFactoryTest.class, new TypeAdapter() {

        @Override
        public Object read(final JsonReader in) throws IOException {
            return null;
        }

        @Override
        public void write(final JsonWriter out, final Object value) throws IOException {
            out.jsonValue("test");
        }
    });
    assertThat(beforeRegisterGson.toJson(new GsonFactoryTest()), is("{}"));
    assertThat(GsonFactory.getGson().toJson(new GsonFactoryTest()), is("test"));
}
Also used : TypeAdapter(com.google.gson.TypeAdapter) Gson(com.google.gson.Gson) JsonReader(com.google.gson.stream.JsonReader) IOException(java.io.IOException) JsonWriter(com.google.gson.stream.JsonWriter) Test(org.junit.Test)

Example 5 with Gson

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

the class WorkspaceServiceTest method shouldBeAbleToGetSettings.

@Test
public void shouldBeAbleToGetSettings() throws Exception {
    final Response response = given().auth().basic(ADMIN_USER_NAME, ADMIN_USER_PASSWORD).when().get(SECURE_PATH + "/workspace/settings");
    assertEquals(response.getStatusCode(), 200);
    final Map<String, String> settings = new Gson().fromJson(response.print(), new TypeToken<Map<String, String>>() {
    }.getType());
    assertEquals(settings, //
    ImmutableMap.of(//
    "che.workspace.auto_snapshot", //
    "true", //
    "che.workspace.auto_restore", //
    "false", "che.workspace.auto_start", "true"));
}
Also used : Response(com.jayway.restassured.response.Response) TypeToken(com.google.gson.reflect.TypeToken) Gson(com.google.gson.Gson) Matchers.anyString(org.mockito.Matchers.anyString) Test(org.testng.annotations.Test)

Aggregations

Gson (com.google.gson.Gson)3886 GsonBuilder (com.google.gson.GsonBuilder)814 IOException (java.io.IOException)671 Test (org.junit.Test)572 ArrayList (java.util.ArrayList)547 HashMap (java.util.HashMap)445 JsonObject (com.google.gson.JsonObject)422 InputStream (java.io.InputStream)264 List (java.util.List)258 URL (java.net.URL)241 Map (java.util.Map)231 JsonElement (com.google.gson.JsonElement)217 Type (java.lang.reflect.Type)215 HttpURLConnection (java.net.HttpURLConnection)191 File (java.io.File)188 ExecutionException (java.util.concurrent.ExecutionException)180 JsonSyntaxException (com.google.gson.JsonSyntaxException)179 TypeToken (com.google.gson.reflect.TypeToken)173 InputStreamReader (java.io.InputStreamReader)173 AsyncTask (android.os.AsyncTask)155