Behavior of JSONObject constructor trimming 0-led integers and converting them to int type instead of String type
Nessuno ha ancora preso questa issue.
Valutazione
- Difficoltà
- 4/5
- Tempo stimato
- 3-5 giorni
- Idoneità per principianti
- 28/100
Direzione di ricerca
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.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Descrizione
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());
}
}
- Lingua principale
- Java
- Stelle
- 4.7k
- Fork
- 2.6k
- Merge medio
- 6g 20h
- PR unite (30g)
- 2
Guida per i contributori
Apri la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Altre issue di stleary/JSON-java
-
Fix before the next release
Difficoltà 2/5 1-3 ore Idoneità per principianti 78/100
-
New JSONPointer tests needed Aperta
Difficoltà 2/5 1-3 ore Idoneità per principianti 76/100
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 88/100
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 82/100
-
Fix before the next release
Difficoltà 2/5 1-3 ore Idoneità per principianti 78/100
Tutte le issue di stleary/JSON-java
Issue simili
-
executions.Query — startDate and timeRange filters are sent with inverted comparison operators Apertaarea/plugin
Difficoltà 2/5 1-3 ore Idoneità per principianti 75/100
kestra-io/plugin-kestra#190 ·
-
litertlm-android AAR ships no consumer ProGuard rules → "mid == null" SIGABRT in minified apps Aperta
Difficoltà 2/5 1-3 ore Idoneità per principianti 70/100
google-ai-edge/LiteRT-LM#3739 ·
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 75/100
integra-team-red/meet-map#249 ·
-
[Studio][Bug] Cancelled create-user dialog keeps the password and admin switch for the next attempt Aperta
Difficoltà 2/5 1-3 ore Idoneità per principianti 75/100
apache/rocketmq-dashboard#5064 ·
-
Consent portal: creating a duplicate Purpose shows a generic error instead of "already exists" Aperta
Difficoltà 2/5 1-3 ore Idoneità per principianti 75/100
wso2/dpdp-accelerator#287 ·