Skip to content

Commit

Permalink
Merge pull request #281 from art-community/fix/json-parsing
Browse files Browse the repository at this point in the history
fix json parsing
  • Loading branch information
mizantrop2397 authored May 12, 2020
2 parents c425233 + a840564 commit d6f130e
Show file tree
Hide file tree
Showing 2 changed files with 244 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import ru.art.entity.*;
import ru.art.json.exception.*;
import static com.fasterxml.jackson.core.JsonToken.*;
import static java.util.Collections.*;
import static java.util.Objects.*;
import static ru.art.core.checker.CheckerForEmptiness.isEmpty;
import static ru.art.core.context.Context.*;
Expand Down Expand Up @@ -107,7 +108,6 @@ private static Entity parseJsonEntity(JsonParser parser) throws IOException {
currentName = emptyIfNull(currentName);
switch (currentToken) {
case NOT_AVAILABLE:
case END_ARRAY:
case END_OBJECT:
return entityBuilder.build();
case START_OBJECT:
Expand Down Expand Up @@ -143,9 +143,11 @@ private static void parseArray(EntityBuilder entityBuilder, JsonParser parser) t
String currentName = emptyIfNull(parser.getCurrentName());
JsonToken currentToken = parser.nextToken();
switch (currentToken) {
case END_ARRAY:
entityBuilder.valueCollectionField(currentName, emptyList());
return;
case NOT_AVAILABLE:
case END_OBJECT:
case END_ARRAY:
case FIELD_NAME:
case VALUE_EMBEDDED_OBJECT:
case VALUE_NULL:
Expand Down Expand Up @@ -272,8 +274,14 @@ private Collection<CollectionValue<Entity>> parseArraysArray(JsonParser parser)
List<CollectionValue<Entity>> array = dynamicArrayOf();
JsonToken currentToken = parser.currentToken();
do {
if (currentToken != START_ARRAY) array.add(entityCollection(parseEntityArray(parser)));
Collection<Entity> entities = dynamicArrayOf();
if (currentToken != START_ARRAY) {
entities = parseEntityArray(parser);
}
currentToken = parser.nextToken();
if (currentToken == END_ARRAY) {
array.add(entityCollection(entities));
}
} while (!parser.isClosed() && currentToken != END_ARRAY);
return array;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,233 @@
package ru.art.test.specification.json


import spock.lang.Specification

import static ru.art.core.constants.StringConstants.*
import static ru.art.json.descriptor.JsonEntityReader.readJson
import static ru.art.json.descriptor.JsonEntityWriter.writeJson


class JsonSpecification extends Specification {
def "should correctly parse/write json"() {
setup:
def json =
"""
{
"result": [
{
"data": [
{
"key": "ÔÈÎ",
"value": "ÞÐÎÂÀ ÀÍÀÑÒÀÑÈß Âß×ÅÑËÀÂÎÂÍÀ"
},
{
"key": "Áàëëàíñ",
"value": "-2084.06"
},
{
"key": "Ëîãèí PPPoE",
"value": "qcnsua6p5u"
}
],
"type": "kv",
"title": "Èíôîðìàöèÿ î êëèåíòå"
},
{
"data": [
[
{
"key": "Èìÿ ñåðâèñà",
"value": "i0011251363"
},
{
"key": "Òèï ñåðâèñà",
"value": "INTERNET"
},
{
"key": "Òàðèôíûé ïëàí",
"value": "[ÐÒÊ] xPON Âñ¸ âìåñòå \\"Äëÿ íà÷àëà\\" ÌÎÍÎ 100Ìáèò/ñ"
},
{
"key": "Ñêîðîñòü ïî òàðèôó [Ìáèò/ñåê]",
"value": "100"
}
]
],
"type": "table",
"title": "Èíôîðìàöèÿ îá óñëóãàõ"
},
{
"data": [
{
"key": "Äàííûå ÒÓ",
"value": "RST-TCN-GIRNOV-PON1-5608-001-013_01-06 [] âîëîêíî âîëîêíî41-48/43"
},
{
"key": "Îáîðóäîâàíèå ÒÓ",
"value": "RST-TCN-GIRNOV-PON1-5608-001-013_01-06"
},
{
"key": "Ïîðò ÒÓ",
"value": "41/48/43"
},
{
"key": "Àâòîðèçîâàí ñ",
"value": "RST-TCN-GIRNOV-PON1-5608 xpon 0/1/0/1:1.1.2000"
},
{
"key": "Îáîðóäîâàíèå ïîñëåäíåé ìèëè",
"value": "RST-TCN-GIRNOV-PON1-5608"
},
{
"key": "Ïîðò ïîñëåäíåé ìèëè",
"value": "0/1/1"
}
],
"type": "kv",
"title": "Òåõíè÷åñêèé ó÷åò"
},
{
"data": [
{
"key": "×èñëî ñåññèé çà ïîñëåäíèé ÷àñ",
"value": "0"
},
{
"key": "×èñëî ñåññèé çà ïîñëåäíèå 3 ÷àñà",
"value": "0"
},
{
"key": "×èñëî ñåññèé çà ïîñëåäíèå ñóòêè",
"value": "3"
},
{
"key": "×èñëî ñåññèé çà ïîñëåäíèå 3-îå ñóòîê",
"value": "3"
}
],
"type": "kv",
"title": "Ñòàòèñòèêà ñåññèé"
},
{
"data": [
{
"key": "Ëîãèí",
"value": "qcnsua6p5u"
},
{
"key": "IP",
"value": "46.61.84.219"
},
{
"key": "Íà÷àëî",
"value": "2020-02-25T08:17:29.000Z"
},
{
"key": "Êîíåö",
"value": "2020-02-28T08:17:30.000Z"
},
{
"key": "Àêòèâíà",
"value": "Íåò",
"estimate": "2"
}
],
"type": "kv",
"title": "Ïîñëåäíÿÿ ñåññèÿ"
},
{
"data": [
{
"key": "Cîñòîÿíèå ïëàòû",
"value": "ðàáîòàåò êîððåêíî",
"estimate": "0"
},
{
"key": "Àäìèíèñòðàòèâíîå ñîñòîÿíèå ïîðòà",
"value": "âêëþ÷åí",
"estimate": "0"
},
{
"key": "Îïåðàòèâíîå ñîñòîÿíèå ïîðòà",
"value": "âêëþ÷åí",
"estimate": "0"
},
{
"key": "Ñòàòóñ ïîäêëþ÷åíèÿ ONT",
"value": "îòêëþ÷åí",
"estimate": "1"
},
{
"key": "Îïèñàíèå",
"value": "auto-add-Mon-Jan-13-12-10-18-202"
},
{
"key": "Äëèíà ëèíèè [km]",
"value": "0"
},
{
"key": "Çàòóõàíèå [dB]",
"value": "0",
"estimate": "0"
},
{
"key": "SN",
"value": "5A544547C8BB20DF (ZTEG-C8BB20DF)"
}
],
"type": "kv",
"title": "Ñîñòîÿíèå ïîðòà"
},
{
"data": [],
"type": "table",
"title": "Òàáëèöà MAC-àäðåñîâ"
},
{
"data": [
[]
],
"type": "table1",
"title": "Ïîðòû ONT"
}
],
"request_id": "6778335",
"conclusions": [
{
"conclusion_text": "Äàííûå ÒÓ ïîëó÷åíû îáîèìè ìåòîäàìè íî íå ðàâíû.",
"conclusion_level": 2,
"conclusion_title": "Òåõíè÷åñêèé ó÷åò"
}
],
"request_created": "2020-05-12T19:43:49+0000"
}"""
when:
def value = readJson(json)

then:
writeJson(value).replaceAll(NEW_LINE, EMPTY_STRING).replaceAll(SPACE, EMPTY_STRING) == json.replaceAll(NEW_LINE, EMPTY_STRING).replaceAll(SPACE, EMPTY_STRING)
}

}

0 comments on commit d6f130e

Please sign in to comment.