Skip to content

Homework 08: json writter#15

Merged
vsineln merged 3 commits into
masterfrom
hw08_json_writer
Feb 21, 2020
Merged

Homework 08: json writter#15
vsineln merged 3 commits into
masterfrom
hw08_json_writer

Conversation

@vsineln

@vsineln vsineln commented Feb 13, 2020

Copy link
Copy Markdown
Owner

No description provided.

@vsineln
vsineln force-pushed the hw08_json_writer branch 2 times, most recently from cc748cb to e100e6b Compare February 14, 2020 14:00
Comment on lines +64 to +67
private boolean checkSimple(Class cl) {
return cl.isPrimitive() || cl == Number.class || cl == String.class || cl == Integer.class || cl == Long.class || cl == Double.class || cl == Float.class
|| cl == Boolean.class || cl == Byte.class || cl == Character.class || cl == Short.class;
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Еще можно их в Set запихать и потом просто проверять вхождение

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Убрала классы в Set, а сам метод в JsonUtil


private List<Object> getArrayValues(Field field, Object fieldInst) {
List<Object> list = new LinkedList<>();
if (checkSimple(field.getType().getComponentType())) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

По идее нам нет дела что за объект в элементе массива. Получили Object и отдали его на съедение методу сериализации, добавили результат. Тоже самое с остальными контейнерами

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Перенесла получение элементов коллекций и массивов в классы соответствующих нод

class JsonWriterTest {
private Gson gson = new Gson();
private JsonWriter jsonWriter = new JsonWriter();

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

А давайте проверим себя еще на вот на таком тесте

    @Test
    void customTest(){
        assertEquals(gson.toJson(null), jsonWriter.toJson(null));
        assertEquals(gson.toJson(true), jsonWriter.toJson(true));
        assertEquals(gson.toJson(false), jsonWriter.toJson(false));
        assertEquals(gson.toJson((byte)1), jsonWriter.toJson((byte)1));
        assertEquals(gson.toJson((short)2f), jsonWriter.toJson((short)2f));
        assertEquals(gson.toJson(3), jsonWriter.toJson(3));
        assertEquals(gson.toJson(4L), jsonWriter.toJson(4L));
        assertEquals(gson.toJson(5f), jsonWriter.toJson(5f));
        assertEquals(gson.toJson(6d), jsonWriter.toJson(6d));
        assertEquals(gson.toJson("aaa"), jsonWriter.toJson("aaa"));
        assertEquals(gson.toJson('b'), jsonWriter.toJson('b'));
        assertEquals(gson.toJson(new int[] {1, 2, 3}), jsonWriter.toJson(new int[] {1, 2, 3}));
        assertEquals(gson.toJson(List.of(4, 5 ,6)), jsonWriter.toJson(List.of(4, 5 ,6)));
        assertEquals(gson.toJson(Collections.singletonList(7)), jsonWriter.toJson(Collections.singletonList(7)));
    }

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Спасибо! Внесла изменения, чтобы этот тест проходил

Comment on lines +26 to +34
if (cl == null || checkSimple(cl)) {
while (iterator.hasNext()) {
addElement(list, iterator.next());
}
} else {
while (iterator.hasNext()) {
list.add(new JsonWriter().parseObject(iterator.next(), new Node()));
}
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Думаю вот эти штуки можно сократить. Еще раз) Нам все равно что лежит в элементе массива/коллекции/значении мапы. Это для нас должен преобразовывать new JsonWriter().parseObject. Таких мест несколько)

@vsineln
vsineln merged commit f69c969 into master Feb 21, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants