Search in sources :

Example 16 with JSONArray

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

the class OpenJsonUtilsTest method streamJsonArray_ShouldReturnEmptyStreamWhenJsonArrayIsEmpty.

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

Example 17 with JSONArray

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

the class OpenJsonUtilsTest method streamJsonArray_ShouldNotConvertJsonObjectValue.

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

Example 18 with JSONArray

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

the class JsonUtils method asArray.

/**
 * Converts a Map to JSONArray suitable for jQuery#param().
 *
 * @param map
 *      the map with key/value(s)
 * @return a JSONArray that contains JSONObject's with name/value pairs
 * @throws JSONException
 */
public static JSONArray asArray(Map<String, Object> map) throws JSONException {
    JSONArray jsonArray = new JSONArray();
    if (map != null) {
        for (Map.Entry<String, Object> entry : map.entrySet()) {
            String name = entry.getKey();
            Object value = entry.getValue();
            if (value instanceof List) {
                List<?> values = (List<?>) value;
                for (Object v : values) {
                    if (v != null) {
                        JSONObject object = new JSONObject();
                        object.put("name", name);
                        object.put("value", v);
                        jsonArray.put(object);
                    }
                }
            } else if (value != null) {
                if (value.getClass().isArray()) {
                    Object[] array = (Object[]) value;
                    for (Object v : array) {
                        if (v != null) {
                            JSONObject object = new JSONObject();
                            object.put("name", name);
                            object.put("value", v);
                            jsonArray.put(object);
                        }
                    }
                } else {
                    JSONObject object = new JSONObject();
                    object.put("name", name);
                    object.put("value", value);
                    jsonArray.put(object);
                }
            }
        }
    }
    return jsonArray;
}
Also used : JSONObject(com.github.openjson.JSONObject) JSONArray(com.github.openjson.JSONArray) JSONObject(com.github.openjson.JSONObject) List(java.util.List) Map(java.util.Map)

Example 19 with JSONArray

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

the class AbstractDefaultAjaxBehavior method getCallbackFunctionBody.

/**
 * Generates the body the {@linkplain #getCallbackFunction(CallbackParameter...) callback
 * function}. To embed this code directly into a piece of javascript, make sure any context
 * parameters are available as local variables, global variables or within the closure.
 *
 * @param extraParameters
 * @return The body of the {@linkplain #getCallbackFunction(CallbackParameter...) callback
 *         function}.
 */
public CharSequence getCallbackFunctionBody(CallbackParameter... extraParameters) {
    AjaxRequestAttributes attributes = getAttributes();
    attributes.setEventNames();
    CharSequence attrsJson = renderAjaxAttributes(getComponent(), attributes);
    StringBuilder sb = new StringBuilder();
    sb.append("var attrs = ");
    sb.append(attrsJson);
    sb.append(";\n");
    JSONArray jsonArray = new JSONArray();
    for (CallbackParameter curExtraParameter : extraParameters) {
        if (curExtraParameter.getAjaxParameterName() != null) {
            try {
                JSONObject object = new JSONObject();
                object.put("name", curExtraParameter.getAjaxParameterName());
                object.put("value", new JSONFunction(curExtraParameter.getAjaxParameterCode()));
                jsonArray.put(object);
            } catch (JSONException e) {
                throw new WicketRuntimeException(e);
            }
        }
    }
    sb.append("var params = ").append(jsonArray).append(";\n");
    sb.append("attrs.").append(AjaxAttributeName.EXTRA_PARAMETERS).append(" = params.concat(attrs.").append(AjaxAttributeName.EXTRA_PARAMETERS).append(" || []);\n");
    sb.append("Wicket.Ajax.ajax(attrs);\n");
    return sb;
}
Also used : AjaxRequestAttributes(org.apache.wicket.ajax.attributes.AjaxRequestAttributes) JSONObject(com.github.openjson.JSONObject) WicketRuntimeException(org.apache.wicket.WicketRuntimeException) JSONArray(com.github.openjson.JSONArray) JSONException(com.github.openjson.JSONException) CallbackParameter(org.apache.wicket.ajax.attributes.CallbackParameter) JSONFunction(org.apache.wicket.ajax.json.JSONFunction)

Example 20 with JSONArray

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

the class AbstractDefaultAjaxBehavior method renderAjaxAttributes.

/**
 * @param component
 * @param attributes
 * @return the attributes as string in JSON format
 */
protected final CharSequence renderAjaxAttributes(final Component component, AjaxRequestAttributes attributes) {
    JSONObject attributesJson = new JSONObject();
    try {
        attributesJson.put(AjaxAttributeName.URL.jsonName(), getCallbackUrl());
        Method method = attributes.getMethod();
        if (Method.POST == method) {
            attributesJson.put(AjaxAttributeName.METHOD.jsonName(), method);
        }
        if (component instanceof Page == false) {
            String componentId = component.getMarkupId();
            attributesJson.put(AjaxAttributeName.MARKUP_ID.jsonName(), componentId);
        }
        String formId = attributes.getFormId();
        if (Strings.isEmpty(formId) == false) {
            attributesJson.put(AjaxAttributeName.FORM_ID.jsonName(), formId);
        }
        if (attributes.isMultipart()) {
            attributesJson.put(AjaxAttributeName.IS_MULTIPART.jsonName(), true);
        }
        String submittingComponentId = attributes.getSubmittingComponentName();
        if (Strings.isEmpty(submittingComponentId) == false) {
            attributesJson.put(AjaxAttributeName.SUBMITTING_COMPONENT_NAME.jsonName(), submittingComponentId);
        }
        CharSequence childSelector = attributes.getChildSelector();
        if (Strings.isEmpty(childSelector) == false) {
            attributesJson.put(AjaxAttributeName.CHILD_SELECTOR.jsonName(), childSelector);
        }
        if (attributes.isSerializeRecursively()) {
            attributesJson.put(AjaxAttributeName.SERIALIZE_RECURSIVELY.jsonName(), true);
        }
        String indicatorId = findIndicatorId();
        if (Strings.isEmpty(indicatorId) == false) {
            attributesJson.put(AjaxAttributeName.INDICATOR_ID.jsonName(), indicatorId);
        }
        for (IAjaxCallListener ajaxCallListener : attributes.getAjaxCallListeners()) {
            if (ajaxCallListener != null) {
                CharSequence initHandler = ajaxCallListener.getInitHandler(component);
                appendListenerHandler(initHandler, attributesJson, AjaxAttributeName.INIT_HANDLER.jsonName(), INIT_HANDLER_FUNCTION_TEMPLATE);
                CharSequence beforeHandler = ajaxCallListener.getBeforeHandler(component);
                appendListenerHandler(beforeHandler, attributesJson, AjaxAttributeName.BEFORE_HANDLER.jsonName(), BEFORE_HANDLER_FUNCTION_TEMPLATE);
                CharSequence beforeSendHandler = ajaxCallListener.getBeforeSendHandler(component);
                appendListenerHandler(beforeSendHandler, attributesJson, AjaxAttributeName.BEFORE_SEND_HANDLER.jsonName(), BEFORE_SEND_HANDLER_FUNCTION_TEMPLATE);
                CharSequence afterHandler = ajaxCallListener.getAfterHandler(component);
                appendListenerHandler(afterHandler, attributesJson, AjaxAttributeName.AFTER_HANDLER.jsonName(), AFTER_HANDLER_FUNCTION_TEMPLATE);
                CharSequence successHandler = ajaxCallListener.getSuccessHandler(component);
                appendListenerHandler(successHandler, attributesJson, AjaxAttributeName.SUCCESS_HANDLER.jsonName(), SUCCESS_HANDLER_FUNCTION_TEMPLATE);
                CharSequence failureHandler = ajaxCallListener.getFailureHandler(component);
                appendListenerHandler(failureHandler, attributesJson, AjaxAttributeName.FAILURE_HANDLER.jsonName(), FAILURE_HANDLER_FUNCTION_TEMPLATE);
                CharSequence completeHandler = ajaxCallListener.getCompleteHandler(component);
                appendListenerHandler(completeHandler, attributesJson, AjaxAttributeName.COMPLETE_HANDLER.jsonName(), COMPLETE_HANDLER_FUNCTION_TEMPLATE);
                CharSequence precondition = ajaxCallListener.getPrecondition(component);
                appendListenerHandler(precondition, attributesJson, AjaxAttributeName.PRECONDITION.jsonName(), PRECONDITION_FUNCTION_TEMPLATE);
                CharSequence doneHandler = ajaxCallListener.getDoneHandler(component);
                appendListenerHandler(doneHandler, attributesJson, AjaxAttributeName.DONE_HANDLER.jsonName(), DONE_HANDLER_FUNCTION_TEMPLATE);
            }
        }
        JSONArray extraParameters = JsonUtils.asArray(attributes.getExtraParameters());
        if (extraParameters.length() > 0) {
            attributesJson.put(AjaxAttributeName.EXTRA_PARAMETERS.jsonName(), extraParameters);
        }
        List<CharSequence> dynamicExtraParameters = attributes.getDynamicExtraParameters();
        if (dynamicExtraParameters != null) {
            for (CharSequence dynamicExtraParameter : dynamicExtraParameters) {
                String func = String.format(DYNAMIC_PARAMETER_FUNCTION_TEMPLATE, dynamicExtraParameter);
                JSONFunction function = new JSONFunction(func);
                attributesJson.append(AjaxAttributeName.DYNAMIC_PARAMETER_FUNCTION.jsonName(), function);
            }
        }
        if (attributes.isAsynchronous() == false) {
            attributesJson.put(AjaxAttributeName.IS_ASYNC.jsonName(), false);
        }
        String[] eventNames = attributes.getEventNames();
        if (eventNames.length == 1) {
            attributesJson.put(AjaxAttributeName.EVENT_NAME.jsonName(), eventNames[0]);
        } else {
            for (String eventName : eventNames) {
                attributesJson.append(AjaxAttributeName.EVENT_NAME.jsonName(), eventName);
            }
        }
        AjaxChannel channel = attributes.getChannel();
        if (channel != null && channel.equals(AjaxChannel.DEFAULT) == false) {
            attributesJson.put(AjaxAttributeName.CHANNEL.jsonName(), channel);
        }
        if (attributes.isPreventDefault()) {
            attributesJson.put(AjaxAttributeName.IS_PREVENT_DEFAULT.jsonName(), true);
        }
        if (AjaxRequestAttributes.EventPropagation.STOP.equals(attributes.getEventPropagation())) {
            attributesJson.put(AjaxAttributeName.EVENT_PROPAGATION.jsonName(), "stop");
        } else if (AjaxRequestAttributes.EventPropagation.STOP_IMMEDIATE.equals(attributes.getEventPropagation())) {
            attributesJson.put(AjaxAttributeName.EVENT_PROPAGATION.jsonName(), "stopImmediate");
        }
        Duration requestTimeout = attributes.getRequestTimeout();
        if (requestTimeout != null) {
            attributesJson.put(AjaxAttributeName.REQUEST_TIMEOUT.jsonName(), requestTimeout.getMilliseconds());
        }
        boolean wicketAjaxResponse = attributes.isWicketAjaxResponse();
        if (wicketAjaxResponse == false) {
            attributesJson.put(AjaxAttributeName.IS_WICKET_AJAX_RESPONSE.jsonName(), false);
        }
        String dataType = attributes.getDataType();
        if (AjaxRequestAttributes.XML_DATA_TYPE.equals(dataType) == false) {
            attributesJson.put(AjaxAttributeName.DATATYPE.jsonName(), dataType);
        }
        ThrottlingSettings throttlingSettings = attributes.getThrottlingSettings();
        if (throttlingSettings != null) {
            JSONObject throttlingSettingsJson = new JSONObject();
            String throttleId = throttlingSettings.getId();
            if (throttleId == null) {
                throttleId = component.getMarkupId();
            }
            throttlingSettingsJson.put(AjaxAttributeName.THROTTLING_ID.jsonName(), throttleId);
            throttlingSettingsJson.put(AjaxAttributeName.THROTTLING_DELAY.jsonName(), throttlingSettings.getDelay().getMilliseconds());
            if (throttlingSettings.getPostponeTimerOnUpdate()) {
                throttlingSettingsJson.put(AjaxAttributeName.THROTTLING_POSTPONE_ON_UPDATE.jsonName(), true);
            }
            attributesJson.put(AjaxAttributeName.THROTTLING.jsonName(), throttlingSettingsJson);
        }
        postprocessConfiguration(attributesJson, component);
    } catch (JSONException e) {
        throw new WicketRuntimeException(e);
    }
    String attributesAsJson = attributesJson.toString();
    return attributesAsJson;
}
Also used : ThrottlingSettings(org.apache.wicket.ajax.attributes.ThrottlingSettings) IAjaxCallListener(org.apache.wicket.ajax.attributes.IAjaxCallListener) WicketRuntimeException(org.apache.wicket.WicketRuntimeException) JSONArray(com.github.openjson.JSONArray) JSONException(com.github.openjson.JSONException) Page(org.apache.wicket.Page) Duration(org.apache.wicket.util.time.Duration) Method(org.apache.wicket.ajax.attributes.AjaxRequestAttributes.Method) JSONFunction(org.apache.wicket.ajax.json.JSONFunction) JSONObject(com.github.openjson.JSONObject)

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