Behavior of JSONObject constructor trimming 0-led integers and converting them to int type instead of String type
まだ誰も着手していません。
評価
調査の方向性
Start at the JSONObject constructor behavior changed in 20231013 and review the linked #783 diff. Reproduce the example with a leading-zero personId, then inspect how getString("personId") handles the resulting value. The issue does not define an agreed replacement behavior, so completion depends on resolving that compatibility question and adding coverage.
索引モデルが issue の本文から書いたものです。
説明
Hello there! I am curious about the new behavior introduced in 20231013 (ref #783), where the JSONObject constructor trims 0-led integers and converts them to int type instead of String type (which was the type in 20230618).
While I can understand the rationale behind trimming, the change in behavior could be problematic for things like IDs with leading 0s, which may have previously relied on the JSONObject constructor preserving the value as String type, only to have it converted to int, and thus failing a subsequent call to JSONObject getString("id") because of type mismatch.
Comparing with other Java JSON libraries, org.json seems to behave in a way that's potentially unsafe as it transforms the input and therefore "hides" the error, versus the other libraries which take a safer approach by throwing an error on input (javax.json, Jackson) or treating the input as "pseudo"-json and letting the getter decide the type (Gson).
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.gson.JsonParser;
import org.json.JSONObject;
import org.junit.jupiter.api.Test;
import javax.json.Json;
import java.io.StringReader;
class MyTest {
@Test
void myTest() throws JsonProcessingException {
String personId = "0123";
// org.json transforms on construction
JSONObject j1 = new JSONObject("{personId: " + personId + "}");
System.out.println(j1.getString("personId")); // Throws exception
// javax.json throws error on construction
javax.json.JsonObject j2 = Json.createReader(new StringReader("{\"personId\": " + personId + "}")).readObject();
System.out.println(j2.getString("personId"));
// gson treats it as pseudo-json and leaves it to the getter to decide: getAsInt will trim, getAsString will not
com.google.gson.JsonObject j3 = JsonParser.parseString("{\"personId\": " + personId + "}").getAsJsonObject();
System.out.println(j3.get("personId").getAsString());
// jackson throws error on construction
JsonNode jsonNode = new ObjectMapper().readTree("{\"personId\": " + personId + "}");
System.out.println(jsonNode.get("personId").asText());
}
}
- 主要言語
- Java
- スター
- 4.7k
- フォーク
- 2.6k
- 平均マージ
- 11日 18分
- マージ済み PR(30日)
- 1
コントリビューションガイド
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
stleary/JSON-java のほかの issue
-
Fix before the next release
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
-
難易度 2/5 1〜3時間 初心者へのやさしさ 76/100
-
難易度 2/5 1〜3時間 初心者へのやさしさ 88/100
-
難易度 2/5 1〜3時間 初心者へのやさしさ 82/100
-
Fix before the next release
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
stleary/JSON-java の issue をすべて見る
似ている issue
-
Bug Java Platform: Java
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
getsentry/sentry-java#6138 · コメント 1 件 ·
-
bug needs triage p2
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
GoogleCloudPlatform/DataflowTemplates#4273 · コメント 1 件 ·
-
[Studio][Bug] Bulk-deleting a full page of alert rules steps the page back while more rules remain オープン
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
apache/rocketmq-dashboard#4654 · コメント 1 件 ·
-
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
-
難易度 2/5 1〜3時間 初心者へのやさしさ 76/100