001/*
002MIT License
003
004Copyright (c) 2020 FBSQL Team
005
006Permission is hereby granted, free of charge, to any person obtaining a copy
007of this software and associated documentation files (the "Software"), to deal
008in the Software without restriction, including without limitation the rights
009to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
010copies of the Software, and to permit persons to whom the Software is
011furnished to do so, subject to the following conditions:
012
013The above copyright notice and this permission notice shall be included in all
014copies or substantial portions of the Software.
015
016THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
017IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
018FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
019AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
020LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
021OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
022SOFTWARE.
023
024Home:   https://fbsql.github.io
025E-Mail: fbsql.team@gmail.com
026*/
027
028package org.fbsql.servlet;
029
030import java.util.Arrays;
031import java.util.LinkedHashMap;
032import java.util.List;
033import java.util.Map;
034
035import org.mozilla.javascript.Callable;
036import org.mozilla.javascript.Context;
037import org.mozilla.javascript.NativeArray;
038import org.mozilla.javascript.NativeJSON;
039import org.mozilla.javascript.Scriptable;
040
041public class RhinoUtils {
042
043        /**
044         * Convert JSON string to Object
045         *
046         * @param json
047         * @return
048         */
049        private static Object asObject(String json) {
050                Context context = Context.enter();
051                context.setLanguageVersion(Context.VERSION_ES6);
052                context.setOptimizationLevel(9); // Rhino optimization: https://developer.mozilla.org/en-US/docs/Mozilla/Projects/Rhino/Optimization
053                context.getWrapFactory().setJavaPrimitiveWrap(false);
054                Scriptable scriptable = context.initStandardObjects();
055                Object     object     = NativeJSON.parse(context, scriptable, json, new Callable() {
056                                                                        @Override
057                                                                        public Object call(Context arg0, Scriptable arg1, Scriptable arg2, Object[] objects) {
058                                                                                Object obj = objects[1];
059                                                                                if (obj instanceof Map)
060                                                                                        return new LinkedHashMap<>((Map) obj);
061                                                                                if (obj instanceof NativeArray) {
062                                                                                        NativeArray nativeArray = (NativeArray) obj;
063                                                                                        return Arrays.asList(nativeArray.toArray());
064                                                                                }
065                                                                                return obj;
066                                                                        }
067                                                                });
068                context.exit();
069                return object;
070        }
071
072        /**
073         * Convert JSON string to List
074         *
075         * @param json
076         * @return
077         */
078        public static List asList(String json) {
079                return (List) asObject(json);
080        }
081
082        /**
083         * Convert JSON string to Map<String, Object>
084         *
085         * @param json
086         * @return
087         */
088        public static Map<String, Object> asMap(String json) {
089                return (Map<String, Object>) asObject(json);
090        }
091}
092
093/*
094Please contact FBSQL Team by E-Mail fbsql.team@gmail.com
095or visit https://fbsql.github.io if you need additional
096information or have any questions.
097*/
098
099/* EOF */