0

i would try to write an JSONparser for gwt.

For this i use the lib org.json on server side and the com.google.gwt.json.client on client side.

I build some interface's to call this two lib with the same methods.

I use:

public class JSONObject extends com.google.gwt.json.client.JSONObject implements IJSONObject
{
  ...
}

and

public class JSONObject extends org.json.JSONObject implements IJSONObject
{
  ...
}

So it works like a sharm, but there is one thing i wouldn't get it.

Client-side:

JSONParser.parseLenient( sJson ).isObject()

Return ans Object from Type: "com.google.gwt.json.client.JSONObject"

And i try to cast this to my custom JSONObject and get an ClassCastException.

My custom JSONObject extends from com.google.gwt.json.client.JSONObject and implements my IJSONobject.

So if i can't cast the object, i can't user the interface.

Thank you

4
  • 1
    Just because that method returns a reference to the parent class does mean it's of your subclass type as well. Commented Jun 10, 2016 at 6:46
  • But i know it is :P I only added methods not attributes Commented Jun 10, 2016 at 6:51
  • What part of this code: JSONParser.parseLenient( sJson ).isObject() uses your custom class? Commented Jun 10, 2016 at 6:53
  • Then there's your answer. Can't expect it to reference your object if it's not even using your code. Right? Commented Jun 10, 2016 at 7:01

2 Answers 2

2

You will get ClassCastException for sure.

Reason:

You are trying to cast an object "com.google.gwt.json.client.JSONObject" into an object "public class JSONObject extends com.google.gwt.json.client.JSONObject", which is not possible.

Example:

public class TestClassCast {

public static void main(String[] args) {

    A a = new A();
    B b = new B();

    A b1 = b; // This is possible since class B extends class A

    // B a1 = a; // This is not possible, which you are trying to do in your scenario.
}


}

class A {
    A(){};
}

class B extends A {
    B(){};
}
Sign up to request clarification or add additional context in comments.

5 Comments

But they are exactly the same, without some methods. Can i not force this?
No we cannot force this.
Have some alternate ideas how can i implement something like that?
It is not possible, since child class can be cast into parent, whereas parent class cannot be cast to child. Referring to my example code, B is child of A, so instance of B can be cast into A, whereas vice versa is not allowed. ie., A b1 = b; is allowed and B a1 = a; is not allowed.
Got it. But away form downcast have you any idea to solve my problem... it seems like the way with my interface wouldn't work
0

ClassCastException on downcast

I think you should extend from super class. Please try this :

public class YourCustomJSON extends com.google.gwt.json.client.JSONValue implement IJSONobject
{
   ...
}

1 Comment

But i don't want an JSONValue?!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.