fyhack / google-gson

Automatically exported from code.google.com/p/google-gson
0 stars 0 forks source link

Deserialization/Serialization issue when json is injected via spring annotations #142

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?

1. Create the following java object

public class Search {

    private String accountNumber = "";
    public String getAccountNumber() {
        return accountNumber;
    }
    public void setAccountNumber(String accountNumber) {
        this.accountNumber = accountNumber;
    }
    public String getConfirmationNumber() {
        return confirmationNumber;
    }
    public void setConfirmationNumber(String confirmationNumber) {
        this.confirmationNumber = confirmationNumber;
    }
    public String getBankNumber() {
        return bankNumber;
    }
    public void setBankNumber(String bankNumber) {
        this.bankNumber = bankNumber;
    }
    private String confirmationNumber = "";
    private String bankNumber = "";

}

2. Create a controller class in Spring and add this method

@Controller("transactionController")
@RequestMapping("/transaction/*.jspx")
public class TransactionController {

     @RequestMapping(value = "lookup.jspx",
              method = { RequestMethod.GET, RequestMethod.POST })
    public ModelAndView lookup(@RequestParam("json") String json) throws 
Exception {

        String jsonString = "";

        try {

     Gson gson = new Gson();
     System.out.println(json); //JSON prints fine here
         Search ns = gson.fromJson(jsonString, Search.class); //runtime 
Error occurs here, everytime

   Replace the above code to this and it works fine.

            JsonElement element = new JsonParser().parse(json);
            gson.fromJson(element , Search.class);

}

3. Create a Junit test method inside another class. Run this test method.

    @SpringBeanByName
    public TransactionController transactionController;

    @Test
    public void testThis() throws Exception{

        Gson gson = new Gson();
        Search search = new Search();
        search.setAccountNumber("5794749");
        String json = gson.toJson(search);
        transactionController.lookup(json);
     }

4. You get this error 

{"accountNumber":"5794749","confirmationNumber":"","bankNumber":""}
05:56:00,288 ERROR 
[com.bbvacompass.webpromises.web.spring.mvc.TransactionController]  Failed 
to generate JSON!
com.google.gson.JsonParseException: Failed parsing JSON source: 
java.io.StringReader@51165116 to Json
    at com.google.gson.JsonParser.parse(JsonParser.java:57)
    at com.google.gson.Gson.fromJson(Gson.java:376)
    at com.google.gson.Gson.fromJson(Gson.java:329)
    at com.google.gson.Gson.fromJson(Gson.java:305)
    at 
com.bbvacompass.webpromises.web.spring.mvc.TransactionController.lookup
(TransactionController.java:100)

5. If you write a simple test, it works fine. 

    @Test
    public void testJSONSearch(){

        Search search = new Search();
        search.setAccountNumber("123456");
        Gson gson = new Gson();
        String json = gson.toJson(search);
        search = gson.fromJson(json, Search.class);

    }

What is the expected output? What do you see instead?

You should get the object back either way

What version of the product are you using? On what operating system?

Gson 1.3 on an IBM 1.6 JVM, but I believe that if you follow the exact 
steps above and inject the value of json, it will bomb using the fromJson
(String, Object) method. The fromJson(Jsonelement, class) works fine.

Please provide any additional information below.

Original issue reported on code.google.com by chrislha...@gmail.com on 7 Aug 2009 at 11:07

GoogleCodeExporter commented 9 years ago
This is a parser error so I wonder if your incoming JSON string is complete 
(although it seems to print fine). Can 
you try adding a blank space at the end and see if that works (that shouldn't 
actually make any difference, I am 
just trying a shot in the  dark). 

Can you debug this further (debug through parser and see what is going on)? We 
do not have the setup or 
expertise to use Spring, so we need someone else to narrow down the problem. 
Thanks.

Original comment by inder123 on 23 Sep 2009 at 5:32

GoogleCodeExporter commented 9 years ago
Keeping it open for a bit longer for further input, but deferring it for a 
future release.

Original comment by inder123 on 29 Sep 2009 at 5:55

GoogleCodeExporter commented 9 years ago
I'm a bit confused here.

In your example you have the following:

String jsonString = "";
try {
  Gson gson = new Gson();
  System.out.println(json); //JSON prints fine here
  Search ns = gson.fromJson(jsonString, Search.class);
  ...
} catch (SomeException e) { ... }

-------------

Shouldn't the parameter into the gson.fromJson(...) method be the "json" 
variable not
"jsonString"?

Original comment by joel.leitch@gmail.com on 10 Oct 2009 at 12:17

GoogleCodeExporter commented 9 years ago
Yes, you are correct. My apologies. This was a project I didn't have access to
anymore and I went back and wrote something to test and it works. This should be
closed as Not a Bug.

Original comment by chrislha...@gmail.com on 10 Oct 2009 at 1:21

GoogleCodeExporter commented 9 years ago

Original comment by inder123 on 10 Oct 2009 at 6:06