guochaiqi / google-gson

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

Gson can't parse JSON text encoded in UTF-8? #221

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
<code>
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;

import com.google.gson.JsonElement;
import com.google.gson.JsonParser;

public class Test {
        private static final String charEncoding="UTF-8";

        private static final String fileName="c:\\test2.txt";
        public static void main(String args[]){
                try{
                        File file=new File(fileName);
                        if(file.canRead()){
                                FileInputStream inStream=new FileInputStream(file);
                                InputStreamReader reader=new InputStreamReader(inStream,
charEncoding);
                                JsonParser parser=new JsonParser();
                                JsonElement jsonA=parser.parse(new BufferedReader(reader));
                                System.out.println(jsonA.isJsonArray());
                                System.out.println(jsonA.toString());
                        }
                }catch(IOException e){
                        e.printStackTrace();
                }
        }

} 
</code>

What is the expected output? What do you see instead?
Caused by: com.google.gson.TokenMgrError: Lexical error at line 1,
column 1.  Encountered: "\ufeff" (65279), after : ""
        at
com.google.gson.JsonParserJavaccTokenManager.getNextToken(JsonParserJavaccTokenM
anager.java:
1193)
        at com.google.gson.JsonParserJavacc.jj_ntk(JsonParserJavacc.java:635)
        at com.google.gson.JsonParserJavacc.parse(JsonParserJavacc.java:10)
        at com.google.gson.JsonParser.parse(JsonParser.java:54)
        ... 1 more 

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

Please provide any additional information below.
attached is the text file containing the json string

Original issue reported on code.google.com by monstern...@gmail.com on 8 Jul 2010 at 12:57

Attachments:

GoogleCodeExporter commented 9 years ago
It looks like you've got an input text containing a byte order mark. I blogged 
the workaround here:
  http://blog.publicobject.com/2010/08/handling-byte-order-mark-in-java.html

Original comment by limpbizkit on 20 Aug 2010 at 6:04