0

I have some JSON where one of the keys has one of three values: an int, a string, or a json object. Using the snippet below I can map this field when it is an int or a string but fail when it's a json object. Where am I going wrong? What should I be doing?

The JSON value key looks like:

"value": 51,

or

"value": 51,

or (and this is where I am having trouble)

"value": {"lat": 53.990614999999998, "lng": -1.5391117000000301, "addr": "Harrogate, North Yorkshire, UK"}


public class Test {

public Test() {
}

public static class Value {
public int slidervalue;
public String voicevalue;
public GeoValue geovalue;  // problem

public Value(int  value) {
   this.slidervalue = value
}

public Value(String  value) { 
this.voicevalue = value; 
}

public Value(JSONObject  value) { 
    JSONObject foo = value; // this is never reached

this.geovalue = value; // and how would this work so as map value to a GeoValue? 
}

private static class GeoValue {
private double _lat;
private double _lng;
private String _addr;

public float getLat() {  
    return (float)_lat;
    }
public void setLat(float lat) { 
    _lat = (double)lat; 
    }
public float getLng() { return (float)_lng;}
public void setLng(float lng) { _lng = (double)lng; }

public String getAddr() { return _addr;}
public void setAddr(String addr) { _addr = addr; }
}

} // end of Value class

public Value getValue() { return _value;}
public void setValue(Value value) { 
_value = value; 
}
} //end of Test class

and this is being used like this:

ObjectMapper mapper = new ObjectMapper();
instance = mInstances.getJSONObject(i).toString();
Test testinstance = mapper.readValue(instance, Test.class);


public class Test {

public Test() {
}

public static class Value {
public int slidervalue;
public String voicevalue;
public GeoValue geovalue;  // problem

public Value(int  value) {
   this.slidervalue = value
}

public Value(String  value) { 
this.voicevalue = value; 
}

public Value(JSONObject  value) { 
    JSONObject foo = value; // this is never reached

this.geovalue = value; // and how would this work so as map value to a GeoValue? 
}

private static class GeoValue {
private double _lat;
private double _lng;
private String _addr;

public float getLat() {  
    return (float)_lat;
    }
public void setLat(float lat) { 
    _lat = (double)lat; 
    }
public float getLng() { return (float)_lng;}
public void setLng(float lng) { _lng = (double)lng; }

public String getAddr() { return _addr;}
public void setAddr(String addr) { _addr = addr; }
}

} // end of Value class

public Value getValue() { return _value;}
public void setValue(Value value) { 
_value = value; 
}
} //end of Test class

and this is being used like this:

ObjectMapper mapper = new ObjectMapper();
instance = mInstances.getJSONObject(i).toString();
Test testinstance = mapper.readValue(instance, Test.class);

This fails with a JSONMappingException: No suitable contructor found for type ... 'value'

Thanks. Alex

2 Answers 2

1

What might work is that you mark the constructor that takes JSONObject with @JsonCreator, but do NOT add @JsonProperty for the single parameter. In that case, incoming JSON is bound to type of that parameter (in this case JSONObject, but you could use Map as well), and passed to constructor. Overloading still works because of special handling for single-string/int/long-argument constructor.

I am not sure if that is the cleanest solution; it might be cleanest to just implement custom deserializer. But it should work.

0

If your code is what you want, your json should be like this:

{"value":{"slidervalue":1,"voicevalue":"aa","geovalue":{"lat":53.990615,"lng":-1.53911170000003,"addr":"Harrogate, North Yorkshire, UK"}}}
1
  • Thanks - but 'value' is one of these, never all three. I'm asking if I can use class constructor signature parameters(int/String/JSONObject)in the mapping to read value - whichever type it is. Alex
    – Alex
    Jul 12, 2011 at 10:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.