google的@SerializedName和@Expose注解

理解Gson库在JSON序列化中的应用及@Expose注解
本文详细介绍了如何使用Gson库将Java对象转换为JSON格式,并着重解释了@SerializedName和@Expose注解的作用。通过实例展示了如何自定义字段名和指定哪些字段应该被序列化。
注解了@SerializedName的字段会被序列化到JSON中,输出的JSON格式中的名字即为注解时给定的名字。public class SomeClassWithFields { @SerializedName("name") private final String someField; private final String someOtherField; public SomeClassWithFields(String a, String b) { this.someField = a; this.someOtherField = b; } } SomeClassWithFields objectToSerialize = new SomeClassWithFields("a", "b"); Gson gson = new Gson(); String jsonRepresentation = gson.toJson(objectToSerialize); System.out.println(jsonRepresentation); ===== OUTPUT ===== {"name":"a","someOtherField":"b"}

上面是google API的例子。这个注解可以用在需要以JSON格式输出pojo类信息的情况。

同样@Expose注解的意思是:An annotation that indicates this member should be exposed for JSON serialization or deserialization。这时google API上的一些话:

This annotation has no effect unless you build com.google.gson.Gson with a com.google.gson.GsonBuilder and invoke com.google.gson.GsonBuilder.excludeFieldsWithoutExposeAnnotation() method.

Here is an example of how this annotation is meant to be used:

 public class User {
   @Expose private String firstName;
   @Expose private String lastName;
   @Expose private String emailAddress;
   private String password;
 }
 
If you created Gson with new Gson(), the toJson() and fromJson() methods will use the password field along-with firstName, lastName, and emailAddress for serialization and deserialization. However, if you created Gson with Gson gson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create() then the toJson() and fromJson() methods of Gson will exclude the password field. This is because the password field is not marked with the @Expose annotation.

Note that another way to achieve the same effect would have been to just mark the password field as transient, and Gson would have excluded it even with default settings. The @Expose annotation is useful in a style of programming where you want to explicitly specify all fields that should get considered for serialization or deserialization.



package com.oplus.engineermode.device.config; import com.google.gson.Gson; import com.google.gson.GsonBuilder; import com.google.gson.JsonObject; import com.google.gson.annotations.SerializedName; import com.oplus.engineermode.core.sdk.utils.Log; import com.oplus.engineermode.device.config.chargespec.ChargeSpecBase; import com.oplus.engineermode.device.config.constant.ChargerType; import java.util.Locale; import java.util.Map; public class ChargeConfig { private static final String TAG = "ChargeConfig"; @SerializedName(value = "dual_cp_support") private boolean mDualChargeDumpSupport; @SerializedName(value = "three_cp_support") private boolean mThreeChargeDumpSupport; @SerializedName(value = "series_dual_battery_support") private boolean mSeriesDualBatterySupport; @SerializedName(value = "parallel_dual_battery_support") private boolean mParallelDualBatterySupport; @SerializedName(value = "normal_support") private boolean mNormalSupport; @SerializedName(value = "vooc_support") private boolean mVOOCSupport; @SerializedName(value = "svooc_support") private boolean mSVOOCSupport; @SerializedName(value = "pd_support") private boolean mPDSupport; @SerializedName(value = "parallel_chg_mos_support") private boolean mParallelChargeMosSupport; @SerializedName(value = "qc_support") private boolean mQCSupport; @SerializedName(value = "pps_support") private boolean mPPSSupport; @SerializedName(value = "ufcs_support") private boolean mUFCSSupport; @SerializedName(value = "ship_mode_support") private boolean mShipModeSupport; @SerializedName(value = "short_id_otp_status_support") private boolean mShortICOtpSupport; @SerializedName(value = "input_current_now_support") private boolean mInputCurrentNowSupport; @SerializedName(value = "qg_vbat_deviation_support") private boolean mVBatDeviationSupport; @SerializedName(value = "em_mode_support") private boolean mEMModeSupport; @SerializedName(value = "bcc_check_support") private boolean mBCCCheckExceptionSupport; @SerializedName(value = "sec_ic_test_support") private boolean mChargeEncryptionICSupport; @SerializedName(value = "gauge_check_support") private boolean mGaugeCheckSupport; @SerializedName(value = "boost_ic_support") private boolean mBoostIcSupport; @SerializedName(value = "balance_project_support") private boolean mBalanceProjectSupport; @SerializedName(value = "qcom_vph_vbat_deviation_support") private boolean mQcomQbatDeviationSupport; @SerializedName(value = "silicon_deep_spec_support") private boolean mDepthDischargeCountSupport; @SerializedName(value = "charge_spec") private Map<String, JsonObject> mChargeSpec; @SerializedName(value = "boost_ic_aging_config") private BoostIcAgingConfig mBoostIcAgingConfig; @SerializedName(value = "batt_sub_btb_support") private boolean mBatterySubBtbSupport; public ChargeConfig() { } public static ChargeConfig getDefaultInstance() { return new ChargeConfig(); } public Map<String, JsonObject> getChargeSpec() { return mChargeSpec; } public <T extends ChargeSpecBase> T getChargeSpec(ChargerType chargerType, Class<T> classOfT) { if (mChargeSpec != null) { final JsonObject jsonObject = mChargeSpec.get(chargerType.name().toLowerCase(Locale.US)); if (jsonObject != null) { return new Gson().fromJson(jsonObject.toString(), classOfT); } } return null; } public void setChargeSpec(Map<String, JsonObject> chargeSpec) { mChargeSpec = chargeSpec; } public boolean isDualChargeDumpSupport() { return mDualChargeDumpSupport; } public void setDualChargeDumpSupport(boolean dualChargeDumpSupport) { mDualChargeDumpSupport = dualChargeDumpSupport; } public boolean isThreeChargeDumpSupport() { return mThreeChargeDumpSupport; } public void setThreeChargeDumpSupport(boolean threeChargeDumpSupport) { mThreeChargeDumpSupport = threeChargeDumpSupport; } public boolean isSeriesDualBatterySupport() { return mSeriesDualBatterySupport; } public void setSeriesDualBatterySupport(boolean seriesDualBatterySupport) { mSeriesDualBatterySupport = seriesDualBatterySupport; } public boolean isParallelDualBatterySupport() { return mParallelDualBatterySupport; } public void setParallelDualBatterySupport(boolean parallelDualBatterySupport) { mParallelDualBatterySupport = parallelDualBatterySupport; } public boolean isNormalSupport() { return mNormalSupport; } public void setNormalSupport(boolean normalSupport) { mNormalSupport = normalSupport; } public boolean isParallelChgMosSupport() { return mParallelChargeMosSupport; } public void setParallelChgMosSupport(boolean mosSupport) { mParallelChargeMosSupport = mosSupport; } public boolean isVOOCSupport() { return mVOOCSupport; } public void setVOOCSupport(boolean VOOCSupport) { mVOOCSupport = VOOCSupport; } public boolean isSVOOCSupport() { return mSVOOCSupport; } public void setSVOOCSupport(boolean SVOOCSupport) { mSVOOCSupport = SVOOCSupport; } public boolean isPDSupport() { return mPDSupport; } public void setPDSupport(boolean PDSupport) { mPDSupport = PDSupport; } public boolean isQCSupport() { return mQCSupport; } public void setQCSupport(boolean QCSupport) { mQCSupport = QCSupport; } public boolean isPPSSupport() { return mPPSSupport; } public void setPPSSupport(boolean PPSSupport) { mPPSSupport = PPSSupport; } public boolean isUFCSSupport() { return mUFCSSupport; } public void setUFCSSupport(boolean UFCSSupport) { mUFCSSupport = UFCSSupport; } public boolean isShipModeSupport() { return mShipModeSupport; } public void setShipModeSupport(boolean shipModeSupport) { mShipModeSupport = shipModeSupport; } public boolean isShortICOtpSupport() { return mShortICOtpSupport; } public void setShortICOtpSupport(boolean shortICOtpSupport) { mShortICOtpSupport = shortICOtpSupport; } public boolean isInputCurrentNowSupport() { return mInputCurrentNowSupport; } public void setInputCurrentNowSupport(boolean inputCurrentNowSupport) { mInputCurrentNowSupport = inputCurrentNowSupport; } public boolean isVBatDeviationSupport() { return mVBatDeviationSupport; } public void setVBatDeviationSupport(boolean VBatDeviationSupport) { mVBatDeviationSupport = VBatDeviationSupport; } public boolean isEMModeSupport() { return mEMModeSupport; } public void setEMModeSupport(boolean EMModeSupport) { mEMModeSupport = EMModeSupport; } public boolean isBCCCheckExceptionSupport() { return mBCCCheckExceptionSupport; } public boolean isChargeEncryptionICSupport() { return mChargeEncryptionICSupport; } public void setBCCCheckExceptionSupport(boolean BCCCheckExceptionSupport) { mBCCCheckExceptionSupport = BCCCheckExceptionSupport; } public void setChargeEncryptionICSupport(boolean chargeEncryptionICSupport) { mChargeEncryptionICSupport = chargeEncryptionICSupport; } public boolean isBoostIcSupport() { return mBoostIcSupport; } public void setBoostIcSupport(boolean BoostIcSupport) { mBoostIcSupport = BoostIcSupport; } public void setBalanceProjectSupport(boolean BalanceProjectSupport) { mBalanceProjectSupport = BalanceProjectSupport; } public boolean isBalanceProjectSupport() { return mBalanceProjectSupport; } public void setQcomQbatDeviationSupport(boolean QcomQbatDeviationSupport) { mQcomQbatDeviationSupport = QcomQbatDeviationSupport; } public boolean isQcomQbatDeviationSupport() { return mQcomQbatDeviationSupport; } public void setDepthDischargeCountSupport(boolean depthDischargeCountSupport) { mDepthDischargeCountSupport = depthDischargeCountSupport; } public boolean isDepthDischargeCountSupport() { Log.i(TAG, "ChargeConfig Info:\n" + toString()); Log.i(TAG, "isDepthDischargeCountSupport debug1 " + mDepthDischargeCountSupport); return mDepthDischargeCountSupport; } public BoostIcAgingConfig getBoostIcAgingConfig() { return mBoostIcAgingConfig; } public boolean isGaugeCheckSupport() { return mGaugeCheckSupport; } public void setGaugeCheckSupport(boolean GaugeCheckSupport) { mGaugeCheckSupport = GaugeCheckSupport; } public boolean isBatterySubBtbSupport() { return mBatterySubBtbSupport; } public void setBatterySubBtbSupport(boolean batterySubBtbSupport) { mBatterySubBtbSupport = batterySubBtbSupport; } @Override public String toString() { return "ChargeConfig{" + "mDualChargeDumpSupport=" + mDualChargeDumpSupport + ", mThreeChargeDumpSupport=" + mThreeChargeDumpSupport + ", mSeriesDualBatterySupport=" + mSeriesDualBatterySupport + ", mParallelDualBatterySupport=" + mParallelDualBatterySupport + ", mNormalSupport=" + mNormalSupport + ", mVOOCSupport=" + mVOOCSupport + ", mSVOOCSupport=" + mSVOOCSupport + ", mPDSupport=" + mPDSupport + ", mQCSupport=" + mQCSupport + ", mPPSSupport=" + mPPSSupport + ", mShipModeSupport=" + mShipModeSupport + ", mShortICOtpSupport=" + mShortICOtpSupport + ", mInputCurrentNowSupport=" + mInputCurrentNowSupport + ", mVBatDeviationSupport=" + mVBatDeviationSupport + ", mEMModeSupport=" + mEMModeSupport + ", mBCCCheckExceptionSupport=" + mBCCCheckExceptionSupport + ", mChargeEncryptionICSupport=" + mChargeEncryptionICSupport + ", mChargeSpec=" + mChargeSpec + ", mParallelChargeMosSupport=" + mParallelChargeMosSupport + ", mBoostIcSupport=" + mBoostIcSupport + ", mBalanceProjectSupport=" + mBalanceProjectSupport + ", mQcomQbatDeviationSupport=" + mQcomQbatDeviationSupport + ", mDepthDischargeCountSupport=" + mDepthDischargeCountSupport + ", mGaugeCheckSupport=" + mGaugeCheckSupport + ", mBatterySubBtbSupport=" + mBatterySubBtbSupport + '}'; } public String toGson() { Gson gson = new GsonBuilder().setPrettyPrinting().create(); return gson.toJson(this); } } 文件devices_config.json { "otg": { "support": true, "cc_detect_support": true }, "usb": { "type": "USB_TYPE_C", "protocol": "USB_PROTOCOL_2_X" }, "vibrator": { "support": true, "version": "VERSION_3", "impedance": "[0,23500]", "resonant.frequency": "[160,180]" }, "charge": { "dual_cp_support": false, "series_dual_battery_support": true, "parallel_dual_battery_support": false, "normal_support": true, "vooc_support": true, "svooc_support": true, "ufcs_support": true, "pd_support": true, "qc_support": true, "pps_support": true, "ship_mode_support": true, "short_id_otp_status_support": false, "input_current_now_support": false, "qg_vbat_deviation_support": false, "em_mode_support": false, "bcc_check_support": true, "boost_ic_support": true, "silicon_deep_spec_support": true, "charge_spec": { "normal": { "battery.temp.plugin.delta.range": "[0,50]", "battery.voltage.range": "[3500,4580]", "high.capacity.battery.voltage.range": "[3880,4580]", "battery.temperate.range": "[150,450]", "high.capacity.battery.temperate.range": "[150,450]", "charger.voltage.range": "[4250,5500]", "high.capacity.charger.voltage.range": "[4250,5500]", "battery.current.range": "[-1200,-250]", "high.capacity.battery.current.range": "[-1200,-100]", "charge.battery.level.range": "[0,100]", "high.capacity.threshold": 75 }, "qc": { "battery.voltage.range": "[3500,4580]", "high.capacity.battery.voltage.range": "[3880,4580]", "battery.temperate.range": "[150,450]", "high.capacity.battery.temperate.range": "[150,450]", "charger.voltage.range": "[8250,9500]", "high.capacity.charger.voltage.range": "[4250,9500]", "battery.current.range": "[-2300,-500]", "high.capacity.battery.current.range": "[-2300,-200]", "charge.sub.current.range": "", "charge.battery.level.range": "[0,100]", "high.capacity.threshold": 60 }, "pps": { "battery.voltage.range": "[3500,4580]", "high.capacity.battery.voltage.range": "[3850,4580]", "battery.temperate.range": "[150,450]", "high.capacity.battery.temperate.range": "[150,450]", "charger.voltage.range": "", "high.capacity.charger.voltage.range": "", "battery.current.range": "[-5500,-2000]", "high.capacity.battery.current.range": "[-5500,-1000]", "charge.battery.level.range": "[0,100]", "high.capacity.threshold": 75, "check.pps.chg.ing": true }, "pd": { "battery.voltage.range": "[3500,4580]", "high.capacity.battery.voltage.range": "[3880,4580]", "battery.temperate.range": "[150,450]", "high.capacity.battery.temperate.range": "[150,450]", "charger.voltage.range": "[8250,9500]", "high.capacity.charger.voltage.range": "[4250,9500]", "battery.current.range": "[-2300,-500]", "high.capacity.battery.current.range": "[-2300,-200]", "charge.sub.current.range": "", "charge.battery.level.range": "[0,100]", "high.capacity.threshold": 60 }, "vooc": { "battery.voltage.range": "[3500,4580]", "high.capacity.battery.voltage.range": "[3880,4580]", "battery.temperate.range": "[150,450]", "high.capacity.battery.temperate.range": "[150,450]", "charger.voltage.range": "", "high.capacity.charger.voltage.range": "", "battery.current.range": "[-2300,-500]", "high.capacity.battery.current.range": "[-2300,-200]", "charge.battery.level.range": "[0,100]", "check.voocchg.ing": false, "high.capacity.threshold": 75 }, "svooc": { "battery.voltage.range": "[3500,4580]", "high.capacity.battery.voltage.range": "[3880,4580]", "battery.temperate.range": "[150,450]", "high.capacity.battery.temperate.range": "[150,450]", "charger.voltage.range": "", "high.capacity.charger.voltage.range": "", "battery.current.range": "[-11400,-2500]", "high.capacity.battery.current.range": "[-4500,-1000]", "charge.battery.level.range": "(30,100]", "battery.voltage.delta.range": "[0,50)", "check.voocchg.ing": true, "high.capacity.threshold": 75 }, "ufcs": { "battery.voltage.range": "[3500,4580]", "high.capacity.battery.voltage.range": "[3850,4580]", "battery.temperate.range": "[150,450]", "high.capacity.battery.temperate.range": "[150,450]", "charger.voltage.range": "", "high.capacity.charger.voltage.range": "", "battery.current.range": "[-3500,-2000]", "high.capacity.battery.current.range": "[-3500,-1000]", "charge.battery.level.range": "[0,100]", "check.ufcs.chg.ing": true, "high.capacity.threshold": 75 } } }, "wireless_charge": { "bpp_support": false, "epp_support": false, "air_vooc_support": false, "air_svooc_support": false, "reverse_support": false, "wireless_power_efficieny_support": false, "wireless_dual_charger_pump_support": false, "charge_spec": {} } } 打印报错 01-01 00:17:55.195 24435 24435 I ChargeConfig: ChargeConfig Info: 01-01 00:17:55.195 24435 24435 I ChargeConfig: ChargeConfig{mDualChargeDumpSupport=false, mThreeChargeDumpSupport=false, mSeriesDualBatterySupport=true, mParallelDualBatterySupport=false, mNormalSupport=true, mVOOCSupport=true, mSVOOCSupport=true, mPDSupport=true, mQCSupport=true, mPPSSupport=true, mShipModeSupport=true, mShortICOtpSupport=false, mInputCurrentNowSupport=false, mVBatDeviationSupport=false, mEMModeSupport=false, mBCCCheckExceptionSupport=true, mChargeEncryptionICSupport=false, mChargeSpec={normal={"battery.temp.plugin.delta.range":"[0,50]","battery.voltage.range":"[3500,4600]","high.capacity.battery.voltage.range":"[3930,4600]", "battery.temperate.range":"[150,450]","high.capacity.battery.temperate.range":"[150,450]","charger.voltage.range":"[4250,5500]","high.capacity.charger.voltage.range":"[4250,5500]","battery.current.range":"[-1200,-250]", "high.capacity.battery.current.range":"[-1200,-100]","charge.battery.level.range":"[0,100]","high.capacity.threshold":75}, qc={"battery.voltage.range":"[3500,4600]","high.capacity.battery.voltage.range":"[3930,4600]", "battery.temperate.range":"[150,450]","high.capacity.battery.temperate.range":"[150,450]","charger.voltage.range":"[8250,9500]","high.capacity.charger.voltage.range":"[4250,9500]","battery.current.range":"[-2300,-500]", "high.capacity.battery.current.range":"[-2300,-200]","charge.sub.current.range":"","charge.battery.level.range":"[0,100]","high.capacity.threshold":60}, pps={"battery.voltage.range":"[3500,4600]", "high.capacity.battery.voltage.range":"[3930,4600]","battery.temperate.range":"[150,450]","high.capacity.battery.temperate.range":"[150,450]","charger.voltage.range":"","high.capacity.charger.voltage.range":"", "battery.current.range":"[-5500,-2000]","high.capacity.battery.current.range":"[-5500,-1000]","charge.battery.level.range":"[0,100]","high.capacity.threshold":75,"check.pps.chg.ing":true}, pd={"battery.voltage.range":"[3500,4600]","high.capacity.battery.voltage.range":"[3930,4600]","battery.temperate.range":"[150,450]","high.capacity.battery.temperate.range":"[150,450]","charger.voltage.range":"[8250,9500]", "high.capacity.charger.voltage.range":"[4250,9500]","battery.current.range":"[-2300,-500]","high.capacity.battery.current.range":"[-2300,-200]","charge.sub.current.range":"","charge.battery.level.range":"[0,100]", "high.capacity.threshold":60}, vooc={"battery.voltage.range":"[3500,4600]","high.capacity.battery.voltage.range":"[3930,4600]","battery.temperate.range":"[150,450]","high.capacity.battery.temperate.range":"[150,450]", "charger.voltage.range":"","high.capacity.charger.voltage.range":"","battery.current.range":"[-2300,-500]","high.capacity.battery.current.range":"[-2300,-200]","charge.battery.level.range":"[0,100]","check.voocchg.ing":false, "high.capacity.threshold":75}, svooc={"battery.voltage.range":"[3500,4600]","high.capacity.battery.voltage.range":"[3930,4600]","battery.temperate.range":"[150,450]","high.capacity.battery.temperate.range":"[150,450]", "charger.voltage.range":"","high.capacity.charger.voltage.range":"","battery.current.range":"[-11400,-2500]","high.capacity.battery.current.range":"[-4500,-1000]","charge.battery.level.range":"(30,100]", "battery.voltage.delta.range":"[0,50)","check.voocchg.ing":true,"high.capacity.threshold":75}, ufcs={"battery.voltage.range":"[3500,4600]","high.capacity.battery.voltage.range":"[3930,4600]","battery.temperate.range":"[150,450]", "high.capacity.battery.temperate.range":"[150,450]","charger.voltage.range":"","high.capacity.charger.voltage.range":"","battery.current.range":"[-3500,-1000]","high.capacity.battery.current.range":"[-3500,-500]", "charge.battery.level.range":"[0,100]","check.ufcs.chg.ing":true,"high.capacity.threshold":75}}, mParallelChargeMosSupport=false, mBoostIcSupport=false, mBalanceProjectSupport=false, mQcomQbatDeviationSupport=false, mDepthDischargeCountSupport=false, mGaugeCheckSupport=false, mBatterySubBtbSupport=false} 01-01 00:17:55.195 24435 24435 I ChargeConfig: isDepthDischargeCountSupport debug1 false json中已经含有silicon_deep_spec_support为什么mDepthDischargeCountSupport还是false
最新发布
08-29
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值