google / gson

A Java serialization/deserialization library to convert Java Objects into JSON and back
Apache License 2.0
23.4k stars 4.29k forks source link

UniCode Convert to String and than convert back to JSON not working #2135

Closed ibtesammalik closed 2 years ago

ibtesammalik commented 2 years ago

Gson version

GSON version auto fetches by using spring boot2.0.9.RELEASE

Java / Android version

JDK 1.8

Used tools

Description

Expected behavior

{"data":{"consignorAddress":"POL IND AMETLLA PARK CALLE AIGUAFREDA, 12 L\u0060AMETLLA DEL VALLES,08480 BARCELONA SPAIN NIF: ES G63059521"}}

Actual behavior

data:{"consignorAddress":"POL IND AMETLLA PARK CALLE AIGUAFREDA, 12 L`AMETLLA DEL VALLES,08480 BARCELONA SPAIN NIF: ES G63059521"}

Reproduction steps

String data ="{\"data\":{\"consignorAddress\":\"POL IND AMETLLA PARK CALLE AIGUAFREDA, 12 L\u0060AMETLLA DEL VALLES,08480 BARCELONA SPAIN NIF: ES G63059521\"}}";
Gson gson = new GsonBuilder().create();

    JsonObject jsonObject =  gson.fromJson(data, JsonObject.class);
    JsonElement orginalData=jsonObject.get("data");
    System.out.println("org data:"+orginalData);

    System.out.println("rep data:"+gson.toJson(orginalData));
  1. Actual string"data":{"consignorAddress":"POL IND AMETLLA PARK CALLE AIGUAFREDA, 12 L\u0060AMETLLA DEL VALLES,08480 BARCELONA SPAIN NIF: ES G63059521"}}
  2. call gson.fromJson(data, JsonObject.class);
  3. print data:{"consignorAddress":"POL IND AMETLLA PARK CALLE AIGUAFREDA, 12 L`AMETLLA DEL VALLES,08480 BARCELONA SPAIN NIF: ES G63059521"}
  4. revert back to JSON by calling JsonElement orginalData=jsonObject.get("data"); gson.toJson(orginalData)
  5. doesn't show actual string
  6. why ` is not converting back to \u0060
Marcono1234 commented 2 years ago

If the \u0060 appears like this in JSON and is not escaped with an additional backslash (e.g. \\u0060), then it represents the Unicode code point U+0060, and Gson can just output the character (`). Both JSON strings "... \u0060 ..." and "... ` ..." are valid and represent the same data.

Is there a reason why you need to have the Unicode escape \u0060 in the JSON output? Are you working with an external library which does not support `?

eamonnmcmanus commented 2 years ago

The current behaviour seems correct, as explained by @Marcono1234.

ibtesammalik commented 2 years ago

If the \u0060 appears like this in JSON and is not escaped with an additional backslash (e.g. \\u0060), then it represents the Unicode code point U+0060, and Gson can just output the character (`). Both JSON strings "... \u0060 ..." and "... ` ..." are valid and represent the same data.

Is there a reason why you need to have the Unicode escape \u0060 in the JSON output? Are you working with an external library which does not support `?

no, I am not using a third library, the reason I need this is because I am going to encrypt complete JSON data and send an encrypted signatory, like this

{ "data":{"consignorAddress":"POL IND AMETLLA PARK CALLE AIGUAFREDA, 12 LAMETLLA DEL VALLES,08480 BARCELONA SPAIN NIF: ES G63059521"}, "signature":"qdfi4BnDBXA1OMotX0TkwRSRQzJ1QhHBgEe4g7Q7EAE=" } if I encrypt data with " ' " then the client will not enable to validate my signatory. because the client is expecting\u0060`

Marcono1234 commented 2 years ago

Which library are you using on the client to extract "data" from the JSON document? To me it sounds like you would also have to verify that client and server extract "data" in the same way from the JSON. Would it be possible to transmit the "data" value separately to the client, so the client would not have to extract it itself, and to prevent any conversion mismatches such as the one you are currently experiencing? Note that Gson might also not be able to preserve the exact formatting (at least in case the JSON is formatted / pretty-printed in some way).