use of net.minidev.json.parser.JSONParser in project json-android-compare by martinadamek.
the class SmartJson method parsePublicTimeline.
public List<Map> parsePublicTimeline(InputStream inputStream) {
List<Map> result = new ArrayList<Map>();
JSONParser p = new JSONParser(JSONParser.MODE_JSON_SIMPLE);
try {
Map map;
Set keys;
Set keys2;
JSONObject user;
JSONObject jsonObject;
JSONArray jsonArray = (JSONArray) p.parse(new InputStreamReader(inputStream));
int size = jsonArray.size();
for (int i = 0; i < size; i++) {
map = new HashMap();
jsonObject = (JSONObject) jsonArray.get(i);
keys = jsonObject.keySet();
for (Object key : keys) {
if ("user".equals(key)) {
user = (JSONObject) jsonObject.get(key);
keys2 = user.keySet();
for (Object key2 : keys2) {
map.put("user." + key2, user.get(key2));
}
} else {
map.put(key, jsonObject.get(key));
}
}
result.add(map);
}
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
use of net.minidev.json.parser.JSONParser in project ddf by codice.
the class TestRestEndpoint method testGetDocumentSourcesSuccess.
/**
* Tests getting source information
*
* @throws Exception
*/
@Test
public void testGetDocumentSourcesSuccess() throws Exception {
final String localSourceId = "local";
final String fed1SourceId = "fed1";
final String fed2SourceId = "fed2";
final String version = "4.0";
final String jsonMimeTypeString = "application/json";
Set<ContentType> contentTypes = new HashSet<ContentType>();
contentTypes.add(new ContentTypeImpl("ct1", "v1"));
contentTypes.add(new ContentTypeImpl("ct2", "v2"));
contentTypes.add(new ContentTypeImpl("ct3", null));
JSONArray contentTypesInJSON = new JSONArray();
for (ContentType ct : contentTypes) {
JSONObject ob = new JSONObject();
ob.put("name", ct.getName());
ob.put("version", ct.getVersion() != null ? ct.getVersion() : "");
contentTypesInJSON.add(ob);
}
Set<SourceDescriptor> sourceDescriptors = new HashSet<SourceDescriptor>();
SourceDescriptorImpl localDescriptor = new SourceDescriptorImpl(localSourceId, contentTypes);
localDescriptor.setVersion(version);
localDescriptor.setAvailable(true);
SourceDescriptorImpl fed1Descriptor = new SourceDescriptorImpl(fed1SourceId, contentTypes);
fed1Descriptor.setVersion(version);
fed1Descriptor.setAvailable(true);
SourceDescriptorImpl fed2Descriptor = new SourceDescriptorImpl(fed2SourceId, null);
fed2Descriptor.setAvailable(true);
sourceDescriptors.add(localDescriptor);
sourceDescriptors.add(fed1Descriptor);
sourceDescriptors.add(fed2Descriptor);
SourceInfoResponse sourceInfoResponse = new SourceInfoResponseImpl(null, null, sourceDescriptors);
CatalogFramework framework = mock(CatalogFramework.class);
when(framework.getSourceInfo(isA(SourceInfoRequestEnterprise.class))).thenReturn(sourceInfoResponse);
RESTEndpoint restEndpoint = new RESTEndpoint(framework);
Response response = restEndpoint.getDocument(null, null);
assertEquals(OK, response.getStatus());
assertEquals(jsonMimeTypeString, response.getMetadata().get("Content-Type").get(0));
String responseMessage = IOUtils.toString((ByteArrayInputStream) response.getEntity());
JSONArray srcList = (JSONArray) new JSONParser().parse(responseMessage);
assertEquals(3, srcList.size());
for (Object o : srcList) {
JSONObject src = (JSONObject) o;
assertEquals(true, src.get("available"));
String id = (String) src.get("id");
if (id.equals(localSourceId)) {
assertThat((Iterable<Object>) src.get("contentTypes"), hasItems(contentTypesInJSON.toArray()));
assertEquals(contentTypes.size(), ((JSONArray) src.get("contentTypes")).size());
assertEquals(version, src.get("version"));
} else if (id.equals(fed1SourceId)) {
assertThat((Iterable<Object>) src.get("contentTypes"), hasItems(contentTypesInJSON.toArray()));
assertEquals(contentTypes.size(), ((JSONArray) src.get("contentTypes")).size());
assertEquals(version, src.get("version"));
} else if (id.equals(fed2SourceId)) {
assertEquals(0, ((JSONArray) src.get("contentTypes")).size());
assertEquals("", src.get("version"));
} else {
fail("Invalid ID returned");
}
}
}
use of net.minidev.json.parser.JSONParser in project ddf by codice.
the class GeoNamesWebService method query.
private Object query(String urlStr) {
final String response;
try {
WebClient client = createWebClient(urlStr);
response = client.acceptEncoding(StandardCharsets.UTF_8.name()).accept("application/json").get(String.class);
} catch (WebApplicationException | ProcessingException e) {
LOGGER.debug("Error while making GeoNames request.", e);
return null;
}
try {
JSONParser parser = new JSONParser(JSONParser.MODE_JSON_SIMPLE);
return parser.parse(response);
} catch (ParseException e) {
LOGGER.debug("Error while parsing JSON message from GeoNames service.", e);
return null;
}
}
Aggregations