GSON 在 Android API 26 和 API 28 之间以不同方式序列化 Location 对象
GSON serializing Location object differently between Android API 26 and API 28
我遇到以下代码片段在模拟器实例 运行ning API 26 与一个 运行ning API 28 之间工作方式不同的问题。这是我的代码片段 运行 将 GPS 点数据对象转换为 JSON.
GPSPointDataFinal dataFinal;
// bunch of code to put data into dataFinal
Gson gson = new GsonBuilder()
.serializeNulls()
.create();
String jsonFinalData = gson.toJson(dataFinal);
当我在 API 26 模拟器中 运行 这段代码时,我得到以下 JSON 输出:
{"gpsData":[
{"elevation":0.0,"location":{
mAltitude":0.0,"mBearing":122.0,"mBearingAccuracyDegrees":0.0,"mElapsedRealtimeNanos":70951742958220,"mExtras":null,"mFieldsMask":14,"mHorizontalAccuracyMeters":20.0,"mLatitude":38.9264983,"mLongitude":-77.0151,"mProvider":"fused","mSpeed":0.0,"mSpeedAccuracyMetersPerSecond":0.0,"mTime":1575977406000,"mVerticalAccuracyMeters":0.0},
"newSplit":false,"pointStatus":2,"time":{
"year":2019,"month":11,"dayOfMonth":10,"hourOfDay":6,"minute":30,"second":6}
}
请注意,所有与位置相关的数据都在此 JSON 字符串中。当我 运行 在 API 28 模拟器中使用相同的代码时,我得到以下 JSON 输出:
{"gpsData":[
{"elevation":40.8,"location":{
"mElapsedRealtimeNanos":9517448972700,"mProvider":"fused"},
"newSplit":false,"pointStatus":2,"time":{
"year":2019,"month":11,"dayOfMonth":10,"hourOfDay":6,"minute":32,"second":2}
}
请注意,位置数据的 none 在 JSON 字符串中。在这两种情况下,我都逐步执行了代码并确认在执行 gson.toJson(dataFinal).
之前 dataFinal 对象中有数据。
调试时,我在 logcat 中没有看到 GSON 或 JSON 相关条目。知道这是什么原因以及如何解决这个问题吗?
有关信息,这是我的 classes:
public class GPSPointDataFinal {
private GregorianCalendar startTime;
private ArrayList<GPSPointData> gpsData;
private CollectGPSDataServiceStatus status;
private ArrayList<Integer> splitList;
public GregorianCalendar getStartTime() {
return startTime;
}
public void setStartTime(GregorianCalendar startTime) {
this.startTime = startTime;
}
public ArrayList<GPSPointData> getGpsData() {
return gpsData;
}
public void setGpsData(ArrayList<GPSPointData> gpsData) {
this.gpsData = gpsData;
}
public CollectGPSDataServiceStatus getStatus() {
return status;
}
public void setStatus(CollectGPSDataServiceStatus status) {
this.status = status;
}
public ArrayList<Integer> getSplitList() {
return splitList;
}
public void setSplitList(ArrayList<Integer> splitList) {
this.splitList = splitList;
}
}
public class GPSPointData {
private Location location;
private double elevation;
private GregorianCalendar time;
private int pointStatus;
private boolean newSplit;
public GPSPointData(Location location, GregorianCalendar time, int status) {
this.location = location;
this.elevation = elevation;
this.time = time;
this.pointStatus = status;
}
public GPSPointData() {
}
public Location getLocation() {
return location;
}
public double getElevation() {
return elevation;
}
public GregorianCalendar getTime() {
return time;
}
public void setLocation(Location location) {
this.location = location;
}
public void setElevation(double elevation) {
this.elevation = elevation;
}
public void setTime(GregorianCalendar time) {
this.time = time;
}
public int getPointStatus() {
return pointStatus;
}
public void setPointStatus(int status) {
this.pointStatus = status;
}
public boolean isNewSplit() {
return newSplit;
}
public void setNewSplit(boolean newSplit) {
this.newSplit = newSplit;
}
}
根据上面 Clownba0t 的建议,通过将我需要的特定字段从 Location 对象中提取到我的 GPSPointData 对象中并且不包括位置对象,问题得到了解决。转换为 JSON 时,现在包含所有字段。
我遇到以下代码片段在模拟器实例 运行ning API 26 与一个 运行ning API 28 之间工作方式不同的问题。这是我的代码片段 运行 将 GPS 点数据对象转换为 JSON.
GPSPointDataFinal dataFinal;
// bunch of code to put data into dataFinal
Gson gson = new GsonBuilder()
.serializeNulls()
.create();
String jsonFinalData = gson.toJson(dataFinal);
当我在 API 26 模拟器中 运行 这段代码时,我得到以下 JSON 输出:
{"gpsData":[
{"elevation":0.0,"location":{
mAltitude":0.0,"mBearing":122.0,"mBearingAccuracyDegrees":0.0,"mElapsedRealtimeNanos":70951742958220,"mExtras":null,"mFieldsMask":14,"mHorizontalAccuracyMeters":20.0,"mLatitude":38.9264983,"mLongitude":-77.0151,"mProvider":"fused","mSpeed":0.0,"mSpeedAccuracyMetersPerSecond":0.0,"mTime":1575977406000,"mVerticalAccuracyMeters":0.0},
"newSplit":false,"pointStatus":2,"time":{
"year":2019,"month":11,"dayOfMonth":10,"hourOfDay":6,"minute":30,"second":6}
}
请注意,所有与位置相关的数据都在此 JSON 字符串中。当我 运行 在 API 28 模拟器中使用相同的代码时,我得到以下 JSON 输出:
{"gpsData":[
{"elevation":40.8,"location":{
"mElapsedRealtimeNanos":9517448972700,"mProvider":"fused"},
"newSplit":false,"pointStatus":2,"time":{
"year":2019,"month":11,"dayOfMonth":10,"hourOfDay":6,"minute":32,"second":2}
}
请注意,位置数据的 none 在 JSON 字符串中。在这两种情况下,我都逐步执行了代码并确认在执行 gson.toJson(dataFinal).
之前 dataFinal 对象中有数据。调试时,我在 logcat 中没有看到 GSON 或 JSON 相关条目。知道这是什么原因以及如何解决这个问题吗?
有关信息,这是我的 classes:
public class GPSPointDataFinal {
private GregorianCalendar startTime;
private ArrayList<GPSPointData> gpsData;
private CollectGPSDataServiceStatus status;
private ArrayList<Integer> splitList;
public GregorianCalendar getStartTime() {
return startTime;
}
public void setStartTime(GregorianCalendar startTime) {
this.startTime = startTime;
}
public ArrayList<GPSPointData> getGpsData() {
return gpsData;
}
public void setGpsData(ArrayList<GPSPointData> gpsData) {
this.gpsData = gpsData;
}
public CollectGPSDataServiceStatus getStatus() {
return status;
}
public void setStatus(CollectGPSDataServiceStatus status) {
this.status = status;
}
public ArrayList<Integer> getSplitList() {
return splitList;
}
public void setSplitList(ArrayList<Integer> splitList) {
this.splitList = splitList;
}
}
public class GPSPointData {
private Location location;
private double elevation;
private GregorianCalendar time;
private int pointStatus;
private boolean newSplit;
public GPSPointData(Location location, GregorianCalendar time, int status) {
this.location = location;
this.elevation = elevation;
this.time = time;
this.pointStatus = status;
}
public GPSPointData() {
}
public Location getLocation() {
return location;
}
public double getElevation() {
return elevation;
}
public GregorianCalendar getTime() {
return time;
}
public void setLocation(Location location) {
this.location = location;
}
public void setElevation(double elevation) {
this.elevation = elevation;
}
public void setTime(GregorianCalendar time) {
this.time = time;
}
public int getPointStatus() {
return pointStatus;
}
public void setPointStatus(int status) {
this.pointStatus = status;
}
public boolean isNewSplit() {
return newSplit;
}
public void setNewSplit(boolean newSplit) {
this.newSplit = newSplit;
}
}
根据上面 Clownba0t 的建议,通过将我需要的特定字段从 Location 对象中提取到我的 GPSPointData 对象中并且不包括位置对象,问题得到了解决。转换为 JSON 时,现在包含所有字段。