开发者

FlexJson deserialize object reference

I'm using Spring Roo which generated set of hibernate and FlexJSON classes.

I have entity called Location and entity called Comment. Location has many comments (1:M).

I'm trying to generate JSON object, which will, when deserialized and inserted reference existing Location object.

When I omit location field, everything is working fine, for example:

{ 
   "date": 1315918228639, 
   "comment": "Bosnia is very nice country" 
}

I don't know how to reference location field. I've tried following, but with little success:

{
   "location": 10,  
   "date": 1315918228639, 
   "comment": "Bosnia is very nice country" 
}

where location id is 10.

How can I reference location field in the JSON?

Edit: Added Comment entity:

@RooJavaBean
@RooToString
@RooJson
@RooEntity
public class Komentar开发者_如何学Python {

    private String comment;

    @ManyToOne
    private Location location;

    @Temporal(TemporalType.TIMESTAMP)
    @DateTimeFormat(style = "M-")
    private Date date;

}


I've solved issue by adding transient property.

@Transient
public long getLocationId(){
    if(location!=null)
        return location.getId();
    else 
        return -1;
}

@Transient
public void setLocationId(long id){
    location = Location.findLocation(id);
}


Got similar problem, but i can't change incoming json message, so i've changed generated aspect file:

@RequestMapping(value = "/jsonArray", method = RequestMethod.POST, headers = "Accept=application/json")
public ResponseEntity<String> Komentar.createFromJsonArray(@RequestBody String json) {
    for (Komentar komentar: Komentar.fromJsonArrayToProducts(json)) {
        komentar.setLocation(Location.findLocation(komentar.getLocation().getId()));
        komentar.persist();
    }
    HttpHeaders headers = new HttpHeaders();
    headers.add("Content-Type", "application/json");
    return new ResponseEntity<String>(headers, HttpStatus.CREATED);
}

komentar.setLocation(Location.findLocation(komentar.getLocation().getId())); was added by me.


I got same problem and solved it by introducing a custom object factory.

Since JSONDeserializer expect a json object for location attribute (ex:"Location":{"id":10,..}), supplying location id as a String/Integer (ex:"Location":"10") will give you an exception.

Therefore I have written LocationObjectFactory class and telling flexjson how to deserialize a Location class object in the way I want.

public class LocationObjectFactory implements ObjectFactory {

    @Override
    public Object instantiate(ObjectBinder context, Object value,
            Type targetType, Class targetClass) {

        if(value instanceof String){
            return Location.findProblem(Long.parseLong((String)value));
        }
        if(value instanceof Integer){
            return Location.findProblem(((Integer)value).longValue());
        }
        else {
            throw context.cannotConvertValueToTargetType(value,targetClass);
        }

    }

}

and deserialize the json string like this

new JSONDeserializer<Komentar>().use(null, Komentar.class).use(Location.class, new LocationObjectFactory()).deserialize(json);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜