5

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}
5
  • 1
    Have you Googled for a library to assist with converting from JSON to Java objects? You cannot be alone in wanting to do this. Commented Aug 24, 2012 at 8:38
  • Why can't you just use the HashMap instead? Commented Aug 24, 2012 at 8:38
  • What would be the benefit of using a HashMap @mercutio Commented Aug 24, 2012 at 9:01
  • @DuncanJones my problem is that I already have transfered JSON to Java, this is done automatically within JAX-RS. But at the point of retrieving the information I have no information about the later object. So having a Object that represents this LinkedHashmap is fine. Commented Aug 24, 2012 at 9:03
  • convertValue worked for me Commented Mar 23, 2022 at 21:15

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

2 Comments

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?
I'm using Gson right now, but I'm still looking forward for a not library dependent way.
1

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

5 Comments

Hmm ok but how do I handle nested objects in a performant way?
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.
Do you know if there is library out there that helps me setting up the object?
From the top of my head - try to look at JAXB (google.co.uk/search?q=java+jaxb)
I tried reflection and I end up using Gson to map my object. Handling nested objects can be a mess...

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.