fyhack / google-gson

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

Error Deserializing objects using fromJson(string, type) #140

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
1. Created this class

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.run a toJson and then try to fromJson back to the object
       Search search = new Search();
       search.setAccountNumber("123456");
       String jsonString = new Gson().toJson(search);
       Search search = new Gson().fromJson(jsonString, 
Search.class); //Parse error here

15:25:18,615 DEBUG []  Lookup JSON: 
{"accountNumber":"5794749","confirmationNumber":"","bankNumber":""}
15:25:18,615 ERROR []  Failed to generate JSON!
com.google.gson.JsonParseException: Failed parsing JSON source: 
java.io.StringReader@5b205b20 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)

3. This code works though

            Gson gson = new Gson();
        JsonElement root = new JsonParser().parse(json);
        return  gson.fromJson(root, type);

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

It should work with either code

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

1.3 with IBM JVM 1.6

Please provide any additional information below.

What appears to be happening is that inside the Library code, the string 
gets converted to a StringReader and then that get passed to a parse 
method, but the parse method tries to parse the StringReader.toSTring(), 
which doesn't yield a JSON string, only a class address. 

Original issue reported on code.google.com by chrislha...@gmail.com on 6 Aug 2009 at 8:30

GoogleCodeExporter commented 9 years ago
Wrote a test in r433 that attempts to reproduce this bug but the test passes. I 
am
using OpenJDK 1.6 on linux.

Original comment by inder123 on 7 Aug 2009 at 1:13

GoogleCodeExporter commented 9 years ago
Is OpenJDK 1.6 on Linux a requirement. In order to reproduce a test, you also 
have 
to reproduce the circumstances. I said IBM JVM 1.6. Now, if Gson, doesn't 
support 
the 1.6 IBM JVM, then we have no issue. I guess I'll have to figure it out 
myself 
and fix it. I don't like using an IBM JVM, but I have no choice on this project.

Original comment by chrislha...@gmail.com on 7 Aug 2009 at 10:47

GoogleCodeExporter commented 9 years ago
I dont know if it is a JDK issue or something else. I dont have the IBM JDK
installed, so can you help testing for it? I wrote tests in r433 that 
generalizes
your code a little bit. Can you run those tests and let us know if they pass?

Original comment by inder123 on 7 Aug 2009 at 9:57

GoogleCodeExporter commented 9 years ago
Issue 142 is a better description of the problem. It isn't JVM related, it is 
Spring
related. Something to do with Injection somehow. I complete examples to 
reproduce in
Issue 142. I apologize for not investigating it further before I posted the 
bug, but
at the time, it was all the information I had. I spent an hour today trying to
consistently reproduce it and getting the steps documented to created that 
issue. 

Original comment by chrislha...@gmail.com on 7 Aug 2009 at 10:32