Skip to content

Commit

Permalink
Avoid simple-json as it produces broken json
Browse files Browse the repository at this point in the history
Signed-off-by: Paulo Lopes <pmlopes@gmail.com>
  • Loading branch information
pmlopes committed Dec 3, 2020
1 parent ab19bb4 commit 47367f0
Show file tree
Hide file tree
Showing 26 changed files with 5,195 additions and 93 deletions.
2 changes: 1 addition & 1 deletion docs/advanced/logging.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ configuration is:

```properties
handlers=java.util.logging.ConsoleHandler
java.util.logging.ConsoleHandler.formatter=io.reactiverse.es4x.jul.ES4XFormatter
java.util.logging.ConsoleHandler.formatter=io.reactiverse.es4x.jul.ANSIFormatter
java.util.logging.ConsoleHandler.level=FINEST

.level=INFO
Expand Down
2 changes: 1 addition & 1 deletion docs/es/advanced/logging.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ La configuracion por defecto es:

```properties
handlers=java.util.logging.ConsoleHandler
java.util.logging.ConsoleHandler.formatter=io.reactiverse.es4x.jul.ES4XFormatter
java.util.logging.ConsoleHandler.formatter=io.reactiverse.es4x.jul.ANSIFormatter
java.util.logging.ConsoleHandler.level=FINEST

.level=INFO
Expand Down
2 changes: 1 addition & 1 deletion docs/gr/advanced/logging.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

```properties
handlers=java.util.logging.ConsoleHandler
java.util.logging.ConsoleHandler.formatter=io.reactiverse.es4x.jul.ES4XFormatter
java.util.logging.ConsoleHandler.formatter=io.reactiverse.es4x.jul.ANSIFormatter
java.util.logging.ConsoleHandler.level=FINEST

.level=INFO
Expand Down
2 changes: 1 addition & 1 deletion docs/pl/advanced/logging.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ to:

```properties
handlers=java.util.logging.ConsoleHandler
java.util.logging.ConsoleHandler.formatter=io.reactiverse.es4x.jul.ES4XFormatter
java.util.logging.ConsoleHandler.formatter=io.reactiverse.es4x.jul.ANSIFormatter
java.util.logging.ConsoleHandler.level=FINEST

.level=INFO
Expand Down
2 changes: 1 addition & 1 deletion docs/ru/advanced/logging.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

```properties
handlers=java.util.logging.ConsoleHandler
java.util.logging.ConsoleHandler.formatter=io.reactiverse.es4x.jul.ES4XFormatter
java.util.logging.ConsoleHandler.formatter=io.reactiverse.es4x.jul.ANSIFormatter
java.util.logging.ConsoleHandler.level=FINEST

.level=INFO
Expand Down
4 changes: 2 additions & 2 deletions es4x/src/main/java/io/reactiverse/es4x/ECMAEngine.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import io.netty.buffer.Unpooled;
import io.reactiverse.es4x.impl.JSObjectMessageCodec;
import io.reactiverse.es4x.impl.VertxFileSystem;
import io.reactiverse.es4x.jul.ES4XFormatter;
import io.reactiverse.es4x.jul.ANSIFormatter;
import io.vertx.core.Future;
import io.vertx.core.Promise;
import io.vertx.core.Vertx;
Expand Down Expand Up @@ -97,7 +97,7 @@ public ECMAEngine(Vertx vertx) {
this.vertx = vertx;
final Handler logHandler = new ConsoleHandler();
// customize the formatter
logHandler.setFormatter(new ES4XFormatter());
logHandler.setFormatter(new ANSIFormatter());
// build it
this.engine = Engine.newBuilder()
.logHandler(logHandler)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,29 @@

import static java.util.logging.Level.*;

public class ES4XFormatter extends Formatter {
public class ANSIFormatter extends Formatter {

// the user is not explicitly disable ANSI colors
private final boolean colors = !Boolean.getBoolean("es4x.bare");
// are ANSI colors allowed?
private static final boolean colors;

static {
String term = System.getenv("TERM");
if (term != null) {
term = term.toLowerCase();
colors =
// this is where the most common config will be on unices
term.equals("xterm-color")
// however as there are lots of terminal emulators, it seems
// safer to look up for the suffix "-256color" as it covers:
// vte, linux, tmux, screen, putty, rxvt, nsterm, ...
|| term.endsWith("-256color");
} else {
// there's no env variable (we're running either embedded (no shell)
// or on a OS that doesn't set the TERM variable (Windows maybe)
// in this case rely on the system property to DISABLE the colors.
colors = !Boolean.getBoolean("es4x.bare");
}
}

@Override
public synchronized String format(LogRecord record) {
Expand Down Expand Up @@ -79,13 +98,13 @@ public synchronized String format(LogRecord record) {

private static String prefix(Level l) {
if (SEVERE.equals(l)) {
return "\u001B[1m\u001B[31m";
return "\u001B[1m\u001B[31m";
}
if (WARNING.equals(l)) {
return "\u001B[1m\u001B[33m";
return "\u001B[1m\u001B[33m";
}
if (INFO.equals(l)) {
return "";
return "";
}
if (CONFIG.equals(l)) {
return "\u001B[1m\u001B[34m";
Expand All @@ -97,7 +116,7 @@ private static String prefix(Level l) {
return "\u001B[1m\u001B[94m";
}
if (FINEST.equals(l)) {
return "\u001B[94m";
return "\u001B[94m";
}

return "[" + l.getName().toUpperCase() + "] ";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
# You may elect to redistribute this code under either of these licenses.
#
handlers=java.util.logging.ConsoleHandler
java.util.logging.ConsoleHandler.formatter=io.reactiverse.es4x.jul.ES4XFormatter
java.util.logging.ConsoleHandler.formatter=io.reactiverse.es4x.jul.ANSIFormatter
java.util.logging.ConsoleHandler.level=FINEST

.level=INFO
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
# You may elect to redistribute this code under either of these licenses.
#
handlers=java.util.logging.ConsoleHandler
java.util.logging.ConsoleHandler.formatter=io.reactiverse.es4x.jul.ES4XFormatter
java.util.logging.ConsoleHandler.formatter=io.reactiverse.es4x.jul.ANSIFormatter
java.util.logging.ConsoleHandler.level=FINEST

.level=INFO
Expand Down
6 changes: 0 additions & 6 deletions pm/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
<maven.version>3.6.3</maven.version>
<slf4j.version>1.7.30</slf4j.version>
<asm.version>9.0</asm.version>
<gson.version>2.8.6</gson.version>
</properties>

<dependencies>
Expand All @@ -30,11 +29,6 @@
<version>${stack.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.github.cliftonlabs</groupId>
<artifactId>json-simple</artifactId>
<version>3.1.1</version>
</dependency>
<dependency>
<groupId>org.ow2.asm</groupId>
<artifactId>asm</artifactId>
Expand Down
41 changes: 41 additions & 0 deletions pm/src/main/java/io/reactiverse/es4x/cli/VertxPatch.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,44 @@
/*
* Copyright 2019 Red Hat, Inc.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* and Apache License v2.0 which accompanies this distribution.
*
* The Eclipse Public License is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* The Apache License v2.0 is available at
* http://www.opensource.org/licenses/apache2.0.php
*
* You may elect to redistribute this code under either of these licenses.
*/
package io.reactiverse.es4x.cli;

import io.reactiverse.es4x.asm.FutureBaseVisitor;
import io.reactiverse.es4x.asm.JsonArrayVisitor;
import io.reactiverse.es4x.asm.JsonObjectVisitor;
import io.reactiverse.es4x.commands.Resolver;
import io.reactiverse.es4x.commands.Versions;
import org.eclipse.aether.artifact.Artifact;

import java.io.*;
import java.util.Collections;
import java.util.Scanner;
import java.util.jar.JarEntry;
import java.util.jar.JarInputStream;

/**
* Internal tooling to ASM patch the vert.x core classes in order to allow JS interop. The patch involves:
*
* <ul>
* <li>JsonObject - to allow using the Java type as a pure JSON object in JavaScript</li>
* <li>JsonArray - to allow using the Java type as a pure JSON object in JavaScript</li>
* <li>FutureBase - to allow using the Java type as a pure Thenable (async/await and Promise APIs) in JavaScript</li>
* </ul>
*
* Usage: {@code java io.reactiverse.es4x.cli.VertxPatch [vertx version] [target]}
*/
public class VertxPatch {

public static void main(String[] args) throws IOException {
Expand All @@ -19,6 +47,19 @@ public static void main(String[] args) throws IOException {
String _target;

switch (args.length) {
case 0:
// load the versions from vertx if possible
try (InputStream is = Versions.class.getClassLoader().getResourceAsStream("META-INF/vertx/vertx-version.txt")) {
if (is != null) {
Scanner scanner = (new Scanner(is, "UTF-8")).useDelimiter("\\A");
if (scanner.hasNext()) {
_version = scanner.next().trim();
_target = "target";
break;
}
}
}
throw new RuntimeException("Missing 'vert-core' from the classpath (cannot guess the right version)");
case 1:
if (args[0] == null) {
throw new RuntimeException("Missing vertx-core version (e.g.: 4.0.0)");
Expand Down
24 changes: 12 additions & 12 deletions pm/src/main/java/io/reactiverse/es4x/commands/Install.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@
*/
package io.reactiverse.es4x.commands;

import com.github.cliftonlabs.json_simple.JsonArray;
import com.github.cliftonlabs.json_simple.JsonObject;
import io.reactiverse.es4x.cli.CmdLineParser;
import io.reactiverse.es4x.asm.FutureBaseVisitor;
import io.reactiverse.es4x.asm.JsonArrayVisitor;
import io.reactiverse.es4x.asm.JsonObjectVisitor;
import io.reactiverse.es4x.cli.GraalVMVersion;
import org.eclipse.aether.artifact.Artifact;
import org.json.JSONArray;
import org.json.JSONObject;

import java.io.*;
import java.nio.charset.StandardCharsets;
Expand Down Expand Up @@ -151,9 +151,9 @@ public void setDestination(String dest) {

private void processPackageJson(File json, Set<String> dependencies) throws IOException {
if (json.exists()) {
JsonObject npm = JSON.parse(json);
if (npm.containsKey("maven")) {
final JsonObject maven = (JsonObject) npm.get("maven");
JSONObject npm = JSON.parseObject(json);
if (npm.has("maven")) {
final JSONObject maven = npm.getJSONObject("maven");
// add this dependency
dependencies.add(maven.get("groupId") + ":" + maven.get("artifactId") + ":" + maven.get("version"));
}
Expand All @@ -162,8 +162,8 @@ private void processPackageJson(File json, Set<String> dependencies) throws IOEx
case ALL:
case PROD:
case PRODUCTION:
if (npm.containsKey("mvnDependencies")) {
final JsonArray maven = (JsonArray) npm.get("mvnDependencies");
if (npm.has("mvnDependencies")) {
final JSONArray maven = npm.getJSONArray("mvnDependencies");
for (Object el : maven) {
// add this dependency
dependencies.add((String) el);
Expand All @@ -175,8 +175,8 @@ private void processPackageJson(File json, Set<String> dependencies) throws IOEx
case ALL:
case DEV:
case DEVELOPMENT:
if (npm.containsKey("mvnDevDependencies")) {
final JsonArray maven = (JsonArray) npm.get("mvnDevDependencies");
if (npm.has("mvnDevDependencies")) {
final JSONArray maven = npm.getJSONArray("mvnDevDependencies");
for (Object el : maven) {
// add this dependency
dependencies.add((String) el);
Expand Down Expand Up @@ -412,13 +412,13 @@ private void createLauncher(Collection<String> artifacts) {

if (json.exists()) {
try {
JsonObject npm = JSON.parse(json);
JSONObject npm = JSON.parseObject(json);
// default main script
String main = ".";
String verticleFactory = "js";

// if package json declares a different main, then it shall be used
if (npm.containsKey("main")) {
if (npm.has("main")) {
main = (String) npm.get("main");
// allow main to be a mjs
if (main != null && main.endsWith(".mjs")) {
Expand All @@ -427,7 +427,7 @@ private void createLauncher(Collection<String> artifacts) {
}

// if package json declares a different main, then it shall be used
if (npm.containsKey("module")) {
if (npm.has("module")) {
main = (String) npm.get("module");
verticleFactory = "mjs";
}
Expand Down
32 changes: 18 additions & 14 deletions pm/src/main/java/io/reactiverse/es4x/commands/JSON.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,31 +15,35 @@
*/
package io.reactiverse.es4x.commands;

import com.github.cliftonlabs.json_simple.JsonException;
import com.github.cliftonlabs.json_simple.Jsoner;
import org.json.JSONArray;
import org.json.JSONObject;

import java.io.*;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;

/**
* Utility class to handle JSON read/write
*/
public class JSON {

@SuppressWarnings("unchecked")
static <T> T parse(File in) throws IOException {
try (Reader fileReader = new FileReader(in)) {
return (T) Jsoner.deserialize(fileReader);
} catch (JsonException | ClassCastException e) {
throw new IOException(e);
static JSONObject parseObject(File in) throws IOException {
return new JSONObject(new String(Files.readAllBytes(in.toPath()), StandardCharsets.UTF_8));
}

static JSONArray parseArray(File in) throws IOException {
return new JSONArray(new String(Files.readAllBytes(in.toPath()), StandardCharsets.UTF_8));
}

static void encodeObject(File file, JSONObject json) throws IOException {
try (OutputStream out = new FileOutputStream(file)) {
out.write(json.toString(2).getBytes(StandardCharsets.UTF_8));
}
}

static void encode(File file, Object json) throws IOException {
try (Writer writer = new FileWriter(file)) {
Jsoner.prettyPrint(new StringReader(Jsoner.serialize(json)), writer, " ", System.lineSeparator());
writer.flush();
} catch (JsonException e) {
throw new IOException(e);
static void encodeArray(File file, JSONArray json) throws IOException {
try (OutputStream out = new FileOutputStream(file)) {
out.write(json.toString(2).getBytes(StandardCharsets.UTF_8));
}
}
}
6 changes: 3 additions & 3 deletions pm/src/main/java/io/reactiverse/es4x/commands/Project.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
*/
package io.reactiverse.es4x.commands;

import com.github.cliftonlabs.json_simple.JsonObject;
import io.reactiverse.es4x.cli.CmdLineParser;
import org.json.JSONObject;

import java.io.File;
import java.io.IOException;
Expand Down Expand Up @@ -143,7 +143,7 @@ public void run() {
}
}

JsonObject npm = JSON.parse(file);
JSONObject npm = JSON.parseObject(file);
String name = null;

// this was a new project, either derive the name from the cwd or set to "unnamed"
Expand All @@ -162,7 +162,7 @@ public void run() {

npm.put("name", name);

JSON.encode(file, npm);
JSON.encodeObject(file, npm);

} catch (IOException e) {
fatal(e.getMessage());
Expand Down
Loading

0 comments on commit 47367f0

Please sign in to comment.