Search in sources :

Example 1 with MemoryDeserializer

use of org.develnext.jphp.json.gson.MemoryDeserializer in project jphp by jphp-compiler.

the class JsonFunctions method json_decode.

public static Memory json_decode(Environment env, String json, boolean assoc, int depth) {
    MemoryDeserializer memoryDeserializer = new MemoryDeserializer();
    memoryDeserializer.setEnv(env);
    GsonBuilder gsonBuilder = JsonExtension.createGsonBuilderForDecode(memoryDeserializer);
    memoryDeserializer.setAssoc(assoc);
    memoryDeserializer.setMaxDepth(depth);
    Gson gson = gsonBuilder.create();
    try {
        env.setUserValue(JsonFunctions.class.getName() + "#error", null);
        Memory r = gson.fromJson(json, Memory.class);
        if (r == null)
            return Memory.NULL;
        else
            return assoc ? r.toImmutable() : r;
    } catch (MemoryDeserializer.MaxDepthException e) {
        env.setUserValue(JsonFunctions.class.getName() + "#error", JsonConstants.JSON_ERROR_DEPTH);
    } catch (JsonSyntaxException e) {
        env.setUserValue(JsonFunctions.class.getName() + "#error", JsonConstants.JSON_ERROR_SYNTAX);
    } catch (JsonParseException e) {
        env.setUserValue(JsonFunctions.class.getName() + "#error", JsonConstants.JSON_ERROR_STATE_MISMATCH);
    }
    return Memory.NULL;
}
Also used : JsonSyntaxException(com.google.gson.JsonSyntaxException) GsonBuilder(com.google.gson.GsonBuilder) Memory(php.runtime.Memory) StringMemory(php.runtime.memory.StringMemory) Gson(com.google.gson.Gson) JsonParseException(com.google.gson.JsonParseException) MemoryDeserializer(org.develnext.jphp.json.gson.MemoryDeserializer)

Aggregations

Gson (com.google.gson.Gson)1 GsonBuilder (com.google.gson.GsonBuilder)1 JsonParseException (com.google.gson.JsonParseException)1 JsonSyntaxException (com.google.gson.JsonSyntaxException)1 MemoryDeserializer (org.develnext.jphp.json.gson.MemoryDeserializer)1 Memory (php.runtime.Memory)1 StringMemory (php.runtime.memory.StringMemory)1