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;
}
Aggregations