Search in sources :

Example 1 with JsonParser

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

the class ServerDtoTest method testSerializerWithAny.

@Test
public void testSerializerWithAny() throws Exception {
    final List<Object> objects = createListTestValueForAny();
    DtoWithAny dto = dtoFactory.createDto(DtoWithAny.class).withStuff(createTestValueForAny()).withObjects(objects);
    final String json = dtoFactory.toJson(dto);
    JsonObject jsonObject = new JsonParser().parse(json).getAsJsonObject();
    Assert.assertEquals(jsonObject.get("stuff"), createTestValueForAny());
    Assert.assertTrue(jsonObject.has("objects"));
    JsonArray jsonArray = jsonObject.get("objects").getAsJsonArray();
    assertEquals(jsonArray.size(), objects.size());
    for (int i = 0; i < jsonArray.size(); i++) {
        assertEquals(jsonArray.get(i), objects.get(i));
    }
}
Also used : JsonArray(com.google.gson.JsonArray) DtoWithAny(org.eclipse.che.dto.definitions.DtoWithAny) JsonObject(com.google.gson.JsonObject) JsonObject(com.google.gson.JsonObject) JsonParser(com.google.gson.JsonParser) Test(org.testng.annotations.Test)

Example 2 with JsonParser

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

the class ServerDtoTest method testComplicatedDtoSerializer.

@Test
public void testComplicatedDtoSerializer() throws Exception {
    final String fooString = "Something";
    final int fooId = 1;
    final String _default = "test_default_keyword";
    List<String> listStrings = new ArrayList<>(2);
    listStrings.add("Something 1");
    listStrings.add("Something 2");
    ComplicatedDto.SimpleEnum simpleEnum = ComplicatedDto.SimpleEnum.ONE;
    // Assume that SimpleDto works. Use it to test nested objects
    SimpleDto simpleDto = dtoFactory.createDto(SimpleDto.class).withName(fooString).withId(fooId).withDefault(_default);
    Map<String, SimpleDto> mapDtos = new HashMap<>(1);
    mapDtos.put(fooString, simpleDto);
    List<SimpleDto> listDtos = new ArrayList<>(1);
    listDtos.add(simpleDto);
    List<List<ComplicatedDto.SimpleEnum>> listOfListOfEnum = new ArrayList<>(1);
    List<ComplicatedDto.SimpleEnum> listOfEnum = new ArrayList<>(3);
    listOfEnum.add(ComplicatedDto.SimpleEnum.ONE);
    listOfEnum.add(ComplicatedDto.SimpleEnum.TWO);
    listOfEnum.add(ComplicatedDto.SimpleEnum.THREE);
    listOfListOfEnum.add(listOfEnum);
    ComplicatedDto dto = dtoFactory.createDto(ComplicatedDto.class).withStrings(listStrings).withSimpleEnum(simpleEnum).withMap(mapDtos).withSimpleDtos(listDtos).withArrayOfArrayOfEnum(listOfListOfEnum);
    final String json = dtoFactory.toJson(dto);
    JsonObject jsonObject = new JsonParser().parse(json).getAsJsonObject();
    Assert.assertTrue(jsonObject.has("strings"));
    JsonArray jsonArray = jsonObject.get("strings").getAsJsonArray();
    assertEquals(jsonArray.get(0).getAsString(), listStrings.get(0));
    assertEquals(jsonArray.get(1).getAsString(), listStrings.get(1));
    Assert.assertTrue(jsonObject.has("simpleEnum"));
    assertEquals(jsonObject.get("simpleEnum").getAsString(), simpleEnum.name());
    Assert.assertTrue(jsonObject.has("map"));
    JsonObject jsonMap = jsonObject.get("map").getAsJsonObject();
    JsonObject value = jsonMap.get(fooString).getAsJsonObject();
    assertEquals(value.get("name").getAsString(), fooString);
    assertEquals(value.get("id").getAsInt(), fooId);
    assertEquals(value.get("default").getAsString(), _default);
    Assert.assertTrue(jsonObject.has("simpleDtos"));
    JsonArray simpleDtos = jsonObject.get("simpleDtos").getAsJsonArray();
    JsonObject simpleDtoJsonObject = simpleDtos.get(0).getAsJsonObject();
    assertEquals(simpleDtoJsonObject.get("name").getAsString(), fooString);
    assertEquals(simpleDtoJsonObject.get("id").getAsInt(), fooId);
    assertEquals(simpleDtoJsonObject.get("default").getAsString(), _default);
    Assert.assertTrue(jsonObject.has("arrayOfArrayOfEnum"));
    JsonArray arrayOfArrayOfEnum = jsonObject.get("arrayOfArrayOfEnum").getAsJsonArray().get(0).getAsJsonArray();
    assertEquals(arrayOfArrayOfEnum.get(0).getAsString(), ComplicatedDto.SimpleEnum.ONE.name());
    assertEquals(arrayOfArrayOfEnum.get(1).getAsString(), ComplicatedDto.SimpleEnum.TWO.name());
    assertEquals(arrayOfArrayOfEnum.get(2).getAsString(), ComplicatedDto.SimpleEnum.THREE.name());
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) JsonObject(com.google.gson.JsonObject) ComplicatedDto(org.eclipse.che.dto.definitions.ComplicatedDto) JsonArray(com.google.gson.JsonArray) ArrayList(java.util.ArrayList) Arrays.asList(java.util.Arrays.asList) List(java.util.List) SimpleDto(org.eclipse.che.dto.definitions.SimpleDto) JsonParser(com.google.gson.JsonParser) Test(org.testng.annotations.Test)

Example 3 with JsonParser

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

the class ServerDtoTest method testSerializerWithFieldNames.

@Test
public void testSerializerWithFieldNames() throws Exception {
    final String fooString = "Something";
    final String _default = "test_default_keyword";
    DtoWithFieldNames dto = dtoFactory.createDto(DtoWithFieldNames.class).withTheName(fooString).withTheDefault(_default);
    final String json = dtoFactory.toJson(dto);
    JsonObject jsonObject = new JsonParser().parse(json).getAsJsonObject();
    assertEquals(jsonObject.get(DtoWithFieldNames.THENAME_FIELD).getAsString(), fooString);
    assertEquals(jsonObject.get(DtoWithFieldNames.THEDEFAULT_FIELD).getAsString(), _default);
}
Also used : DtoWithFieldNames(org.eclipse.che.dto.definitions.DtoWithFieldNames) JsonObject(com.google.gson.JsonObject) JsonParser(com.google.gson.JsonParser) Test(org.testng.annotations.Test)

Example 4 with JsonParser

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

the class ServerDtoTest method testCloneWithNullAny.

@Test
public void testCloneWithNullAny() throws Exception {
    DtoWithAny dto1 = dtoFactory.createDto(DtoWithAny.class);
    DtoWithAny dto2 = dtoFactory.clone(dto1);
    Assert.assertEquals(dto1, dto2);
    JsonElement json = new JsonParser().parse(dtoFactory.toJson(dto1));
    JsonObject expJson = new JsonObject();
    expJson.addProperty("id", 0);
    expJson.add("objects", new JsonArray());
    assertEquals(expJson, json);
}
Also used : JsonArray(com.google.gson.JsonArray) DtoWithAny(org.eclipse.che.dto.definitions.DtoWithAny) JsonElement(com.google.gson.JsonElement) JsonObject(com.google.gson.JsonObject) JsonParser(com.google.gson.JsonParser) Test(org.testng.annotations.Test)

Example 5 with JsonParser

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

the class NodeJsBreakpointsParser method parse.

@Override
public Breakpoints parse(NodeJsOutput nodeJsOutput) throws NodeJsDebuggerParseException {
    final List<Breakpoint> breakpoints = new ArrayList<>();
    JsonObject json = new JsonParser().parse(nodeJsOutput.getOutput()).getAsJsonObject();
    if (json.has("breakpoints")) {
        Iterator<JsonElement> iter = json.getAsJsonArray("breakpoints").iterator();
        while (iter.hasNext()) {
            JsonObject item = iter.next().getAsJsonObject();
            try {
                final String condition = item.has("condition") && !item.get("condition").isJsonNull() ? item.get("condition").getAsString() : null;
                final boolean isEnabled = item.has("active") && !item.get("active").isJsonNull() && item.get("active").getAsBoolean();
                final int lineNumber = item.get("line").getAsInt();
                final String target;
                String targetType = item.get("type").getAsString();
                switch(targetType) {
                    case "scriptId":
                        target = String.valueOf(item.get("script_id").getAsInt());
                        break;
                    case "scriptRegExp":
                        target = item.get("script_regexp").getAsString();
                        break;
                    default:
                        throw new IllegalArgumentException("Unsupported 'type' value: " + targetType);
                }
                Location location = new LocationImpl(targetType + ":" + target, lineNumber + 1);
                Breakpoint breakpoint = new BreakpointImpl(location, isEnabled, condition);
                breakpoints.add(breakpoint);
            } catch (Exception e) {
                LOG.error("Failed to parse breakpoint: " + item.toString(), e);
            }
        }
    }
    return new Breakpoints(breakpoints);
}
Also used : Breakpoint(org.eclipse.che.api.debug.shared.model.Breakpoint) BreakpointImpl(org.eclipse.che.api.debug.shared.model.impl.BreakpointImpl) ArrayList(java.util.ArrayList) JsonObject(com.google.gson.JsonObject) Breakpoint(org.eclipse.che.api.debug.shared.model.Breakpoint) NodeJsDebuggerParseException(org.eclipse.che.plugin.nodejsdbg.server.exception.NodeJsDebuggerParseException) JsonElement(com.google.gson.JsonElement) LocationImpl(org.eclipse.che.api.debug.shared.model.impl.LocationImpl) JsonParser(com.google.gson.JsonParser) Location(org.eclipse.che.api.debug.shared.model.Location)

Aggregations

JsonParser (com.google.gson.JsonParser)323 JsonObject (com.google.gson.JsonObject)263 JsonElement (com.google.gson.JsonElement)88 JsonArray (com.google.gson.JsonArray)49 IOException (java.io.IOException)39 Gson (com.google.gson.Gson)31 InputStreamReader (java.io.InputStreamReader)24 HashMap (java.util.HashMap)20 Map (java.util.Map)18 ArrayList (java.util.ArrayList)16 JsonReader (com.google.gson.stream.JsonReader)11 Test (org.junit.Test)11 AssetManager (android.content.res.AssetManager)10 InputStream (java.io.InputStream)10 Type (java.lang.reflect.Type)10 URL (java.net.URL)9 Test (org.testng.annotations.Test)9 UserType (com.glitchcog.fontificator.bot.UserType)8 EmojiType (com.glitchcog.fontificator.emoji.EmojiType)8 JsonParseException (com.google.gson.JsonParseException)8