@NbBundle.Messages({ "LBL_InvalidRC=Invalid .jshintrc", "# {0} - Linted JavaScript file", "# {1} - RC file", "DESC_InvalidRC=File {0} was linted with default configuration because its related {1} is not valid json", "ICON_InvalidRC=" }) private Scriptable getConfig(Context cx, FileObject fo) throws ParseException, IOException { JsonParser parser = new JsonParser(cx, scope); FileObject config = findConfig(fo); if (config == null) { return cx.newObject(scope); } String json = config.asText(); try { return (Scriptable) parser.parseValue(json); } catch (ParseException ex) { JTextArea text = new JTextArea(Bundle.DESC_InvalidRC(fo.getPath(), config.getPath())); NotificationDisplayer.getDefault().notify(Bundle.LBL_InvalidRC(), new ImageIcon(Bundle.ICON_InvalidRC()), text, text, NotificationDisplayer.Priority.NORMAL); return cx.newObject(scope); } }
private static Object parse(Context cx, Scriptable scope, String jtext) { try { return new JsonParser(cx, scope).parseValue(jtext); } catch (JsonParser.ParseException ex) { throw ScriptRuntime.constructError("SyntaxError", ex.getMessage()); } }
private Object parseJson(final Context context, final Scriptable scope, final InputStream json) throws IOException, TransformationException { try { final char buf[] = new char[4096]; int read = -1; final StringBuilder builder = new StringBuilder(); try (final Reader reader = new InputStreamReader(json, UTF_8)) { while ((read = reader.read(buf)) > -1) { builder.append(buf, 0, read); } } return new JsonParser(context, scope).parseValue(builder.toString()); } catch(final JsonParser.ParseException e) { throw new TransformationException("Unable to parse JSON", e); } }
@Override public void putJson(final String varName, final String json) { runWithContext(new RhinoCallable<Object, RuntimeException>() { @Override protected Object doCall(Context cx, Scriptable scope) throws RuntimeException { try { Object obj = new JsonParser(cx, scope).parseValue(json); scope.put(varName, scope, obj); return null; } catch (ParseException e) { throw Throwables.propagate(e); } } }); }
@Override public Object coerce(Object obj, Type type) { Context cx = Context.enter(); try { if (obj instanceof String) { return new JsonParser(cx, cx.initStandardObjects()).parseValue((String) obj); } return obj; } catch (ParseException e) { // just assume it's actually a string return obj; } }
@Override public Object stringToJson(String json) { Context context = Context.getCurrentContext(); try { return new JsonParser(context, ScriptRuntime.getGlobal(context)).parseValue(json); } catch (JsonParser.ParseException e) { logger.error("Unable to create a json object from string {}", json, e); throw new IllegalArgumentException(e.getMessage(), e); } }
@Before public void setUp() { cx = Context.enter(); parser = new JsonParser(cx, cx.initStandardObjects()); }