Java Code Examples for elemental.json.Json#create()

The following examples show how to use elemental.json.Json#create() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ElementListenerMap.java    From flow with Apache License 2.0 6 votes vote down vote up
public JsonValue toJson() {
    if (debounceSettings.isEmpty()) {
        return Json.create(false);
    } else if (debounceSettings.size() == 1
            && debounceSettings.containsKey(Integer.valueOf(0))) {
        // Shorthand if only debounce is a dummy filter debounce
        return Json.create(true);
    } else {
        // [[timeout1, phase1, phase2, ...], [timeout2, phase1, ...]]
        return debounceSettings.entrySet().stream()
                .map(entry -> Stream
                        .concat(Stream.of(
                                Json.create(entry.getKey().intValue())),
                                entry.getValue().stream()
                                        .map(DebouncePhase::getIdentifier)
                                        .map(Json::create))
                        .collect(JsonUtils.asArray()))
                .collect(JsonUtils.asArray());
    }

}
 
Example 2
Source File: ListAddChange.java    From flow with Apache License 2.0 6 votes vote down vote up
@Override
protected void populateJson(JsonObject json, ConstantPool constantPool) {
    json.put(JsonConstants.CHANGE_TYPE, JsonConstants.CHANGE_TYPE_SPLICE);

    super.populateJson(json, constantPool);

    json.put(JsonConstants.CHANGE_SPLICE_INDEX, getIndex());

    Function<Object, JsonValue> mapper;
    String addKey;
    if (nodeValues) {
        addKey = JsonConstants.CHANGE_SPLICE_ADD_NODES;
        mapper = item -> Json.create(((StateNode) item).getId());
    } else {
        addKey = JsonConstants.CHANGE_SPLICE_ADD;
        mapper = item -> JsonCodec.encodeWithConstantPool(item,
                constantPool);
    }

    JsonArray newItemsJson = newItems.stream().map(mapper)
            .collect(JsonUtils.asArray());
    json.put(addKey, newItemsJson);
}
 
Example 3
Source File: JavaScriptInvocationTest.java    From flow with Apache License 2.0 6 votes vote down vote up
@Test
public void testSerializable() {
    JavaScriptInvocation invocation = new UIInternals.JavaScriptInvocation(
            "expression", "string", Json.create("jsonString"));

    JavaScriptInvocation deserialized = SerializationUtils
            .deserialize(SerializationUtils.serialize(invocation));

    Assert.assertNotSame(invocation, deserialized);

    Assert.assertEquals("expression", deserialized.getExpression());
    Assert.assertEquals(2, deserialized.getParameters().size());
    Assert.assertEquals("string", deserialized.getParameters().get(0));
    Assert.assertEquals("jsonString",
            ((JsonString) deserialized.getParameters().get(1)).getString());
}
 
Example 4
Source File: ClientJsonCodec.java    From flow with Apache License 2.0 6 votes vote down vote up
/**
 * Helper for encoding any "primitive" value that is directly supported in
 * JSON. Supported values types are {@link String}, {@link Number},
 * {@link Boolean}, {@link JsonValue}. <code>null</code> is also supported.
 *
 * @param value
 *            the value to encode
 * @return the value encoded as JSON
 */
public static JsonValue encodeWithoutTypeInfo(Object value) {
    if (value == null) {
        // undefined shouln't go as undefined, it should be encoded as null
        return Json.createNull();
    } else if (GWT.isScript()) {
        return WidgetUtil.crazyJsoCast(value);
    } else {
        if (value instanceof String) {
            return Json.create((String) value);
        } else if (value instanceof Number) {
            return Json.create(((Number) value).doubleValue());
        } else if (value instanceof Boolean) {
            return Json.create(((Boolean) value).booleanValue());
        } else if (value instanceof JsonValue) {
            return (JsonValue) value;
        }
        throw new IllegalArgumentException(
                "Can't encode" + value.getClass() + " to json");
    }
}
 
Example 5
Source File: JsonCodecTest.java    From flow with Apache License 2.0 5 votes vote down vote up
@Test
public void decodeAs_booleanJson() {
    JsonBoolean json = Json.create(true);
    Assert.assertTrue(JsonCodec.decodeAs(json, Boolean.class));
    Assert.assertEquals("true", JsonCodec.decodeAs(json, String.class));
    Assert.assertEquals(Integer.valueOf(1),
            JsonCodec.decodeAs(json, Integer.class));
    Assert.assertEquals(Double.valueOf(1.0),
            JsonCodec.decodeAs(json, Double.class));
    Assert.assertEquals(json, JsonCodec.decodeAs(json, JsonValue.class));
}
 
Example 6
Source File: JsonCodecTest.java    From flow with Apache License 2.0 5 votes vote down vote up
@Test
public void decodeAs_stringJson() {
    JsonString json = Json.create("Test123 String\n !%");
    Assert.assertTrue(JsonCodec.decodeAs(json, Boolean.class));
    Assert.assertEquals("Test123 String\n !%",
            JsonCodec.decodeAs(json, String.class));
    Assert.assertEquals(Integer.valueOf(0),
            JsonCodec.decodeAs(json, Integer.class));
    Assert.assertTrue(JsonCodec.decodeAs(json, Double.class).isNaN());
    Assert.assertEquals(json, JsonCodec.decodeAs(json, JsonValue.class));
}
 
Example 7
Source File: JsonCodecTest.java    From flow with Apache License 2.0 5 votes vote down vote up
@Test
public void decodeAs_numberJson() {
    JsonNumber json = Json.create(15.7);
    Assert.assertTrue(JsonCodec.decodeAs(json, Boolean.class));
    Assert.assertEquals("15.7", JsonCodec.decodeAs(json, String.class));
    Assert.assertEquals(Integer.valueOf(15),
            JsonCodec.decodeAs(json, Integer.class));
    Assert.assertEquals(Double.valueOf(15.7),
            JsonCodec.decodeAs(json, Double.class));
    Assert.assertEquals(json, JsonCodec.decodeAs(json, JsonValue.class));
}
 
Example 8
Source File: AbstractBasicModelType.java    From flow with Apache License 2.0 4 votes vote down vote up
@Override
public JsonValue toJson() {
    return Json.create(type.getSimpleName());
}
 
Example 9
Source File: JsonCodec.java    From flow with Apache License 2.0 3 votes vote down vote up
/**
 * Encodes a "primitive" value or a constant pool reference to JSON. This
 * methods supports {@link ConstantPoolKey} in addition to the types
 * supported by {@link #encodeWithoutTypeInfo(Object)}.
 *
 * @param value
 *            the value to encode
 * @param constantPool
 *            the constant pool to use for encoding constant pool references
 * @return the value encoded as JSON
 */
public static JsonValue encodeWithConstantPool(Object value,
        ConstantPool constantPool) {
    if (value instanceof ConstantPoolKey) {
        ConstantPoolKey reference = (ConstantPoolKey) value;
        return Json.create(constantPool.getConstantId(reference));
    } else {
        return encodeWithoutTypeInfo(value);
    }
}