From ca76ea45cf5b2a76d2037227348e2b7a4d49a99f Mon Sep 17 00:00:00 2001 From: Alexey Andreev Date: Fri, 10 Mar 2023 21:19:30 +0100 Subject: [PATCH] Get rid of Gson library --- .../teavm/buildutil/PublishTeaVMPlugin.java | 9 +- classlib/build.gradle.kts | 1 - .../classlib/impl/unicode/CLDRReader.java | 281 ++++++++++-------- .../impl/unicode/NumbersJsonParser.java | 27 ++ .../teavm/backend/c/util/Deobfuscator.java | 14 +- .../backend/c/util/HeapDumpConverter.java | 16 +- .../json/JsonAllErrorVisitor.java | 4 +- .../org/teavm/common/json/JsonArrayValue.java | 35 +++ .../json/JsonArrayVisitor.java | 4 +- .../teavm/common/json/JsonBooleanValue.java | 28 ++ .../c/util => common}/json/JsonConsumer.java | 4 +- .../json/JsonErrorReporter.java | 4 +- .../org/teavm/common/json/JsonFloatValue.java | 29 ++ .../org/teavm/common/json/JsonIntValue.java | 34 +++ .../org/teavm/common/json/JsonNullValue.java | 19 ++ .../teavm/common/json/JsonNumericValue.java | 20 ++ .../teavm/common/json/JsonObjectValue.java | 45 +++ .../json/JsonObjectVisitor.java | 8 +- .../c/util => common}/json/JsonParser.java | 4 +- .../json/JsonPropertyVisitor.java | 7 +- .../teavm/common/json/JsonStringValue.java | 29 ++ .../json/JsonSyntaxException.java | 4 +- .../java/org/teavm/common/json/JsonValue.java | 30 ++ .../common/json/JsonValueParserVisitor.java | 90 ++++++ .../teavm/common/json/JsonValueProvider.java | 31 ++ .../json/JsonVisitingConsumer.java | 4 +- .../c/util => common}/json/JsonVisitor.java | 4 +- gradle/libs.versions.toml | 1 - 28 files changed, 612 insertions(+), 174 deletions(-) create mode 100644 classlib/src/main/java/org/teavm/classlib/impl/unicode/NumbersJsonParser.java rename core/src/main/java/org/teavm/{backend/c/util => common}/json/JsonAllErrorVisitor.java (95%) create mode 100644 core/src/main/java/org/teavm/common/json/JsonArrayValue.java rename core/src/main/java/org/teavm/{backend/c/util => common}/json/JsonArrayVisitor.java (92%) create mode 100644 core/src/main/java/org/teavm/common/json/JsonBooleanValue.java rename core/src/main/java/org/teavm/{backend/c/util => common}/json/JsonConsumer.java (94%) rename core/src/main/java/org/teavm/{backend/c/util => common}/json/JsonErrorReporter.java (90%) create mode 100644 core/src/main/java/org/teavm/common/json/JsonFloatValue.java create mode 100644 core/src/main/java/org/teavm/common/json/JsonIntValue.java create mode 100644 core/src/main/java/org/teavm/common/json/JsonNullValue.java create mode 100644 core/src/main/java/org/teavm/common/json/JsonNumericValue.java create mode 100644 core/src/main/java/org/teavm/common/json/JsonObjectValue.java rename core/src/main/java/org/teavm/{backend/c/util => common}/json/JsonObjectVisitor.java (80%) rename core/src/main/java/org/teavm/{backend/c/util => common}/json/JsonParser.java (99%) rename core/src/main/java/org/teavm/{backend/c/util => common}/json/JsonPropertyVisitor.java (88%) create mode 100644 core/src/main/java/org/teavm/common/json/JsonStringValue.java rename core/src/main/java/org/teavm/{backend/c/util => common}/json/JsonSyntaxException.java (94%) create mode 100644 core/src/main/java/org/teavm/common/json/JsonValue.java create mode 100644 core/src/main/java/org/teavm/common/json/JsonValueParserVisitor.java create mode 100644 core/src/main/java/org/teavm/common/json/JsonValueProvider.java rename core/src/main/java/org/teavm/{backend/c/util => common}/json/JsonVisitingConsumer.java (98%) rename core/src/main/java/org/teavm/{backend/c/util => common}/json/JsonVisitor.java (94%) diff --git a/build-logic/src/main/java/org/teavm/buildutil/PublishTeaVMPlugin.java b/build-logic/src/main/java/org/teavm/buildutil/PublishTeaVMPlugin.java index d86a2fce9..ee1721315 100644 --- a/build-logic/src/main/java/org/teavm/buildutil/PublishTeaVMPlugin.java +++ b/build-logic/src/main/java/org/teavm/buildutil/PublishTeaVMPlugin.java @@ -34,7 +34,12 @@ public abstract class PublishTeaVMPlugin implements Plugin { public void apply(Project target) { target.getPlugins().apply(PublishingPlugin.class); target.getPlugins().apply(MavenPublishPlugin.class); - target.getPlugins().apply(SigningPlugin.class); + + var publish = Boolean.parseBoolean(target.getProviders().gradleProperty("teavm.mavenCentral.publish") + .getOrElse("false")); + if (publish) { + target.getPlugins().apply(SigningPlugin.class); + } var extension = new ExtensionImpl(); target.getExtensions().add(PublishTeaVMExtension.class, EXTENSION_NAME, extension); @@ -45,8 +50,6 @@ public abstract class PublishTeaVMPlugin implements Plugin { customizePublication(target, publication, extension); }); }); - var publish = Boolean.parseBoolean(target.getProviders().gradleProperty("teavm.mavenCentral.publish") - .getOrElse("false")); if (publish) { var signing = target.getExtensions().getByType(SigningExtension.class); publishing.getPublications().configureEach(signing::sign); diff --git a/classlib/build.gradle.kts b/classlib/build.gradle.kts index 2ba504f1e..17334d8c5 100644 --- a/classlib/build.gradle.kts +++ b/classlib/build.gradle.kts @@ -29,7 +29,6 @@ dependencies { api(project(":jso:impl")) api(project(":metaprogramming:impl")) api(libs.commons.io) - api(libs.gson) api(libs.jzlib) api(libs.jodaTime) diff --git a/classlib/src/main/java/org/teavm/classlib/impl/unicode/CLDRReader.java b/classlib/src/main/java/org/teavm/classlib/impl/unicode/CLDRReader.java index 23d8208ad..910433f2e 100644 --- a/classlib/src/main/java/org/teavm/classlib/impl/unicode/CLDRReader.java +++ b/classlib/src/main/java/org/teavm/classlib/impl/unicode/CLDRReader.java @@ -15,24 +15,28 @@ */ package org.teavm.classlib.impl.unicode; -import com.google.gson.JsonElement; -import com.google.gson.JsonObject; -import com.google.gson.JsonParser; import java.io.BufferedInputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; +import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.LinkedHashMap; import java.util.LinkedHashSet; -import java.util.List; import java.util.Map; import java.util.Properties; import java.util.Set; import java.util.zip.ZipEntry; import java.util.zip.ZipInputStream; +import org.teavm.common.json.JsonObjectValue; +import org.teavm.common.json.JsonParser; +import org.teavm.common.json.JsonValue; +import org.teavm.common.json.JsonValueParserVisitor; +import org.teavm.common.json.JsonValueProvider; +import org.teavm.common.json.JsonVisitingConsumer; +import org.teavm.common.json.JsonVisitor; public class CLDRReader { private static final String[] weekdayKeys = { "sun", "mon", "tue", "wed", "thu", "fri", "sat" }; @@ -132,7 +136,7 @@ public class CLDRReader { readTimeZones(localeName, localeInfo, input); break; case "ca-gregorian.json": { - JsonObject root = (JsonObject) new JsonParser().parse(new InputStreamReader(input)); + var root = (JsonObjectValue) parse(input); readEras(localeName, localeInfo, root); readAmPms(localeName, localeInfo, root); readMonths(localeName, localeInfo, root); @@ -158,50 +162,49 @@ public class CLDRReader { } private void readLanguages(String localeCode, CLDRLocale locale, InputStream input) { - JsonObject root = (JsonObject) new JsonParser().parse(new InputStreamReader(input)); - JsonObject languagesJson = root.get("main").getAsJsonObject().get(localeCode).getAsJsonObject() - .get("localeDisplayNames").getAsJsonObject().get("languages").getAsJsonObject(); - for (Map.Entry property : languagesJson.entrySet()) { + var root = parse(input).asObject(); + var languagesJson = root.get("main").asObject().get(localeCode).asObject() + .get("localeDisplayNames").asObject().get("languages").asObject(); + for (var property : languagesJson.entrySet()) { String language = property.getKey(); if (availableLanguages.contains(language)) { - locale.languages.put(language, property.getValue().getAsString()); + locale.languages.put(language, property.getValue().asString()); } } } private void readCountries(String localeCode, CLDRLocale locale, InputStream input) { - JsonObject root = (JsonObject) new JsonParser().parse(new InputStreamReader(input)); - JsonObject countriesJson = root.get("main").getAsJsonObject().get(localeCode).getAsJsonObject() - .get("localeDisplayNames").getAsJsonObject().get("territories").getAsJsonObject(); - for (Map.Entry property : countriesJson.entrySet()) { + var root = parse(input).asObject(); + var countriesJson = root.get("main").asObject().get(localeCode).asObject() + .get("localeDisplayNames").asObject().get("territories").asObject(); + for (var property : countriesJson.entrySet()) { String country = property.getKey(); if (availableCountries.contains(country)) { - locale.territories.put(country, property.getValue().getAsString()); + locale.territories.put(country, property.getValue().asString()); } } } private void readTimeZones(String localeCode, CLDRLocale locale, InputStream input) { - JsonObject root = (JsonObject) new JsonParser().parse(new InputStreamReader(input)); - JsonObject zonesJson = root.get("main").getAsJsonObject().get(localeCode).getAsJsonObject() - .get("dates").getAsJsonObject().get("timeZoneNames").getAsJsonObject().get("zone") - .getAsJsonObject(); - List timeZones = new ArrayList<>(); - for (Map.Entry area : zonesJson.entrySet()) { - String areaName = area.getKey(); - for (Map.Entry location : area.getValue().getAsJsonObject().entrySet()) { - String locationName = location.getKey(); - JsonElement city = location.getValue().getAsJsonObject().get("exemplarCity"); + var root = parse(input).asObject(); + var zonesJson = root.get("main").asObject().get(localeCode).asObject() + .get("dates").asObject().get("timeZoneNames").asObject().get("zone") + .asObject(); + var timeZones = new ArrayList(); + for (var area : zonesJson.entrySet()) { + var areaName = area.getKey(); + for (var location : area.getValue().asObject().entrySet()) { + var locationName = location.getKey(); + var city = location.getValue().asObject().get("exemplarCity"); if (city != null) { - CLDRTimeZone tz = new CLDRTimeZone(areaName, locationName, city.getAsString()); + var tz = new CLDRTimeZone(areaName, locationName, city.asString()); timeZones.add(tz); } else { - for (Map.Entry sublocation : location.getValue() - .getAsJsonObject().entrySet()) { - city = location.getValue().getAsJsonObject().get("exemplarCity"); + for (var sublocation : location.getValue().asObject().entrySet()) { + city = location.getValue().asObject().get("exemplarCity"); if (city != null) { - CLDRTimeZone tz = new CLDRTimeZone(areaName, locationName + "/" + sublocation.getKey(), - city.getAsString()); + var tz = new CLDRTimeZone(areaName, locationName + "/" + sublocation.getKey(), + city.asString()); timeZones.add(tz); } } @@ -212,156 +215,155 @@ public class CLDRReader { } private void readCurrencies(String localeCode, CLDRLocale locale, InputStream input) { - JsonObject root = (JsonObject) new JsonParser().parse(new InputStreamReader(input)); - JsonObject currenciesJson = root.get("main").getAsJsonObject().get(localeCode).getAsJsonObject() - .get("numbers").getAsJsonObject().get("currencies").getAsJsonObject(); - for (Map.Entry currencyEntry : currenciesJson.entrySet()) { - String currencyCode = currencyEntry.getKey(); - JsonObject currencyJson = currencyEntry.getValue().getAsJsonObject(); - CLDRCurrency currency = new CLDRCurrency(); - currency.name = currencyJson.get("displayName").getAsString(); + var root = parse(input).asObject(); + var currenciesJson = root.get("main").asObject().get(localeCode).asObject() + .get("numbers").asObject().get("currencies").asObject(); + for (var currencyEntry : currenciesJson.entrySet()) { + var currencyCode = currencyEntry.getKey(); + var currencyJson = currencyEntry.getValue().asObject(); + var currency = new CLDRCurrency(); + currency.name = currencyJson.get("displayName").asString(); if (currencyJson.has("symbol")) { - currency.symbol = currencyJson.get("symbol").getAsString(); + currency.symbol = currencyJson.get("symbol").asString(); } locale.currencies.put(currencyCode, currency); } } private void readNumbers(String localeCode, CLDRLocale locale, InputStream input) { - JsonObject root = (JsonObject) new JsonParser().parse(new InputStreamReader(input)); - JsonObject numbersJson = root.get("main").getAsJsonObject().get(localeCode).getAsJsonObject() - .get("numbers").getAsJsonObject(); - String numbering = numbersJson.get("defaultNumberingSystem").getAsString(); - JsonObject symbolsJson = numbersJson.get("symbols-numberSystem-" + numbering).getAsJsonObject(); - locale.decimalData.decimalSeparator = symbolsJson.get("decimal").getAsString().charAt(0); - locale.decimalData.groupingSeparator = symbolsJson.get("group").getAsString().charAt(0); - locale.decimalData.listSeparator = symbolsJson.get("list").getAsString().charAt(0); - locale.decimalData.percent = symbolsJson.get("percentSign").getAsString().charAt(0); - locale.decimalData.minusSign = symbolsJson.get("minusSign").getAsString().charAt(0); - locale.decimalData.exponentSeparator = symbolsJson.get("exponential").getAsString(); - locale.decimalData.perMille = symbolsJson.get("perMille").getAsString().charAt(0); - locale.decimalData.infinity = symbolsJson.get("infinity").getAsString(); - locale.decimalData.nan = symbolsJson.get("nan").getAsString(); + var root = parse(input).asObject(); + var numbersJson = root.get("main").asObject().get(localeCode).asObject() + .get("numbers").asObject(); + String numbering = numbersJson.get("defaultNumberingSystem").asString(); + var symbolsJson = numbersJson.get("symbols-numberSystem-" + numbering).asObject(); + locale.decimalData.decimalSeparator = symbolsJson.get("decimal").asString().charAt(0); + locale.decimalData.groupingSeparator = symbolsJson.get("group").asString().charAt(0); + locale.decimalData.listSeparator = symbolsJson.get("list").asString().charAt(0); + locale.decimalData.percent = symbolsJson.get("percentSign").asString().charAt(0); + locale.decimalData.minusSign = symbolsJson.get("minusSign").asString().charAt(0); + locale.decimalData.exponentSeparator = symbolsJson.get("exponential").asString(); + locale.decimalData.perMille = symbolsJson.get("perMille").asString().charAt(0); + locale.decimalData.infinity = symbolsJson.get("infinity").asString(); + locale.decimalData.nan = symbolsJson.get("nan").asString(); - JsonObject numberJson = numbersJson.get("decimalFormats-numberSystem-" + numbering).getAsJsonObject(); - locale.numberFormat = numberJson.get("standard").getAsString(); + var numberJson = numbersJson.get("decimalFormats-numberSystem-" + numbering).asObject(); + locale.numberFormat = numberJson.get("standard").asString(); - JsonObject percentJson = numbersJson.get("percentFormats-numberSystem-" + numbering).getAsJsonObject(); - locale.percentFormat = percentJson.get("standard").getAsString(); + var percentJson = numbersJson.get("percentFormats-numberSystem-" + numbering).asObject(); + locale.percentFormat = percentJson.get("standard").asString(); - JsonObject currencyJson = numbersJson.get("currencyFormats-numberSystem-" + numbering).getAsJsonObject(); - locale.currencyFormat = currencyJson.get("standard").getAsString(); + var currencyJson = numbersJson.get("currencyFormats-numberSystem-" + numbering).asObject(); + locale.currencyFormat = currencyJson.get("standard").asString(); } - private void readEras(String localeCode, CLDRLocale locale, JsonObject root) { - JsonObject erasJson = root.get("main").getAsJsonObject().get(localeCode).getAsJsonObject() - .get("dates").getAsJsonObject().get("calendars").getAsJsonObject() - .get("gregorian").getAsJsonObject().get("eras").getAsJsonObject().get("eraAbbr").getAsJsonObject(); - String bc = erasJson.get("0").getAsString(); - String ac = erasJson.get("1").getAsString(); + private void readEras(String localeCode, CLDRLocale locale, JsonObjectValue root) { + var erasJson = root.get("main").asObject().get(localeCode).asObject() + .get("dates").asObject().get("calendars").asObject() + .get("gregorian").asObject().get("eras").asObject().get("eraAbbr").asObject(); + var bc = erasJson.get("0").asString(); + var ac = erasJson.get("1").asString(); locale.eras = new String[] { bc, ac }; } - private void readAmPms(String localeCode, CLDRLocale locale, JsonObject root) { - JsonObject ampmJson = root.get("main").getAsJsonObject().get(localeCode).getAsJsonObject() - .get("dates").getAsJsonObject().get("calendars").getAsJsonObject() - .get("gregorian").getAsJsonObject().get("dayPeriods").getAsJsonObject() - .get("format").getAsJsonObject().get("abbreviated").getAsJsonObject(); - String am = ampmJson.get("am").getAsString(); - String pm = ampmJson.get("pm").getAsString(); + private void readAmPms(String localeCode, CLDRLocale locale, JsonObjectValue root) { + var ampmJson = root.get("main").asObject().get(localeCode).asObject() + .get("dates").asObject().get("calendars").asObject() + .get("gregorian").asObject().get("dayPeriods").asObject() + .get("format").asObject().get("abbreviated").asObject(); + String am = ampmJson.get("am").asString(); + String pm = ampmJson.get("pm").asString(); locale.dayPeriods = new String[] { am, pm }; } - private void readMonths(String localeCode, CLDRLocale locale, JsonObject root) { - JsonObject monthsJson = root.get("main").getAsJsonObject().get(localeCode).getAsJsonObject() - .get("dates").getAsJsonObject().get("calendars").getAsJsonObject() - .get("gregorian").getAsJsonObject().get("months").getAsJsonObject() - .get("format").getAsJsonObject().get("wide").getAsJsonObject(); + private void readMonths(String localeCode, CLDRLocale locale, JsonObjectValue root) { + var monthsJson = root.get("main").asObject().get(localeCode).asObject() + .get("dates").asObject().get("calendars").asObject() + .get("gregorian").asObject().get("months").asObject() + .get("format").asObject().get("wide").asObject(); locale.months = new String[12]; for (int i = 0; i < 12; ++i) { - locale.months[i] = monthsJson.get(String.valueOf(i + 1)).getAsString(); + locale.months[i] = monthsJson.get(String.valueOf(i + 1)).asString(); } } - private void readShortMonths(String localeCode, CLDRLocale locale, JsonObject root) { - JsonObject monthsJson = root.get("main").getAsJsonObject().get(localeCode).getAsJsonObject() - .get("dates").getAsJsonObject().get("calendars").getAsJsonObject() - .get("gregorian").getAsJsonObject().get("months").getAsJsonObject() - .get("format").getAsJsonObject().get("abbreviated").getAsJsonObject(); + private void readShortMonths(String localeCode, CLDRLocale locale, JsonObjectValue root) { + var monthsJson = root.get("main").asObject().get(localeCode).asObject() + .get("dates").asObject().get("calendars").asObject() + .get("gregorian").asObject().get("months").asObject() + .get("format").asObject().get("abbreviated").asObject(); locale.shortMonths = new String[12]; for (int i = 0; i < 12; ++i) { - locale.shortMonths[i] = monthsJson.get(String.valueOf(i + 1)).getAsString(); + locale.shortMonths[i] = monthsJson.get(String.valueOf(i + 1)).asString(); } } - private void readWeekdays(String localeCode, CLDRLocale locale, JsonObject root) { - JsonObject weekdaysJson = root.get("main").getAsJsonObject().get(localeCode).getAsJsonObject() - .get("dates").getAsJsonObject().get("calendars").getAsJsonObject() - .get("gregorian").getAsJsonObject().get("days").getAsJsonObject() - .get("format").getAsJsonObject().get("wide").getAsJsonObject(); + private void readWeekdays(String localeCode, CLDRLocale locale, JsonObjectValue root) { + var weekdaysJson = root.get("main").asObject().get(localeCode).asObject() + .get("dates").asObject().get("calendars").asObject() + .get("gregorian").asObject().get("days").asObject() + .get("format").asObject().get("wide").asObject(); locale.weekdays = new String[7]; for (int i = 0; i < 7; ++i) { - locale.weekdays[i] = weekdaysJson.get(weekdayKeys[i]).getAsString(); + locale.weekdays[i] = weekdaysJson.get(weekdayKeys[i]).asString(); } } - private void readShortWeekdays(String localeCode, CLDRLocale locale, JsonObject root) { - JsonObject weekdaysJson = root.get("main").getAsJsonObject().get(localeCode).getAsJsonObject() - .get("dates").getAsJsonObject().get("calendars").getAsJsonObject() - .get("gregorian").getAsJsonObject().get("days").getAsJsonObject() - .get("format").getAsJsonObject().get("abbreviated").getAsJsonObject(); + private void readShortWeekdays(String localeCode, CLDRLocale locale, JsonObjectValue root) { + var weekdaysJson = root.get("main").asObject().get(localeCode).asObject() + .get("dates").asObject().get("calendars").asObject() + .get("gregorian").asObject().get("days").asObject() + .get("format").asObject().get("abbreviated").asObject(); locale.shortWeekdays = new String[7]; for (int i = 0; i < 7; ++i) { - locale.shortWeekdays[i] = weekdaysJson.get(weekdayKeys[i]).getAsString(); + locale.shortWeekdays[i] = weekdaysJson.get(weekdayKeys[i]).asString(); } } - private void readDateFormats(String localeCode, CLDRLocale locale, JsonObject root) { - JsonObject formatsJson = root.get("main").getAsJsonObject().get(localeCode).getAsJsonObject() - .get("dates").getAsJsonObject().get("calendars").getAsJsonObject() - .get("gregorian").getAsJsonObject().get("dateFormats").getAsJsonObject(); - locale.dateFormats = new CLDRDateFormats(formatsJson.get("short").getAsString(), - formatsJson.get("medium").getAsString(), formatsJson.get("long").getAsString(), - formatsJson.get("full").getAsString()); + private void readDateFormats(String localeCode, CLDRLocale locale, JsonObjectValue root) { + var formatsJson = root.get("main").asObject().get(localeCode).asObject() + .get("dates").asObject().get("calendars").asObject() + .get("gregorian").asObject().get("dateFormats").asObject(); + locale.dateFormats = new CLDRDateFormats(formatsJson.get("short").asString(), + formatsJson.get("medium").asString(), formatsJson.get("long").asString(), + formatsJson.get("full").asString()); } - private void readTimeFormats(String localeCode, CLDRLocale locale, JsonObject root) { - JsonObject formatsJson = root.get("main").getAsJsonObject().get(localeCode).getAsJsonObject() - .get("dates").getAsJsonObject().get("calendars").getAsJsonObject() - .get("gregorian").getAsJsonObject().get("timeFormats").getAsJsonObject(); - locale.timeFormats = new CLDRDateFormats(formatsJson.get("short").getAsString(), - formatsJson.get("medium").getAsString(), formatsJson.get("long").getAsString(), - formatsJson.get("full").getAsString()); + private void readTimeFormats(String localeCode, CLDRLocale locale, JsonObjectValue root) { + var formatsJson = root.get("main").asObject().get(localeCode).asObject() + .get("dates").asObject().get("calendars").asObject() + .get("gregorian").asObject().get("timeFormats").asObject(); + locale.timeFormats = new CLDRDateFormats(formatsJson.get("short").asString(), + formatsJson.get("medium").asString(), formatsJson.get("long").asString(), + formatsJson.get("full").asString()); } - private void readDateTimeFormats(String localeCode, CLDRLocale locale, JsonObject root) { - JsonObject formatsJson = root.get("main").getAsJsonObject().get(localeCode).getAsJsonObject() - .get("dates").getAsJsonObject().get("calendars").getAsJsonObject() - .get("gregorian").getAsJsonObject().get("dateTimeFormats").getAsJsonObject(); - locale.dateTimeFormats = new CLDRDateFormats(formatsJson.get("short").getAsString(), - formatsJson.get("medium").getAsString(), formatsJson.get("long").getAsString(), - formatsJson.get("full").getAsString()); + private void readDateTimeFormats(String localeCode, CLDRLocale locale, JsonObjectValue root) { + var formatsJson = root.get("main").asObject().get(localeCode).asObject() + .get("dates").asObject().get("calendars").asObject() + .get("gregorian").asObject().get("dateTimeFormats").asObject(); + locale.dateTimeFormats = new CLDRDateFormats(formatsJson.get("short").asString(), + formatsJson.get("medium").asString(), formatsJson.get("long").asString(), + formatsJson.get("full").asString()); } private void readWeekData(InputStream input) { - JsonObject root = (JsonObject) new JsonParser().parse(new InputStreamReader(input)); - JsonObject weekJson = root.get("supplemental").getAsJsonObject().get("weekData").getAsJsonObject(); - JsonObject minDaysJson = weekJson.get("minDays").getAsJsonObject(); - for (Map.Entry property : minDaysJson.entrySet()) { - minDaysMap.put(property.getKey(), property.getValue().getAsInt()); + var root = parse(input).asObject(); + var weekJson = root.get("supplemental").asObject().get("weekData").asObject(); + var minDaysJson = weekJson.get("minDays").asObject(); + for (var property : minDaysJson.entrySet()) { + minDaysMap.put(property.getKey(), Integer.parseInt(property.getValue().asString())); } - JsonObject firstDayJson = weekJson.get("firstDay").getAsJsonObject(); - for (Map.Entry property : firstDayJson.entrySet()) { - firstDayMap.put(property.getKey(), getNumericDay(property.getValue().getAsString())); + var firstDayJson = weekJson.get("firstDay").asObject(); + for (var property : firstDayJson.entrySet()) { + firstDayMap.put(property.getKey(), getNumericDay(property.getValue().asString())); } } private void readLikelySubtags(InputStream input) { - JsonObject root = (JsonObject) new JsonParser().parse(new InputStreamReader(input)); - JsonObject likelySubtagsJson = root.get("supplemental").getAsJsonObject().get("likelySubtags") - .getAsJsonObject(); - for (Map.Entry property : likelySubtagsJson.entrySet()) { - likelySubtags.put(property.getKey(), property.getValue().getAsString()); + var root = parse(input).asObject(); + var likelySubtagsJson = root.get("supplemental").asObject().get("likelySubtags").asObject(); + for (var property : likelySubtagsJson.entrySet()) { + likelySubtags.put(property.getKey(), property.getValue().asString()); } } @@ -420,4 +422,21 @@ public class CLDRReader { ensureInitialized(); return Collections.unmodifiableMap(likelySubtags); } + + private JsonValue parse(InputStream input) { + var provider = new JsonValueProvider(); + var visitor = JsonValueParserVisitor.create(provider); + parse(visitor, input); + return provider.getValue(); + } + + private void parse(JsonVisitor visitor, InputStream input) { + var consumer = new JsonVisitingConsumer(visitor); + var parser = new JsonParser(consumer); + try { + parser.parse(new InputStreamReader(input, StandardCharsets.UTF_8)); + } catch (IOException e) { + throw new RuntimeException(e); + } + } } diff --git a/classlib/src/main/java/org/teavm/classlib/impl/unicode/NumbersJsonParser.java b/classlib/src/main/java/org/teavm/classlib/impl/unicode/NumbersJsonParser.java new file mode 100644 index 000000000..cd485ea09 --- /dev/null +++ b/classlib/src/main/java/org/teavm/classlib/impl/unicode/NumbersJsonParser.java @@ -0,0 +1,27 @@ +/* + * Copyright 2023 Alexey Andreev. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.teavm.classlib.impl.unicode; + +import org.teavm.common.json.JsonPropertyVisitor; + +class NumbersJsonParser extends JsonPropertyVisitor { + CLDRLocale locale; + + NumbersJsonParser() { + super(true); + + } +} diff --git a/core/src/main/java/org/teavm/backend/c/util/Deobfuscator.java b/core/src/main/java/org/teavm/backend/c/util/Deobfuscator.java index 8b2d3c474..0fa067b5a 100644 --- a/core/src/main/java/org/teavm/backend/c/util/Deobfuscator.java +++ b/core/src/main/java/org/teavm/backend/c/util/Deobfuscator.java @@ -26,13 +26,13 @@ import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; -import org.teavm.backend.c.util.json.JsonAllErrorVisitor; -import org.teavm.backend.c.util.json.JsonArrayVisitor; -import org.teavm.backend.c.util.json.JsonErrorReporter; -import org.teavm.backend.c.util.json.JsonParser; -import org.teavm.backend.c.util.json.JsonPropertyVisitor; -import org.teavm.backend.c.util.json.JsonVisitingConsumer; -import org.teavm.backend.c.util.json.JsonVisitor; +import org.teavm.common.json.JsonAllErrorVisitor; +import org.teavm.common.json.JsonArrayVisitor; +import org.teavm.common.json.JsonErrorReporter; +import org.teavm.common.json.JsonParser; +import org.teavm.common.json.JsonPropertyVisitor; +import org.teavm.common.json.JsonVisitingConsumer; +import org.teavm.common.json.JsonVisitor; public class Deobfuscator { private Map callSites = new HashMap<>(); diff --git a/core/src/main/java/org/teavm/backend/c/util/HeapDumpConverter.java b/core/src/main/java/org/teavm/backend/c/util/HeapDumpConverter.java index 6e95694e7..0cd0b7cab 100644 --- a/core/src/main/java/org/teavm/backend/c/util/HeapDumpConverter.java +++ b/core/src/main/java/org/teavm/backend/c/util/HeapDumpConverter.java @@ -32,14 +32,14 @@ import java.util.Collection; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; -import org.teavm.backend.c.util.json.JsonAllErrorVisitor; -import org.teavm.backend.c.util.json.JsonArrayVisitor; -import org.teavm.backend.c.util.json.JsonErrorReporter; -import org.teavm.backend.c.util.json.JsonObjectVisitor; -import org.teavm.backend.c.util.json.JsonParser; -import org.teavm.backend.c.util.json.JsonPropertyVisitor; -import org.teavm.backend.c.util.json.JsonVisitingConsumer; -import org.teavm.backend.c.util.json.JsonVisitor; +import org.teavm.common.json.JsonAllErrorVisitor; +import org.teavm.common.json.JsonArrayVisitor; +import org.teavm.common.json.JsonErrorReporter; +import org.teavm.common.json.JsonObjectVisitor; +import org.teavm.common.json.JsonParser; +import org.teavm.common.json.JsonPropertyVisitor; +import org.teavm.common.json.JsonVisitingConsumer; +import org.teavm.common.json.JsonVisitor; public final class HeapDumpConverter { private static byte[] buffer = new byte[8]; diff --git a/core/src/main/java/org/teavm/backend/c/util/json/JsonAllErrorVisitor.java b/core/src/main/java/org/teavm/common/json/JsonAllErrorVisitor.java similarity index 95% rename from core/src/main/java/org/teavm/backend/c/util/json/JsonAllErrorVisitor.java rename to core/src/main/java/org/teavm/common/json/JsonAllErrorVisitor.java index aabf87347..7468dffc2 100644 --- a/core/src/main/java/org/teavm/backend/c/util/json/JsonAllErrorVisitor.java +++ b/core/src/main/java/org/teavm/common/json/JsonAllErrorVisitor.java @@ -1,5 +1,5 @@ /* - * Copyright 2019 Alexey Andreev. + * Copyright 2023 Alexey Andreev. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.teavm.backend.c.util.json; +package org.teavm.common.json; public class JsonAllErrorVisitor extends JsonVisitor { @Override diff --git a/core/src/main/java/org/teavm/common/json/JsonArrayValue.java b/core/src/main/java/org/teavm/common/json/JsonArrayValue.java new file mode 100644 index 000000000..50248981f --- /dev/null +++ b/core/src/main/java/org/teavm/common/json/JsonArrayValue.java @@ -0,0 +1,35 @@ +/* + * Copyright 2023 Alexey Andreev. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.teavm.common.json; + +import java.util.ArrayList; +import java.util.List; + +public class JsonArrayValue extends JsonValue { + private List elements = new ArrayList<>(); + + public int size() { + return elements.size(); + } + + public JsonValue get(int index) { + return elements.get(index); + } + + public void add(JsonValue element) { + elements.add(element); + } +} diff --git a/core/src/main/java/org/teavm/backend/c/util/json/JsonArrayVisitor.java b/core/src/main/java/org/teavm/common/json/JsonArrayVisitor.java similarity index 92% rename from core/src/main/java/org/teavm/backend/c/util/json/JsonArrayVisitor.java rename to core/src/main/java/org/teavm/common/json/JsonArrayVisitor.java index 0b556c057..a59ee8a15 100644 --- a/core/src/main/java/org/teavm/backend/c/util/json/JsonArrayVisitor.java +++ b/core/src/main/java/org/teavm/common/json/JsonArrayVisitor.java @@ -1,5 +1,5 @@ /* - * Copyright 2019 Alexey Andreev. + * Copyright 2023 Alexey Andreev. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.teavm.backend.c.util.json; +package org.teavm.common.json; public class JsonArrayVisitor extends JsonAllErrorVisitor { private JsonVisitor itemVisitor; diff --git a/core/src/main/java/org/teavm/common/json/JsonBooleanValue.java b/core/src/main/java/org/teavm/common/json/JsonBooleanValue.java new file mode 100644 index 000000000..ae7207071 --- /dev/null +++ b/core/src/main/java/org/teavm/common/json/JsonBooleanValue.java @@ -0,0 +1,28 @@ +/* + * Copyright 2023 Alexey Andreev. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.teavm.common.json; + +public class JsonBooleanValue extends JsonValue { + private boolean innerValue; + + public JsonBooleanValue(boolean value) { + this.innerValue = value; + } + + public boolean asBoolean() { + return innerValue; + } +} diff --git a/core/src/main/java/org/teavm/backend/c/util/json/JsonConsumer.java b/core/src/main/java/org/teavm/common/json/JsonConsumer.java similarity index 94% rename from core/src/main/java/org/teavm/backend/c/util/json/JsonConsumer.java rename to core/src/main/java/org/teavm/common/json/JsonConsumer.java index 79f8e45c0..d71a08971 100644 --- a/core/src/main/java/org/teavm/backend/c/util/json/JsonConsumer.java +++ b/core/src/main/java/org/teavm/common/json/JsonConsumer.java @@ -1,5 +1,5 @@ /* - * Copyright 2019 Alexey Andreev. + * Copyright 2023 Alexey Andreev. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.teavm.backend.c.util.json; +package org.teavm.common.json; public abstract class JsonConsumer { public void enterObject(JsonErrorReporter reporter) { diff --git a/core/src/main/java/org/teavm/backend/c/util/json/JsonErrorReporter.java b/core/src/main/java/org/teavm/common/json/JsonErrorReporter.java similarity index 90% rename from core/src/main/java/org/teavm/backend/c/util/json/JsonErrorReporter.java rename to core/src/main/java/org/teavm/common/json/JsonErrorReporter.java index 5db7a283e..3478a0fe0 100644 --- a/core/src/main/java/org/teavm/backend/c/util/json/JsonErrorReporter.java +++ b/core/src/main/java/org/teavm/common/json/JsonErrorReporter.java @@ -1,5 +1,5 @@ /* - * Copyright 2019 Alexey Andreev. + * Copyright 2023 Alexey Andreev. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.teavm.backend.c.util.json; +package org.teavm.common.json; public abstract class JsonErrorReporter { public abstract void error(String message); diff --git a/core/src/main/java/org/teavm/common/json/JsonFloatValue.java b/core/src/main/java/org/teavm/common/json/JsonFloatValue.java new file mode 100644 index 000000000..55dec8e93 --- /dev/null +++ b/core/src/main/java/org/teavm/common/json/JsonFloatValue.java @@ -0,0 +1,29 @@ +/* + * Copyright 2023 Alexey Andreev. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.teavm.common.json; + +public class JsonFloatValue extends JsonNumericValue { + private double number; + + public JsonFloatValue(double number) { + this.number = number; + } + + @Override + public double asNumber() { + return number; + } +} diff --git a/core/src/main/java/org/teavm/common/json/JsonIntValue.java b/core/src/main/java/org/teavm/common/json/JsonIntValue.java new file mode 100644 index 000000000..8565ba0f2 --- /dev/null +++ b/core/src/main/java/org/teavm/common/json/JsonIntValue.java @@ -0,0 +1,34 @@ +/* + * Copyright 2023 Alexey Andreev. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.teavm.common.json; + +public class JsonIntValue extends JsonNumericValue { + private long number; + + public JsonIntValue(long number) { + this.number = number; + } + + @Override + public double asNumber() { + return number; + } + + @Override + public long asIntNumber() { + return number; + } +} diff --git a/core/src/main/java/org/teavm/common/json/JsonNullValue.java b/core/src/main/java/org/teavm/common/json/JsonNullValue.java new file mode 100644 index 000000000..0d575af4b --- /dev/null +++ b/core/src/main/java/org/teavm/common/json/JsonNullValue.java @@ -0,0 +1,19 @@ +/* + * Copyright 2023 Alexey Andreev. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.teavm.common.json; + +public class JsonNullValue extends JsonValue { +} diff --git a/core/src/main/java/org/teavm/common/json/JsonNumericValue.java b/core/src/main/java/org/teavm/common/json/JsonNumericValue.java new file mode 100644 index 000000000..3aef24903 --- /dev/null +++ b/core/src/main/java/org/teavm/common/json/JsonNumericValue.java @@ -0,0 +1,20 @@ +/* + * Copyright 2023 Alexey Andreev. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.teavm.common.json; + +public abstract class JsonNumericValue extends JsonValue { + public abstract double asNumber(); +} diff --git a/core/src/main/java/org/teavm/common/json/JsonObjectValue.java b/core/src/main/java/org/teavm/common/json/JsonObjectValue.java new file mode 100644 index 000000000..bd0481388 --- /dev/null +++ b/core/src/main/java/org/teavm/common/json/JsonObjectValue.java @@ -0,0 +1,45 @@ +/* + * Copyright 2023 Alexey Andreev. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.teavm.common.json; + +import java.util.HashMap; +import java.util.Map; +import java.util.Set; + +public class JsonObjectValue extends JsonValue { + private Map properties = new HashMap<>(); + + public void put(String name, JsonValue value) { + properties.put(name, value); + } + + public JsonValue get(String name) { + return properties.get(name); + } + + public boolean has(String name) { + return properties.containsKey(name); + } + + public Set> entrySet() { + return properties.entrySet(); + } + + @Override + public JsonObjectValue asObject() { + return this; + } +} diff --git a/core/src/main/java/org/teavm/backend/c/util/json/JsonObjectVisitor.java b/core/src/main/java/org/teavm/common/json/JsonObjectVisitor.java similarity index 80% rename from core/src/main/java/org/teavm/backend/c/util/json/JsonObjectVisitor.java rename to core/src/main/java/org/teavm/common/json/JsonObjectVisitor.java index 08a423a7c..20e016e78 100644 --- a/core/src/main/java/org/teavm/backend/c/util/json/JsonObjectVisitor.java +++ b/core/src/main/java/org/teavm/common/json/JsonObjectVisitor.java @@ -1,5 +1,5 @@ /* - * Copyright 2019 Alexey Andreev. + * Copyright 2023 Alexey Andreev. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -13,12 +13,12 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.teavm.backend.c.util.json; +package org.teavm.common.json; public class JsonObjectVisitor extends JsonVisitor { - private JsonPropertyVisitor propertyVisitor; + private JsonVisitor propertyVisitor; - public JsonObjectVisitor(JsonPropertyVisitor propertyVisitor) { + public JsonObjectVisitor(JsonVisitor propertyVisitor) { this.propertyVisitor = propertyVisitor; } diff --git a/core/src/main/java/org/teavm/backend/c/util/json/JsonParser.java b/core/src/main/java/org/teavm/common/json/JsonParser.java similarity index 99% rename from core/src/main/java/org/teavm/backend/c/util/json/JsonParser.java rename to core/src/main/java/org/teavm/common/json/JsonParser.java index 7d3ded375..be3a3b3e4 100644 --- a/core/src/main/java/org/teavm/backend/c/util/json/JsonParser.java +++ b/core/src/main/java/org/teavm/common/json/JsonParser.java @@ -1,5 +1,5 @@ /* - * Copyright 2019 Alexey Andreev. + * Copyright 2023 Alexey Andreev. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.teavm.backend.c.util.json; +package org.teavm.common.json; import java.io.IOException; import java.io.Reader; diff --git a/core/src/main/java/org/teavm/backend/c/util/json/JsonPropertyVisitor.java b/core/src/main/java/org/teavm/common/json/JsonPropertyVisitor.java similarity index 88% rename from core/src/main/java/org/teavm/backend/c/util/json/JsonPropertyVisitor.java rename to core/src/main/java/org/teavm/common/json/JsonPropertyVisitor.java index 0dd9696e8..03b25b611 100644 --- a/core/src/main/java/org/teavm/backend/c/util/json/JsonPropertyVisitor.java +++ b/core/src/main/java/org/teavm/common/json/JsonPropertyVisitor.java @@ -1,5 +1,5 @@ /* - * Copyright 2019 Alexey Andreev. + * Copyright 2023 Alexey Andreev. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.teavm.backend.c.util.json; +package org.teavm.common.json; import java.util.HashMap; import java.util.Map; @@ -26,8 +26,9 @@ public class JsonPropertyVisitor extends JsonAllErrorVisitor { this.skipNonExistentProperties = skipNonExistentProperties; } - public void addProperty(String propertyName, JsonVisitor visitor) { + public JsonPropertyVisitor addProperty(String propertyName, JsonVisitor visitor) { properties.put(propertyName, visitor); + return this; } @Override diff --git a/core/src/main/java/org/teavm/common/json/JsonStringValue.java b/core/src/main/java/org/teavm/common/json/JsonStringValue.java new file mode 100644 index 000000000..0ebe808c6 --- /dev/null +++ b/core/src/main/java/org/teavm/common/json/JsonStringValue.java @@ -0,0 +1,29 @@ +/* + * Copyright 2023 Alexey Andreev. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.teavm.common.json; + +public class JsonStringValue extends JsonValue { + private String text; + + public JsonStringValue(String text) { + this.text = text; + } + + @Override + public String asString() { + return text; + } +} diff --git a/core/src/main/java/org/teavm/backend/c/util/json/JsonSyntaxException.java b/core/src/main/java/org/teavm/common/json/JsonSyntaxException.java similarity index 94% rename from core/src/main/java/org/teavm/backend/c/util/json/JsonSyntaxException.java rename to core/src/main/java/org/teavm/common/json/JsonSyntaxException.java index 2b093a50a..4ea7c63a3 100644 --- a/core/src/main/java/org/teavm/backend/c/util/json/JsonSyntaxException.java +++ b/core/src/main/java/org/teavm/common/json/JsonSyntaxException.java @@ -1,5 +1,5 @@ /* - * Copyright 2019 Alexey Andreev. + * Copyright 2023 Alexey Andreev. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.teavm.backend.c.util.json; +package org.teavm.common.json; public class JsonSyntaxException extends RuntimeException { private final int lineNumber; diff --git a/core/src/main/java/org/teavm/common/json/JsonValue.java b/core/src/main/java/org/teavm/common/json/JsonValue.java new file mode 100644 index 000000000..c0f8cee9c --- /dev/null +++ b/core/src/main/java/org/teavm/common/json/JsonValue.java @@ -0,0 +1,30 @@ +/* + * Copyright 2023 Alexey Andreev. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.teavm.common.json; + +public abstract class JsonValue { + public JsonObjectValue asObject() { + throw new IllegalStateException(); + } + + public String asString() { + throw new IllegalStateException(); + } + + public long asIntNumber() { + throw new IllegalStateException(); + } +} diff --git a/core/src/main/java/org/teavm/common/json/JsonValueParserVisitor.java b/core/src/main/java/org/teavm/common/json/JsonValueParserVisitor.java new file mode 100644 index 000000000..344fc71df --- /dev/null +++ b/core/src/main/java/org/teavm/common/json/JsonValueParserVisitor.java @@ -0,0 +1,90 @@ +/* + * Copyright 2023 Alexey Andreev. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.teavm.common.json; + +import java.util.function.Consumer; + +public abstract class JsonValueParserVisitor extends JsonAllErrorVisitor { + public abstract void consume(JsonValue value); + + public static JsonValueParserVisitor create(Consumer consumer) { + return new JsonValueParserVisitor() { + @Override + public void consume(JsonValue value) { + consumer.accept(value); + } + }; + } + + @Override + public JsonVisitor object(JsonErrorReporter reporter) { + var jsonObject = new JsonObjectValue(); + consume(jsonObject); + return new JsonAllErrorVisitor() { + @Override + public JsonVisitor property(JsonErrorReporter reporter, String name) { + return new JsonValueParserVisitor() { + @Override + public void consume(JsonValue value) { + jsonObject.put(name, value); + } + }; + } + }; + } + + @Override + public JsonVisitor array(JsonErrorReporter reporter) { + var jsonArray = new JsonArrayValue(); + consume(jsonArray); + return new JsonAllErrorVisitor() { + @Override + public JsonVisitor array(JsonErrorReporter reporter) { + return new JsonValueParserVisitor() { + @Override + public void consume(JsonValue value) { + jsonArray.add(value); + } + }; + } + }; + } + + @Override + public void stringValue(JsonErrorReporter reporter, String value) { + consume(new JsonStringValue(value)); + } + + @Override + public void intValue(JsonErrorReporter reporter, long value) { + consume(new JsonIntValue(value)); + } + + @Override + public void floatValue(JsonErrorReporter reporter, double value) { + consume(new JsonFloatValue(value)); + } + + @Override + public void nullValue(JsonErrorReporter reporter) { + consume(new JsonNullValue()); + } + + @Override + public void booleanValue(JsonErrorReporter reporter, boolean value) { + consume(new JsonBooleanValue(value)); + } +} diff --git a/core/src/main/java/org/teavm/common/json/JsonValueProvider.java b/core/src/main/java/org/teavm/common/json/JsonValueProvider.java new file mode 100644 index 000000000..d20b91251 --- /dev/null +++ b/core/src/main/java/org/teavm/common/json/JsonValueProvider.java @@ -0,0 +1,31 @@ +/* + * Copyright 2023 Alexey Andreev. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.teavm.common.json; + +import java.util.function.Consumer; + +public class JsonValueProvider implements Consumer { + private JsonValue value; + + public JsonValue getValue() { + return value; + } + + @Override + public void accept(JsonValue jsonValue) { + value = jsonValue; + } +} diff --git a/core/src/main/java/org/teavm/backend/c/util/json/JsonVisitingConsumer.java b/core/src/main/java/org/teavm/common/json/JsonVisitingConsumer.java similarity index 98% rename from core/src/main/java/org/teavm/backend/c/util/json/JsonVisitingConsumer.java rename to core/src/main/java/org/teavm/common/json/JsonVisitingConsumer.java index 3189de26c..85b4b533a 100644 --- a/core/src/main/java/org/teavm/backend/c/util/json/JsonVisitingConsumer.java +++ b/core/src/main/java/org/teavm/common/json/JsonVisitingConsumer.java @@ -1,5 +1,5 @@ /* - * Copyright 2019 Alexey Andreev. + * Copyright 2023 Alexey Andreev. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.teavm.backend.c.util.json; +package org.teavm.common.json; import java.util.ArrayDeque; import java.util.Deque; diff --git a/core/src/main/java/org/teavm/backend/c/util/json/JsonVisitor.java b/core/src/main/java/org/teavm/common/json/JsonVisitor.java similarity index 94% rename from core/src/main/java/org/teavm/backend/c/util/json/JsonVisitor.java rename to core/src/main/java/org/teavm/common/json/JsonVisitor.java index 87c825565..4b27f8995 100644 --- a/core/src/main/java/org/teavm/backend/c/util/json/JsonVisitor.java +++ b/core/src/main/java/org/teavm/common/json/JsonVisitor.java @@ -1,5 +1,5 @@ /* - * Copyright 2019 Alexey Andreev. + * Copyright 2023 Alexey Andreev. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.teavm.backend.c.util.json; +package org.teavm.common.json; public abstract class JsonVisitor { public JsonVisitor object(JsonErrorReporter reporter) { diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 8475cfc85..d2e1a4ef4 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -30,7 +30,6 @@ asm-analysis = { module = "org.ow2.asm:asm-analysis", version.ref = "asm" } jackson-annotations = { module = "com.fasterxml.jackson.core:jackson-annotations", version.ref = "jackson" } jackson-databind = { module = "com.fasterxml.jackson.core:jackson-databind", version.ref = "jackson" } -gson = "com.google.code.gson:gson:2.10.1" jzlib = "com.jcraft:jzlib:1.1.3" jodaTime = "joda-time:joda-time:2.12.2" htmlunit = "net.sourceforge.htmlunit:htmlunit:2.70.0"