4

I'll start directly with my question and later on I give more background information.

Simple: I hava a LinkedHashMap<String, String> and it represents a specific object. What is the best way to convert it to that object?

I know that you can use generics to get through all elements and set the fields, but what is whit nested objects?

Background: I have a JAX-RS service that consumes JSON objects. My service handles different kind of objects and just represents an interface. So I don't know which objects are coming from outside and which program is using my service.

At runtime I get the information via @Inject on my interface. The JAX-RS service stores the data from the client in an untyped Object and its done automatically (thats the LinkedHashMap<String, String>).

With my interface I want to provide a method like setObject and parameter should be a object of that type. I can handle all of this, but not the part where I convert the Object LinkedHashMap<String, String> to that specific object.

Example Structure: Target Objects could like that

public class Document {
    private String title;
    private String id;
    private int version;
}

The LinkedHashMap looks like that

{title=some title, id=1, version=1}
Tobias Sarnow
  • 1,076
  • 2
  • 12
  • 40

2 Answers2

3

You can create two classes

@XmlAccessorType(XmlAccessType.FIELD) 
public class Document {
    @XmlElement
    private String title;
    @XmlElement
    private String id;
    @XmlElement
    private int version;
}  


@XmlAccessorType(XmlAccessType.FIELD)
public class MapJson {
    @XmlElement
    private LinkedHashMap<String, String> documents; 
}  

and cobvert Object to JSON usingg
Jackson

new org.codehaus.jackson.map.ObjectMapper().writeValueAsString(instanceofMapJson);  

Google JSON

new com.google.gson.Gson().toJson(instanceofMapJson);  

PS. Using google json, you can remove xml annotations from your classes

Ilya
  • 29,135
  • 19
  • 110
  • 158
  • I also thought about performing a reverse marshaling with the same method JAX-RS is using. I really like Gson but for my case I would like to have a JAX-RS standard way, so that on application server jackson or any other JAX-RS implementation will be used. Is there a way for that? – Tobias Sarnow Aug 24 '12 at 11:24
  • I'm using Gson right now, but I'm still looking forward for a not library dependent way. – Tobias Sarnow Sep 04 '12 at 09:49
1

Reflection is the only way for setting properties of generic unknown object.
You can find everything you will need in the docs.

Germann Arlington
  • 3,315
  • 2
  • 17
  • 19
  • Hmm ok but how do I handle nested objects in a performant way? – Tobias Sarnow Aug 24 '12 at 09:04
  • Reflection is NOT designed for performance. You can try to cache object's (class) variable->method mappings in your (application containing) `setObject` method, it will eliminate the need to iterate over the object's properties/methods every time, but it is still going to be (relatively) slow. There is nothing you can do about dynamic method invocation - it **IS SLOW**. – Germann Arlington Aug 24 '12 at 09:17
  • Do you know if there is library out there that helps me setting up the object? – Tobias Sarnow Aug 24 '12 at 09:21
  • From the top of my head - try to look at JAXB (https://www.google.co.uk/search?q=java+jaxb) – Germann Arlington Aug 24 '12 at 09:34
  • 1
    I tried reflection and I end up using Gson to map my object. Handling nested objects can be a mess... – Tobias Sarnow Sep 04 '12 at 09:48