Java 类org.mozilla.javascript.NativeJSON 实例源码

项目:TEI-Completer    文件:JSONTransformer.java   
@Override
public void transform(final InputStream content, final Path transformation, final OutputStream result) throws TransformationException, IOException {
    try {
        final Context context = Context.enter();
        context.setOptimizationLevel(OPTIMIZATION_LEVEL);
        context.setLanguageVersion(LANGUAGE_VERSION);

        final Scriptable scope = context.initStandardObjects();

        //read in the content
        final Object jsonObj = parseJson(context, scope, content);

        //load the javascript with the transform function
        try(final Reader reader = Files.newBufferedReader(transformation, UTF_8)) {
            context.evaluateReader(scope, reader, transformation.getFileName().toString(), 1, null);
        }

        final Object fnTransformObj = scope.get("transform", scope);
        if(!(fnTransformObj instanceof Function)) {
          throw new TransformationException("Function `transform` is not defined!");
        } else {
            final Object functionArgs[] = { jsonObj };
            final Function fnTransform = (Function)fnTransformObj;
            final Object resultObj = fnTransform.call(context, scope, scope, functionArgs);
            final String jsonResult = (String)NativeJSON.stringify(context, scope, resultObj, null, null);

            final char buf[] = new char[4096];
            int read = -1;
            try(final Reader jsonReader = new StringReader(jsonResult);
                final Writer writer = new OutputStreamWriter(result)) {
                while((read = jsonReader.read(buf)) > -1) {
                    writer.write(buf, 0, read);
                }
            }
        }
    } finally {
        Context.exit();
    }
}
项目:AndroidAPS    文件:DetermineBasalAdapterAMAJS.java   
public Object makeParam(JSONObject jsonObject, Context rhino, Scriptable scope) {

        if(jsonObject == null) return Undefined.instance;

        Object param = NativeJSON.parse(rhino, scope, jsonObject.toString(), new Callable() {
            @Override
            public Object call(Context context, Scriptable scriptable, Scriptable scriptable1, Object[] objects) {
                return objects[1];
            }
        });
        return param;
    }
项目:AndroidAPS    文件:DetermineBasalAdapterAMAJS.java   
public Object makeParamArray(JSONArray jsonArray, Context rhino, Scriptable scope) {
        //Object param = NativeJSON.parse(rhino, scope, "{myarray: " + jsonArray.toString() + " }", new Callable() {
        Object param = NativeJSON.parse(rhino, scope, jsonArray.toString(), new Callable() {
        @Override
        public Object call(Context context, Scriptable scriptable, Scriptable scriptable1, Object[] objects) {
            return objects[1];
        }
    });
    return param;
}
项目:AndroidAPS    文件:DetermineBasalAdapterMAJS.java   
public Object makeParam(JSONObject jsonObject, Context rhino, Scriptable scope) {
    Object param = NativeJSON.parse(rhino, scope, jsonObject.toString(), new Callable() {
        @Override
        public Object call(Context context, Scriptable scriptable, Scriptable scriptable1, Object[] objects) {
            return objects[1];
        }
    });
    return param;
}
项目:elasticshell    文件:RhinoJsonToString.java   
@Override
public String jsonToString(NativeObject json, boolean prettify) {
    Context context = Context.getCurrentContext();
    Object jsonString = NativeJSON.stringify(context, ScriptRuntime.getGlobal(context), json, null, prettify ? "  " : null);
    return jsonString.toString();
}