Mined and Summarized Use Cases For API: genson

  1. Async Class not working to print out contents of JSON feed

    None .. details

    1. while ((line = reader.readLine()) != null)
    2. { // <-- oops stringBuilder.append(line)
    3. }

    • See Also (3)
    1. Jackson deserializing nested polymorphic type

      None details

      Reactions - Positive 0, Negative 0, Others 0

      1. Genson genson = new Genson.Builder().setWithClassMetadata(true) .addAlias("FinalLevel", FinalLevel.class) .create()
      2. // the @class must be first property in the json objectString testStr = "{ \"@class\": \"FinalLevel\", \"test\": \"foo\"}"
      3. FirstLevel result = genson.deserialize(testStr, FirstLevel.class)
      4. System.out.println(result.getTestValue())

    2. jackson de-serializing polymorphic types

      None details

      Reactions - Positive 0, Negative 0, Others 0

      1. Genson genson = new Genson.Builder().setWithClassMetadata(true).create()

    3. How can I send the Object class to server with Jackson?

      One of its features allows you to serialize to json and type information this enables you to deserialize back to the right type. The serialized json will look like "content" "@class":"package.path.Message" the object values... You can even define aliases for the serialized classes Important Note that you need to use the same configuration of genson on both sides. details

      Reactions - Positive 0, Negative 0, Others 0

      1. Genson genson = new Genson.Builder().setWithClassMetadata(true).create()
      2. json = genson.serialize(yourMessage)
      3. // then deserialize it backMessage message = genson.deserialize(json, Message .class)

  2. When using JSON in Fragments the app is not responding in Android Development

    Can you rewrite this with that and help me because I used it once and got the whole app not responding and the data was not parsed. Write this in your onCreate for api level 9 and also use async task for parsing values . .. details

    1. StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build()
    2. StrictMode.setThreadPolicy(policy)

    Positive Reactions
    1. no this is not recommended.
    Other Reactions
    1. If you are testing then ok. All network related operation must be done in a background thread.

    • See Also (2)
    1. Immutable/polymorphic POJO <-> JSON serialization with Jackson

      Genson gives you also the ability to use aliases used instead classes names). . details

      Reactions - Positive 2, Negative 0, Others 0

      1. new Genson.Builder().addAlias("shape", Shape.class) .addAlias("circle", Circle.class) .create()

      Positive Reactions
      1. That's a great tip, thanks!
      2. I'll definitely look into that!.

    2. Immutable/polymorphic POJO <-> JSON serialization with Jackson

      Everything works in your example with 0 annotations or heavy conf. Genson gives you also the ability to use aliases used instead classes names). details

      Reactions - Positive 2, Negative 0, Others 0

      1. Genson genson = new Genson.Builder().setWithClassMetadata(true) .setWithDebugInfoPropertyNameResolver(true) .create()
      2. String jsonCircle = genson.serialize(circle)
      3. String jsonRectangle = genson.serialize(rectangle)
      4. System.out.println(jsonCircle)
      5. // {"@class":"your.package.Circle","radius":123}System.out.println(jsonRectangle)
      6. // {"@class":"your.package.Rectangle","w":20,"h":30}// Throws nothing :)Shape newCircle = genson.deserialize(jsonCircle, Shape.class)
      7. Shape newRectangle = genson.deserialize(jsonRectangle, Shape.class)

      Positive Reactions
      1. That's a great tip, thanks!
      2. I'll definitely look into that!.

  3. GSON serialisation - how to exclude some fields from serialisation, but not deserialisation

    Put JsonIgnore(deseriaize=true on your getPassword method. Or if you want genson to use only fields instead of public getter/setter and fields configure it like that In that case put the annotation on the field. .. details

    1. Genson genson = new Genson.Builder() .setUseGettersAndSetters(false) .setFieldVisibility(VisibilityFilter.DEFAULT) .create()

    • See Also (2)
    1. Using Gson with Interface Types

      None details

      Reactions - Positive 0, Negative 0, Others 0

      1. // a better way to achieve the same thing would be to use an alias// no need to use setWithClassMetadata(true) as when you add an alias Genson // will automatically enable the class metadata mechanismgenson = new Genson.Builder().addAlias("loginRequest", LoginRequest.class).create()
      2. // output is {"@class":"loginRequest", ... other properties ...}genson.serialize(someRequest)

    2. How to deserialize JSON response from Jersey REST service to collection of java objects

      I don't want to use any additional libs I believe this can be done using jersey-json.jar and JAXB but don't know how. I think you want to use an anonymous subclass of GenericType List<Gadget>.class won't work because of type erasure. details

      Reactions - Positive 0, Negative 0, Others 0

      1. r.get(new GenericType<List<Gadget>>() {})

All usage scenarios for API genson
  • Async Class not working to print out contents of JSON feed

    None details

    Reactions - Positive 0, Negative 0, Others 0

    1. while ((line = reader.readLine()) != null)
    2. { // <-- oops stringBuilder.append(line)
    3. }

  • When using JSON in Fragments the app is not responding in Android Development

    Can you rewrite this with that and help me because I used it once and got the whole app not responding and the data was not parsed. Write this in your onCreate for api level 9 and also use async task for parsing values . details

    Reactions - Positive 1, Negative 0, Others 0

    1. StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build()
    2. StrictMode.setThreadPolicy(policy)

    Positive Reactions
    1. no this is not recommended.
    Other Reactions
    1. If you are testing then ok. All network related operation must be done in a background thread.

  • GSON serialisation - how to exclude some fields from serialisation, but not deserialisation

    Put JsonIgnore(deseriaize=true on your getPassword method. Or if you want genson to use only fields instead of public getter/setter and fields configure it like that In that case put the annotation on the field. details

    Reactions - Positive 0, Negative 0, Others 0

    1. Genson genson = new Genson.Builder() .setUseGettersAndSetters(false) .setFieldVisibility(VisibilityFilter.DEFAULT) .create()

  • Using Gson with Interface Types

    None details

    Reactions - Positive 0, Negative 0, Others 0

    1. // a better way to achieve the same thing would be to use an alias// no need to use setWithClassMetadata(true) as when you add an alias Genson // will automatically enable the class metadata mechanismgenson = new Genson.Builder().addAlias("loginRequest", LoginRequest.class).create()
    2. // output is {"@class":"loginRequest", ... other properties ...}genson.serialize(someRequest)

  • Immutable/polymorphic POJO <-> JSON serialization with Jackson

    Everything works in your example with 0 annotations or heavy conf. Genson gives you also the ability to use aliases used instead classes names). details

    Reactions - Positive 2, Negative 0, Others 0

    1. Genson genson = new Genson.Builder().setWithClassMetadata(true) .setWithDebugInfoPropertyNameResolver(true) .create()
    2. String jsonCircle = genson.serialize(circle)
    3. String jsonRectangle = genson.serialize(rectangle)
    4. System.out.println(jsonCircle)
    5. // {"@class":"your.package.Circle","radius":123}System.out.println(jsonRectangle)
    6. // {"@class":"your.package.Rectangle","w":20,"h":30}// Throws nothing :)Shape newCircle = genson.deserialize(jsonCircle, Shape.class)
    7. Shape newRectangle = genson.deserialize(jsonRectangle, Shape.class)

    Positive Reactions
    1. That's a great tip, thanks!
    2. I'll definitely look into that!.

  • Immutable/polymorphic POJO <-> JSON serialization with Jackson

    Genson gives you also the ability to use aliases used instead classes names). . details

    Reactions - Positive 2, Negative 0, Others 0

    1. new Genson.Builder().addAlias("shape", Shape.class) .addAlias("circle", Circle.class) .create()

    Positive Reactions
    1. That's a great tip, thanks!
    2. I'll definitely look into that!.

  • Jackson deserializing nested polymorphic type

    None details

    Reactions - Positive 0, Negative 0, Others 0

    1. Genson genson = new Genson.Builder().setWithClassMetadata(true) .addAlias("FinalLevel", FinalLevel.class) .create()
    2. // the @class must be first property in the json objectString testStr = "{ \"@class\": \"FinalLevel\", \"test\": \"foo\"}"
    3. FirstLevel result = genson.deserialize(testStr, FirstLevel.class)
    4. System.out.println(result.getTestValue())

  • jackson de-serializing polymorphic types

    None details

    Reactions - Positive 0, Negative 0, Others 0

    1. Genson genson = new Genson.Builder().setWithClassMetadata(true).create()

  • How can I send the Object class to server with Jackson?

    One of its features allows you to serialize to json and type information this enables you to deserialize back to the right type. The serialized json will look like "content" "@class":"package.path.Message" the object values... You can even define aliases for the serialized classes Important Note that you need to use the same configuration of genson on both sides. details

    Reactions - Positive 0, Negative 0, Others 0

    1. Genson genson = new Genson.Builder().setWithClassMetadata(true).create()
    2. json = genson.serialize(yourMessage)
    3. // then deserialize it backMessage message = genson.deserialize(json, Message .class)

  • How to deserialize JSON response from Jersey REST service to collection of java objects

    I don't want to use any additional libs I believe this can be done using jersey-json.jar and JAXB but don't know how. I think you want to use an anonymous subclass of GenericType List<Gadget>.class won't work because of type erasure. details

    Reactions - Positive 0, Negative 0, Others 0

    1. r.get(new GenericType<List<Gadget>>() {})