Search in sources :

Example 1 with StdClass

use of php.runtime.lang.StdClass in project jphp by jphp-compiler.

the class JsonFunctionsTest method testObjectJsonEncode.

@Test
public void testObjectJsonEncode() {
    assertEquals("{\"0\":100,\"1\":500}", JsonFunctions.json_encode(new ArrayMemory(100, 500), JsonConstants.JSON_FORCE_OBJECT));
    ArrayMemory array = new ArrayMemory(100, 500);
    array.put("x", new LongMemory(100500));
    assertEquals("{\"0\":100,\"1\":500,\"x\":100500}", JsonFunctions.json_encode(array));
    StdClass stdClass = new StdClass(env);
    stdClass.getProperties().put("x", new LongMemory(100));
    stdClass.getProperties().put("y", new LongMemory(500));
    stdClass.getProperties().put("\0*\0z", new LongMemory(100500));
    assertEquals("{\"x\":100,\"y\":500}", JsonFunctions.json_encode(new ObjectMemory(stdClass)));
}
Also used : StdClass(php.runtime.lang.StdClass) Test(org.junit.Test)

Example 2 with StdClass

use of php.runtime.lang.StdClass in project jphp by jphp-compiler.

the class DeserializerTest method testObjects.

@Test
public void testObjects() {
    Memory value = unserialize("O:8:\"stdClass\":0:{}");
    Assert.assertTrue(value instanceof ObjectMemory);
    Assert.assertTrue(value.toValue(ObjectMemory.class).value instanceof StdClass);
    Assert.assertEquals(0, value.toValue(ObjectMemory.class).getProperties().size());
    value = unserialize("O:8:\"stdClass\":2:{s:1:\"x\";s:3:\"foo\";s:1:\"y\";s:3:\"bar\";}");
    Assert.assertTrue(value instanceof ObjectMemory);
    Assert.assertTrue(value.toValue(ObjectMemory.class).value instanceof StdClass);
    Assert.assertEquals(2, value.toValue(ObjectMemory.class).getProperties().size());
    Assert.assertEquals("foo", value.toValue(ObjectMemory.class).getProperties().valueOfIndex("x").toString());
    Assert.assertEquals("bar", value.toValue(ObjectMemory.class).getProperties().valueOfIndex("y").toString());
}
Also used : ObjectMemory(php.runtime.memory.ObjectMemory) Memory(php.runtime.Memory) ArrayMemory(php.runtime.memory.ArrayMemory) ObjectMemory(php.runtime.memory.ObjectMemory) StdClass(php.runtime.lang.StdClass) Test(org.junit.Test)

Example 3 with StdClass

use of php.runtime.lang.StdClass in project jphp by jphp-compiler.

the class SerializerTest method testObjects.

@Test
public void testObjects() {
    Assert.assertEquals("O:8:\"stdClass\":0:{}", serialize(new ObjectMemory(new StdClass(environment))));
    StdClass stdClass = new StdClass(environment);
    stdClass.getProperties().refOfIndex("x").assign("foo");
    stdClass.getProperties().refOfIndex("y").assign("bar");
    Assert.assertEquals("O:8:\"stdClass\":2:{s:1:\"x\";s:3:\"foo\";s:1:\"y\";s:3:\"bar\";}", serialize(new ObjectMemory(stdClass)));
}
Also used : StdClass(php.runtime.lang.StdClass) Test(org.junit.Test)

Example 4 with StdClass

use of php.runtime.lang.StdClass in project jphp by jphp-compiler.

the class MemoryDeserializer method convert.

protected Memory convert(JsonElement json, int depth) {
    if (depth > maxDepth)
        throw new MaxDepthException();
    if (json.isJsonNull())
        return Memory.NULL;
    if (json.isJsonPrimitive()) {
        JsonPrimitive jsonPrimitive = json.getAsJsonPrimitive();
        if (jsonPrimitive.isString())
            return StringMemory.valueOf(jsonPrimitive.getAsString());
        else if (jsonPrimitive.isBoolean())
            return jsonPrimitive.getAsBoolean() ? Memory.TRUE : Memory.FALSE;
        else if (jsonPrimitive.isNumber()) {
            Memory l = StringMemory.toLong(jsonPrimitive.getAsString());
            if (l != null)
                return l;
            else
                return new DoubleMemory(json.getAsDouble());
        }
        return Memory.NULL;
    } else if (json.isJsonArray()) {
        ArrayMemory array = new ArrayMemory();
        for (JsonElement el : json.getAsJsonArray()) array.add(convert(el, depth + 1).toImmutable());
        return array.toConstant();
    } else if (json.isJsonObject()) {
        JsonObject jsonObject = json.getAsJsonObject();
        StdClass stdClass = assoc ? null : new StdClass(env.get());
        ArrayMemory array = assoc ? new ArrayMemory() : stdClass.getProperties();
        for (Map.Entry<String, JsonElement> el : jsonObject.entrySet()) {
            String key = el.getKey();
            if (!key.startsWith("\0"))
                array.put(key, convert(el.getValue(), depth + 1).toImmutable());
        }
        return assoc ? array : new ObjectMemory(stdClass);
    } else
        return Memory.NULL;
}
Also used : ArrayMemory(php.runtime.memory.ArrayMemory) ObjectMemory(php.runtime.memory.ObjectMemory) DoubleMemory(php.runtime.memory.DoubleMemory) Memory(php.runtime.Memory) ArrayMemory(php.runtime.memory.ArrayMemory) StringMemory(php.runtime.memory.StringMemory) ObjectMemory(php.runtime.memory.ObjectMemory) DoubleMemory(php.runtime.memory.DoubleMemory) Map(java.util.Map) StdClass(php.runtime.lang.StdClass)

Example 5 with StdClass

use of php.runtime.lang.StdClass in project jphp by jphp-compiler.

the class Memory method toObject.

public Memory toObject(Environment env) {
    StdClass stdClass = new StdClass(env);
    stdClass.getProperties().refOfIndex("scalar").assign(toImmutable());
    return new ObjectMemory(stdClass);
}
Also used : StdClass(php.runtime.lang.StdClass)

Aggregations

StdClass (php.runtime.lang.StdClass)7 Test (org.junit.Test)4 Memory (php.runtime.Memory)3 ArrayMemory (php.runtime.memory.ArrayMemory)2 ObjectMemory (php.runtime.memory.ObjectMemory)2 Map (java.util.Map)1 ForeachIterator (php.runtime.lang.ForeachIterator)1 DoubleMemory (php.runtime.memory.DoubleMemory)1 StringMemory (php.runtime.memory.StringMemory)1