Search in sources :

Example 1 with MemorySerializer

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

the class JsonFunctions method json_encode.

@Immutable
public static String json_encode(Memory memory, int options) {
    GsonBuilder builder;
    if (options != 0) {
        MemorySerializer serializer = new MemorySerializer();
        builder = JsonExtension.createGsonBuilder(serializer);
        if ((options & JsonConstants.JSON_PRETTY_PRINT) == JsonConstants.JSON_PRETTY_PRINT) {
            builder.setPrettyPrinting();
        }
        if ((options & JsonConstants.JSON_HEX_TAG) != JsonConstants.JSON_HEX_TAG) {
            builder.disableHtmlEscaping();
        }
        if ((options & JsonConstants.JSON_FORCE_OBJECT) == JsonConstants.JSON_FORCE_OBJECT) {
            serializer.setForceObject(true);
        }
        if ((options & JsonConstants.JSON_NUMERIC_CHECK) == JsonConstants.JSON_NUMERIC_CHECK) {
            serializer.setNumericCheck(true);
        }
        if ((options & JsonConstants.JSON_UNESCAPED_UNICODE) == JsonConstants.JSON_UNESCAPED_UNICODE) {
            serializer.unescapedUnicode(true);
        }
    } else {
        builder = JsonExtension.DEFAULT_GSON_BUILDER;
    }
    Gson gson = builder.create();
    return gson.toJson(memory);
}
Also used : MemorySerializer(org.develnext.jphp.json.gson.MemorySerializer) GsonBuilder(com.google.gson.GsonBuilder) Gson(com.google.gson.Gson) Immutable(php.runtime.annotation.Runtime.Immutable)

Aggregations

Gson (com.google.gson.Gson)1 GsonBuilder (com.google.gson.GsonBuilder)1 MemorySerializer (org.develnext.jphp.json.gson.MemorySerializer)1 Immutable (php.runtime.annotation.Runtime.Immutable)1