Behavior of JSONObject constructor trimming 0-led integers and converting them to int type instead of String type

Abierto
#826 15 comentarios 0 reacciones 0 asignados Ver en GitHub

Nadie ha tomado este issue todavía.

Evaluación

Dificultad
4/5
Tiempo estimado
3-5 días
Aptitud para principiantes
28/100
Tipo de issue
Error
Claridad
Necesita aclaración
Estado de actividad
Estancado
Stack tecnológico
java
Área
backend

Línea de trabajo

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.

Escrito por el modelo de indexación a partir del texto del issue.

Descripción

Feedback Requested Fixed

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());
    }
}
Lenguaje dominante
Java
Estrellas
4.7k
Forks
2.6k
Merge medio
11 d 18 min
PR fusionados (30 d)
1

Guía de contribución

Abrir la guía de contribución

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Más de stleary/JSON-java

Todos los issues de stleary/JSON-java

Issues similares

Más issues de Java

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.