wangyao5 / json-smart

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

IllegalAccessError when trying to serialize java.util.Calendar #54

Closed GoogleCodeExporter closed 9 years ago

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

1. Add the code:

Calendar rightNow = Calendar.getInstance();
Map<String,Object> myMap = new HashMap<>();
myMap.put("myDate",rightNow);
System.out.println("The JSON Object: " + JSONObject.toJSONString(myMap));

2. Build : mvn clean package
3. And execute: java -jar target/my-app-1.0-SNAPSHOT.jar

I created a github repo with the code to show you the problem:

https://github.com/jmaslibre/jsmart-test

To reproduce the problem using it:

1. git clone git@github.com:jmaslibre/jsmart-test.git
2. cd cd jsmart-test/
3. mvn clean package
4. java -jar target/my-app-1.0-SNAPSHOT.jar

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

The JSON Object: <json represantation of the object>

Instead I get:

-----------------------------------
Exception in thread "main" java.lang.IllegalAccessError: tried to access field 
java.util.Calendar.time from class 
net.minidev.asm.java.util.GregorianCalendarAccAccess
    at net.minidev.asm.java.util.GregorianCalendarAccAccess.get(Unknown Source)
    at net.minidev.json.reader.BeansWriterASM.writeJSONString(BeansWriterASM.java:21)
    at net.minidev.json.JSONValue.writeJSONString(JSONValue.java:596)
    at net.minidev.json.reader.JsonWriter.writeJSONKV(JsonWriter.java:392)
    at net.minidev.json.reader.JsonWriter$7.writeJSONString(JsonWriter.java:145)
    at net.minidev.json.reader.JsonWriter$7.writeJSONString(JsonWriter.java:128)
    at net.minidev.json.JSONObject.writeJSON(JSONObject.java:174)
    at net.minidev.json.JSONObject.toJSONString(JSONObject.java:74)
    at net.minidev.json.JSONObject.toJSONString(JSONObject.java:58)
    at com.mycompany.app.App.main(App.java:21)
---------------------------------------------------------

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

I am currently using: Java version: 1.7.0_67, vendor: Oracle Corporation, 
Apache Maven 3.0.5 in Debian Gnu/Linux

I tested with json-smart <version>2.1.1</version>,

and also cloned the repository:
  https://github.com/netplex/json-smart-v2
and buid the using the latest version there: 

392619dda5c53a25b64abe101299f9d263f894e6

Please provide any additional information below.

If you need any more information please let me know.

Thanks for your work in json-smart.

Original issue reported on code.google.com by jmasli...@gmail.com on 26 Mar 2015 at 5:38

GoogleCodeExporter commented 9 years ago
You have to register Calandar Type to make it serialisable.

JSONValue.registerWriter(Calendar.class, new JsonWriterI<Calendar>() {
// serialisation code.
}) {

Original comment by uriel.chemouni on 29 Jul 2015 at 2:36