Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gson. converting to JSON a password with the character =

Tags:

android

gson

I'm using Gson in an Android app to convert a complicated object to JSON representation. One of the fields is a string called QuickPin containing an encrypted password and the character "=" is converted to "\003d" by Gson.

The Json string is consumed by a C# WEBAPI application, but returns "an error has occurred" message.

The following JSON returns that error message :

{"UserContractID":"929c1399-11c4-490e-8cff-5b1458ac18e2","UserAuthentication":"MethodCombo":{"AuthMethod":[1]},"QuickPin":"mW2n2uTECEtVqWA2B9MzvQ\u003d\u003d"},"CustomerID":0,"OriginID":0,"OriginTypeID":0,"Status":0}

Meanwhile this JSON works fine :

{"UserContractID":"929c1399-11c4-490e-8cff-5b1458ac18e2","UserAuthentication":{"QuickPin":"mW2n2uTECEtVqWA2B9MzvQ==","MethodCombo":{"AuthMethod":[1]}},"CustomerID":0,"OriginID":0,"OriginTypeID":0,"Status":0}

Are there a way to force Gson to maintain the string with the password with the = (and others if is the case) characters?

My Android code is :

Gson gson = new Gson();
user = new User();
user.UserAuthentication = new UserAuthentication();
user.UserAuthentication.QuickPin = "mW2n2uTECEtVqWA2B9MzvQ==";
user.UserAuthentication.MethodCombo = new MethodCombo();
user.UserAuthentication.MethodCombo.AuthMethod = new ArrayList<Integer>();
user.UserAuthentication.MethodCombo.AuthMethod.add(1);
user.Status = 0;
String jsonRepresentation = gson.toJson(user);
object.put("user", jsonRepresentation);

Thanks

like image 343
razonasistemas Avatar asked Mar 03 '14 23:03

razonasistemas


1 Answers

Gson escapes HTML metacharacters by default. You can disable this behavior.

Gson gson = new GsonBuilder().disableHtmlEscaping().create();
like image 81
rutter Avatar answered Oct 23 '22 04:10

rutter