Menu

#13 "use" not working properly (on String)

2.0.0
closed-invalid
5
2014-10-14
2010-03-17
No

What I want to do is to have a "bar" object that may hold different types of data.
However this was not working by doing for example ".use("bar", String)" or by using a TypeLocator.

I decided to write a little test and found that using was not working correctly at all.
Can you give me a some feedback on this ?
What I do is just a redundant declaration - which is pretty much the simplest thing I could trace the problem down to.

Much appreciated !

public class FlexJsonTest
{
private String bar;
private int type;

FlexJsonTest()
{
setBar("test");
setType(1);
}

public void setBar(String bar) {
this.bar = bar;
}
public String getBar() {
return (String)bar;
}

public void setType(int type) {
this.type = type;
}
public int getType() {
return type;
}

public static void main(String[] args)
{
String intermediate;
FlexJsonTest source = new FlexJsonTest();
FlexJsonTest destination;

// {"bar":"test","class":"FlexJsonTest","type":1}
intermediate = new JSONSerializer().deepSerialize(source);

// Problem here
destination = new JSONDeserializer<FlexJsonTest>().use("bar", String.class).deserialize(intermediate);
}
}

Discussion

  • Johannes Hange

    Johannes Hange - 2010-03-17

    Error is:

    Exception in thread "main" java.lang.ClassCastException: java.lang.String cannot be cast to java.util.Map
    at flexjson.factories.ClassLocatorObjectFactory.instantiate(ClassLocatorObjectFactory.java:42)
    at flexjson.ObjectBinder.convert(ObjectBinder.java:174)
    at flexjson.ObjectBinder.bindIntoObject(ObjectBinder.java:140)
    at flexjson.factories.BeanObjectFactory.instantiate(BeanObjectFactory.java:17)
    at flexjson.ObjectBinder.bind(ObjectBinder.java:72)
    at flexjson.ObjectBinder.bind(ObjectBinder.java:64)
    at flexjson.JSONDeserializer.deserialize(JSONDeserializer.java:156)
    at FlexJsonTest.main(FlexJsonTest.java:132)

     
  • Charlie Hubbard

    Charlie Hubbard - 2010-03-17

    What exactly is the problem? The code provided doesn't make any sense. use() method is meant to provide information about a type that can't be determined from inspecting the class. String is well known concrete class so there's no need for a use() method call for the "bar" property.

    If you serialized FlexJsonTest object I'm fairly certain by looking in the class attribute that was serialized it will determine the class to use for the root object. Once it knows that it can determine that Bar is a string from the getter/setter in that class. What is the expected behavior and what is the actual behavior?

     
  • Johannes Hange

    Johannes Hange - 2010-03-17

    Hi and thanks for replying so quickly.
    I am aware of this.

    As I said what I really want to do is:
    JSONDeserializer<Response>()
    .use("data", new TypeLocator<String>("type")
    .add("challenge", String.class)
    .add("information", ArrayList.class)
    .add("signature", String.class)
    ).deserialize(this.data);

    With __data being an Object__ holding different kinds of payload.

    The test was written to check an even simpler case (String to String) and use could not even handly this case properly which should work without any problems (imo),

    Thanks in advanc

     
  • Johannes Hange

    Johannes Hange - 2010-03-17

    To clearify "this.data" is the intermediate string and not connected to "Response.data".

     
  • Brandon Goodin

    Brandon Goodin - 2013-08-28

    Is this still an issue?

     
  • Brandon Goodin

    Brandon Goodin - 2013-08-28
    • Group: --> 2.0.0
     
  • Charlie Hubbard

    Charlie Hubbard - 2013-08-28

    I think this was a misunderstanding of how to use deserializer and what type locator meant. I think he wanted to use type locator to change the type on a sibling field, but type locator is only meant to change the object type that the type locator field belongs to. So I think this isn't a bug.

     
  • Charlie Hubbard

    Charlie Hubbard - 2014-10-14
    • status: open --> closed-invalid
     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.