This is absolutely driving me nuts. I had this all working the other day, then broke something. Spent yesterday thinking I knew what it was and trying to track it down in vain. Here is the JSON string generated:
There is an IconInfo object that contains a list of 0 or more IconLocationInfo objects (location of the icon by day), each of which should contain a LatLonInfo object.
The problem is that the LatLonInfo object is not being serialized into JSON. Can anyone give me a hint why? I'm sure the latLonInfo attribute is being set, as I have dumped it out. There are no exceptions on the console.
Here are the classes (I've omitted IconInfo and deleted the equals/hashcode methods for brevity):
public class IconLocationInfoImpl implements IconLocationInfo {
private String name = null;
private Integer day = null;
private LatLonInfo latLonInfo = new LatLonInfoImpl();
public String getName() {
return this.name;
}
public void setName(String newValue) {
this.name = newValue;
}
public Integer getDay() {
if (this.day == null)
return 0;
else
return this.day;
}
public void setDay(Integer newValue) {
this.day = newValue;
}
public LatLonInfo getLatLonInfo() {
return this.latLonInfo;
}
public void setLatLonInfo(LatLonInfo newValue) {
this.latLonInfo = newValue;
}
/**
* Returns true if the IconLocationInfo object has a location set.
*/
public boolean latLonEntered() {
return (this.latLon != null) && this.latLon.latLonEntered();
}
public IconLocationInfo cloneCommon() {
IconLocationInfoImpl clone = new IconLocationInfoImpl();
clone.setName(this.getName());
clone.setDay(this.getDay());
clone.setLatLonInfo(this.getLatLonInfo());
return null;
}
/**
* toString
* @return String a string representation of the IconLocationInfoImpl object.
*/
public String toString() {
return new StringBuffer("IconLocationInfoImpl: ")
.append("Name(").append(this.getName()).append("), ")
.append("Day(").append(this.getDay()).append("), ")
.append(this.getLatLonInfo())
.toString();
}
} // IconLocationInfoImpl
public class LatLonInfoImpl implements LatLonInfo {
public final static LatLonInfo NO_LOCATION = new LatLonInfoImpl(null, null);
private String latitude; // this is the unit's current latitude
private String longitude; // this is the unit's current longitude
/**
* Default constructor for an empty LatLonImpl object.
*/
public LatLonInfoImpl() {
}
/**
* Construct a LatLonImpl using the latitude and longitude
* passed in as Strings. Note that no validation is performed
* (at present) to determine whether or not these are valid
* latitude and longitude coordinates.
*
* @param latitude
* @param longitude
*/
public LatLonInfoImpl(String latitude, String longitude) {
this();
this.setLatitude(latitude);
this.setLongitude(longitude);
}
/**
* Returns the latitude coordinate as a String.
* @return String representing the Latitude
*/
public String getLatitude() {
return this.latitude;
}
/**
* Sets the longitude of the LatLon
* @param newValue
*/
public void setLatitude(String newValue) {
this.latitude = newValue;
}
/**
* Returns the longitude coordinate as a String.
* @return String that represents the Longitude
*/
public String getLongitude() {
return this.longitude;
}
/**
* Sets the longitude of the LatLon
* @param newValue
*/
public void setLongitude(String newValue) {
this.longitude = newValue;
}
/**
* Returns a boolean flag indicating whether or not
* the LatLon object defines a valid set of coordinates.
*
* @return
*/
public boolean latLonEntered() {
return (this.latitude != null)&&(this.longitude != null);
}
/**
* Returns the latitude as a Double object.
*
* @return Double the latitude as a numeric Double object, null if not defined.
*/
public Double fetchDoubleLat() {
if (this.getLatitude()==null) return null;
Double lat = null;
try {
lat = Double.parseDouble(this.latitude);
} catch (java.lang.NumberFormatException nfe) {
lat = Double.NaN;
}
return lat;
} // getDoubleLat
/**
* Returns the longitude as a Double object.
*
* @return Double the longitude as a numeric Double object, null if not defined.
*/
public Double fetchDoubleLon() {
if (this.getLongitude()==null) return null;
Double lon = null;
try {
lon = Double.parseDouble(this.longitude);
} catch (java.lang.NumberFormatException nfe) {
lon = Double.NaN;
}
return lon;
}
/**
* The toString method returns a String of the format
* Location: day(x) name(xxxx) lat(xx.xxx) lon(xxx.xxxx)
*/
public String toString() {
return new StringBuffer("Lat/Lon: (")
.append(this.getLatitude())
.append("/")
.append(this.getLongitude())
.append(")")
.toString();
} // toString()
/**
* Clone only the LatLon interface specific fields (as a workaround
* for some JSON serialization issues.)
*/
public LatLonInfo cloneCommon() {
LatLonInfoImpl clone = new LatLonInfoImpl();
clone.setLatitude(this.getLatitude());
clone.setLongitude(this.getLongitude());
return clone;
}
} // LatLonImpl
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
What are the methods exposed by the LatLonInfo interface? I assume it exposes the getters somehow. the only reason I can think right now for this not to work is that JSONObject fails to find any properties on a LatLotInfo instance (it uses PropertyDescriptor by the way).
Cheers,
Andres
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
This is absolutely driving me nuts. I had this all working the other day, then broke something. Spent yesterday thinking I knew what it was and trying to track it down in vain. Here is the JSON string generated:
{"editingAllowed":true,"groupIdList":[1004,1003],"iconImagePath":"/static/img/casualtysource.gif","iconTypeName":"CASUALTY_SOURCE","id":1039,"locationInfoList":[{"day":0,"name":"START"}],"name":"CS1","notes":""}
There is an IconInfo object that contains a list of 0 or more IconLocationInfo objects (location of the icon by day), each of which should contain a LatLonInfo object.
The problem is that the LatLonInfo object is not being serialized into JSON. Can anyone give me a hint why? I'm sure the latLonInfo attribute is being set, as I have dumped it out. There are no exceptions on the console.
Here are the classes (I've omitted IconInfo and deleted the equals/hashcode methods for brevity):
public class IconLocationInfoImpl implements IconLocationInfo {
private String name = null;
private Integer day = null;
private LatLonInfo latLonInfo = new LatLonInfoImpl();
public String getName() {
return this.name;
}
public void setName(String newValue) {
this.name = newValue;
}
public Integer getDay() {
if (this.day == null)
return 0;
else
return this.day;
}
public void setDay(Integer newValue) {
this.day = newValue;
}
public LatLonInfo getLatLonInfo() {
return this.latLonInfo;
}
public void setLatLonInfo(LatLonInfo newValue) {
this.latLonInfo = newValue;
}
/**
* Returns true if the IconLocationInfo object has a location set.
*/
public boolean latLonEntered() {
return (this.latLon != null) && this.latLon.latLonEntered();
}
public IconLocationInfo cloneCommon() {
IconLocationInfoImpl clone = new IconLocationInfoImpl();
clone.setName(this.getName());
clone.setDay(this.getDay());
clone.setLatLonInfo(this.getLatLonInfo());
return null;
}
/**
* toString
* @return String a string representation of the IconLocationInfoImpl object.
*/
public String toString() {
return new StringBuffer("IconLocationInfoImpl: ")
.append("Name(").append(this.getName()).append("), ")
.append("Day(").append(this.getDay()).append("), ")
.append(this.getLatLonInfo())
.toString();
}
} // IconLocationInfoImpl
public class LatLonInfoImpl implements LatLonInfo {
public final static LatLonInfo NO_LOCATION = new LatLonInfoImpl(null, null);
private String latitude; // this is the unit's current latitude
private String longitude; // this is the unit's current longitude
/**
* Default constructor for an empty LatLonImpl object.
*/
public LatLonInfoImpl() {
}
/**
* Construct a LatLonImpl using the latitude and longitude
* passed in as Strings. Note that no validation is performed
* (at present) to determine whether or not these are valid
* latitude and longitude coordinates.
*
* @param latitude
* @param longitude
*/
public LatLonInfoImpl(String latitude, String longitude) {
this();
this.setLatitude(latitude);
this.setLongitude(longitude);
}
/**
* Returns the latitude coordinate as a String.
* @return String representing the Latitude
*/
public String getLatitude() {
return this.latitude;
}
/**
* Sets the longitude of the LatLon
* @param newValue
*/
public void setLatitude(String newValue) {
this.latitude = newValue;
}
/**
* Returns the longitude coordinate as a String.
* @return String that represents the Longitude
*/
public String getLongitude() {
return this.longitude;
}
/**
* Sets the longitude of the LatLon
* @param newValue
*/
public void setLongitude(String newValue) {
this.longitude = newValue;
}
/**
* Returns a boolean flag indicating whether or not
* the LatLon object defines a valid set of coordinates.
*
* @return
*/
public boolean latLonEntered() {
return (this.latitude != null)&&(this.longitude != null);
}
/**
* Returns the latitude as a Double object.
*
* @return Double the latitude as a numeric Double object, null if not defined.
*/
public Double fetchDoubleLat() {
if (this.getLatitude()==null) return null;
Double lat = null;
try {
lat = Double.parseDouble(this.latitude);
} catch (java.lang.NumberFormatException nfe) {
lat = Double.NaN;
}
return lat;
} // getDoubleLat
/**
* Returns the longitude as a Double object.
*
* @return Double the longitude as a numeric Double object, null if not defined.
*/
public Double fetchDoubleLon() {
if (this.getLongitude()==null) return null;
Double lon = null;
try {
lon = Double.parseDouble(this.longitude);
} catch (java.lang.NumberFormatException nfe) {
lon = Double.NaN;
}
return lon;
}
/**
* The toString method returns a String of the format
* Location: day(x) name(xxxx) lat(xx.xxx) lon(xxx.xxxx)
*/
public String toString() {
return new StringBuffer("Lat/Lon: (")
.append(this.getLatitude())
.append("/")
.append(this.getLongitude())
.append(")")
.toString();
} // toString()
/**
* Clone only the LatLon interface specific fields (as a workaround
* for some JSON serialization issues.)
*/
public LatLonInfo cloneCommon() {
LatLonInfoImpl clone = new LatLonInfoImpl();
clone.setLatitude(this.getLatitude());
clone.setLongitude(this.getLongitude());
return clone;
}
} // LatLonImpl
Hi Andrew,
What are the methods exposed by the LatLonInfo interface? I assume it exposes the getters somehow. the only reason I can think right now for this not to work is that JSONObject fails to find any properties on a LatLotInfo instance (it uses PropertyDescriptor by the way).
Cheers,
Andres