Search in sources :

Example 21 with JSONArray

use of com.github.openjson.JSONArray in project openmeetings by apache.

the class WebSocketHelper method getMessage.

public static JSONObject getMessage(User curUser, List<ChatMessage> list, BiConsumer<JSONObject, User> uFmt) {
    JSONArray arr = new JSONArray();
    final FastDateFormat fullFmt = FormatHelper.getDateTimeFormat(curUser);
    final FastDateFormat dateFmt = FormatHelper.getDateFormat(curUser);
    final FastDateFormat timeFmt = FormatHelper.getTimeFormat(curUser);
    for (ChatMessage m : list) {
        String smsg = m.getMessage();
        smsg = smsg == null ? smsg : " " + smsg.replaceAll("&nbsp;", " ") + " ";
        JSONObject from = new JSONObject().put("id", m.getFromUser().getId()).put("displayName", m.getFromName()).put("name", getDisplayName(m.getFromUser()));
        if (uFmt != null) {
            uFmt.accept(from, m.getFromUser());
        }
        arr.put(setScope(new JSONObject(), m, curUser.getId()).put("id", m.getId()).put("message", smsg).put("from", from).put("actions", curUser.getId() == m.getFromUser().getId() ? "short" : "full").put("sent", fullFmt.format(m.getSent())).put("date", dateFmt.format(m.getSent())).put("time", timeFmt.format(m.getSent())));
    }
    return new JSONObject().put("type", "chat").put("msg", arr);
}
Also used : JSONObject(com.github.openjson.JSONObject) ChatMessage(org.apache.openmeetings.db.entity.basic.ChatMessage) JSONArray(com.github.openjson.JSONArray) FastDateFormat(org.apache.commons.lang3.time.FastDateFormat)

Example 22 with JSONArray

use of com.github.openjson.JSONArray in project triplea by triplea-game.

the class DownloadFileParser method parse.

public static List<DownloadFileDescription> parse(final InputStream is) {
    final JSONArray yamlData = new JSONArray(new Yaml().loadAs(is, List.class));
    final List<DownloadFileDescription> downloads = new ArrayList<>();
    OpenJsonUtils.stream(yamlData).map(JSONObject.class::cast).forEach(yaml -> {
        final String url = yaml.getString(Tags.url.toString());
        final String description = yaml.getString(Tags.description.toString());
        final String mapName = yaml.getString(Tags.mapName.toString());
        final Version version = new Version(yaml.getInt(Tags.version.toString()), 0);
        final DownloadFileDescription.DownloadType downloadType = OpenJsonUtils.optEnum(yaml, DownloadFileDescription.DownloadType.class, Tags.mapType.toString(), DownloadFileDescription.DownloadType.MAP);
        final DownloadFileDescription.MapCategory mapCategory = OpenJsonUtils.optEnum(yaml, DownloadFileDescription.MapCategory.class, Tags.mapCategory.toString(), DownloadFileDescription.MapCategory.EXPERIMENTAL);
        final String img = yaml.optString(Tags.img.toString());
        final DownloadFileDescription dl = new DownloadFileDescription(url, description, mapName, version, downloadType, mapCategory, img);
        downloads.add(dl);
    });
    return downloads;
}
Also used : Version(games.strategy.util.Version) JSONArray(com.github.openjson.JSONArray) ArrayList(java.util.ArrayList) List(java.util.List) ArrayList(java.util.ArrayList) Yaml(org.yaml.snakeyaml.Yaml)

Example 23 with JSONArray

use of com.github.openjson.JSONArray in project triplea by triplea-game.

the class OpenJsonUtilsTest method streamJsonArray_ShouldReturnStreamContainingJsonArrayElements.

@Test
public void streamJsonArray_ShouldReturnStreamContainingJsonArrayElements() {
    final JSONArray jsonArray = new JSONArray(Arrays.asList("text", 1, 1.0));
    final List<Object> elements = OpenJsonUtils.stream(jsonArray).collect(Collectors.toList());
    assertThat(elements, is(Arrays.asList("text", 1, 1.0)));
}
Also used : JSONArray(com.github.openjson.JSONArray) JSONObject(com.github.openjson.JSONObject) Test(org.junit.jupiter.api.Test)

Example 24 with JSONArray

use of com.github.openjson.JSONArray in project triplea by triplea-game.

the class OpenJsonUtilsTest method streamJsonArray_ShouldNotConvertJsonArrayValue.

@Test
public void streamJsonArray_ShouldNotConvertJsonArrayValue() {
    final List<Object> expectedElements = Arrays.asList(new JSONArray(Arrays.asList(42)));
    final JSONArray jsonArray = new JSONArray(expectedElements);
    final List<Object> actualElements = OpenJsonUtils.stream(jsonArray).collect(Collectors.toList());
    assertThat(actualElements, is(expectedElements));
}
Also used : JSONArray(com.github.openjson.JSONArray) JSONObject(com.github.openjson.JSONObject) Test(org.junit.jupiter.api.Test)

Example 25 with JSONArray

use of com.github.openjson.JSONArray in project triplea by triplea-game.

the class OpenJsonUtilsTest method toList_ShouldConvertJsonArrayValueToList.

@Test
public void toList_ShouldConvertJsonArrayValueToList() {
    final JSONArray jsonArray = new JSONArray(Arrays.asList(new JSONArray(Arrays.asList(42))));
    final List<Object> elements = OpenJsonUtils.toList(jsonArray);
    assertThat(elements, is(Arrays.asList(Arrays.asList(42))));
}
Also used : JSONArray(com.github.openjson.JSONArray) JSONObject(com.github.openjson.JSONObject) Test(org.junit.jupiter.api.Test)

Aggregations

JSONArray (com.github.openjson.JSONArray)35 JSONObject (com.github.openjson.JSONObject)30 Test (org.junit.jupiter.api.Test)11 List (java.util.List)5 AppointmentDTO (org.apache.openmeetings.db.dto.calendar.AppointmentDTO)5 ArrayList (java.util.ArrayList)4 Whiteboard (org.apache.openmeetings.db.dto.room.Whiteboard)4 Form (javax.ws.rs.core.Form)3 Response (javax.ws.rs.core.Response)3 JSONException (com.github.openjson.JSONException)2 BufferedImage (java.awt.image.BufferedImage)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 Map (java.util.Map)2 Entry (java.util.Map.Entry)2 Client (org.apache.openmeetings.db.entity.basic.Client)2 Room (org.apache.openmeetings.db.entity.room.Room)2 RoomElement (org.apache.openmeetings.db.entity.room.Room.RoomElement)2 NullStringer (org.apache.openmeetings.util.NullStringer)2 PDDocument (org.apache.pdfbox.pdmodel.PDDocument)2