Skip to content

Commit

Permalink
Make JSON parser handle longs explicitly separately from ints
Browse files Browse the repository at this point in the history
  • Loading branch information
boxbeam committed Feb 28, 2024
1 parent 834c42e commit 0588c15
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 6 deletions.
2 changes: 1 addition & 1 deletion res/plugin.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: RedLib
main: redempt.redlib.RedLib
version: 2024-02-13 00:50
version: 2024-02-28 02:53
author: Redempt
api-version: 1.13
load: STARTUP
21 changes: 16 additions & 5 deletions src/redempt/redlib/json/JSONParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,13 @@ public class JSONParser {
}

public static String toJSONString(Object o) {
return o instanceof String ? '"' + ((String) o).replace("\\", "\\\\").replace("\"", "\\\"") + '"' : o.toString();
if (o instanceof String) {
return ((String) o).replace("\\", "\\\\").replace("\"", "\\\"");
}
if (o instanceof Long) {
return o + "L";
}
return o.toString();
}

public static JSONMap parseMap(String json) {
Expand Down Expand Up @@ -60,7 +66,7 @@ private void whitespace() {
}
}

private long integer() {
private Number integer() {
boolean negative = peek() == '-';
if (negative) {
pos++;
Expand All @@ -70,14 +76,19 @@ private long integer() {
out *= 10;
out += advance() - '0';
}
return negative ? -out : out;
long number = negative ? -out : out;
if (peek() == 'L') {
advance();
return number;
}
return (int) number;
}

private double decimal(long first) {
assertChar('.');
int start = pos;
long second = integer();
double decimal = second * Math.pow(0.1, pos - start);
Number second = integer();
double decimal = second.doubleValue() * Math.pow(0.1, pos - start);
return first < 0 ? first - decimal : decimal + first;
}

Expand Down

0 comments on commit 0588c15

Please sign in to comment.