由于 Google Ads API 使用 proto3 作为其默认载荷格式,因此在使用 .NET 客户端库时,请务必了解一些协议缓冲区惯例和类型。
可选字段
Google Ads API 中的许多字段都标记为 optional。这样,您就可以区分字段具有空值的情况与服务器未返回字段值的情况。这些字段的行为类似于常规属性,但它们还提供了其他方法来清除字段以及检查字段是否已设置。
例如,Campaign 对象的 Name 字段被标记为 optional,因此您可以使用以下方法来处理此字段:
// Get the name.
string name = campaign.Name;
// Set the name.
campaign.Name = name;
// Check if the campaign object has the name field set.
bool hasName = campaign.HasName();
// Clear the name field. Use this method to exclude the Name field from
// being sent to the server in a subsequent API call.
campaign.ClearName();
// Set the campaign name to an empty string value. This value will be
// sent to the server if you use this object in a subsequent API call.
campaign.Name = "";
// This throws a runtime ArgumentNullException. Use ClearName() instead.
campaign.Name = null;
重复字段
在 Google Ads API 中,字段数组表示为只读 RepeatedField。
例如,广告系列的 url_custom_parameters 字段是一个重复字段,因此在 .NET 客户端库中表示为只读 RepeatedField<CustomParameter>。RepeatedField<T> 会实现 IList<T> 接口。
您可以通过以下两种方式填充 RepeatedField 属性:
AddRange 方法
Campaign campaign = new Campaign()
{
ResourceName = ResourceNames.Campaign(customerId, campaignId),
Status = CampaignStatus.Paused,
};
// Add values to UrlCustomParameters using the AddRange method.
campaign.UrlCustomParameters.AddRange(new CustomParameter[]
{
new CustomParameter { Key = "season", Value = "christmas" },
new CustomParameter { Key = "promocode", Value = "NY123" }
});
集合初始化程序语法
// Option 1: Initialize the field directly.
Campaign campaign = new Campaign()
{
ResourceName = ResourceNames.Campaign(customerId, campaignId),
Status = CampaignStatus.Paused,
// Directly initialize the field.
UrlCustomParameters =
{
new CustomParameter { Key = "season", Value = "christmas" },
new CustomParameter { Key = "promocode", Value = "NY123" }
}
};
// Option 2: Initialize using an intermediate variable.
CustomParameter[] parameters = new CustomParameter[]
{
new CustomParameter { Key = "season", Value = "christmas" },
new CustomParameter { Key = "promocode", Value = "NY123" }
};
Campaign campaign1 = new Campaign()
{
ResourceName = ResourceNames.Campaign(customerId, campaignId),
Status = CampaignStatus.Paused,
// Initialize from an existing array.
UrlCustomParameters = { parameters }
};
Oneof 字段
Google Ads API 中的某些字段会被标记为 oneof 字段,这意味着该字段可以包含不同类型的值,但在给定时间只能包含一个值。oneof 字段类似于 C 编程语言中的联合类型。
.NET 库通过为 oneof 字段中可包含的每种类型的值提供一个属性来实现 oneof 字段,所有属性都会更新共享底层存储字段。
例如,广告系列的 campaign_bidding_strategy 会标记为 oneof 字段。该类的实现方式如下(为简洁起见,代码已简化):
public sealed partial class Campaign : pb::IMessage<Campaign>
{
object campaignBiddingStrategy_ = null;
CampaignBiddingStrategyOneofCase campaignBiddingStrategyCase_;
public ManualCpc ManualCpc
{
get
{
return campaignBiddingStrategyCase_ ==
CampaignBiddingStrategyOneofCase.ManualCpc
? (ManualCpc)campaignBiddingStrategy_ : null;
}
set
{
campaignBiddingStrategy_ = value;
campaignBiddingStrategyCase_ =
CampaignBiddingStrategyOneofCase.ManualCpc;
}
}
public ManualCpm ManualCpm
{
get
{
return campaignBiddingStrategyCase_ ==
CampaignBiddingStrategyOneofCase.ManualCpm
? (ManualCpm)campaignBiddingStrategy_ : null;
}
set
{
campaignBiddingStrategy_ = value;
campaignBiddingStrategyCase_ =
CampaignBiddingStrategyOneofCase.ManualCpm;
}
}
public CampaignBiddingStrategyOneofCase CampaignBiddingStrategyCase
{
get { return campaignBiddingStrategyCase_; }
}
}
由于 oneof 属性共享存储空间,因此一项分配可能会覆盖之前的分配,从而导致细微的 bug。例如:
Campaign campaign = new Campaign()
{
ManualCpc = new ManualCpc(),
ManualCpm = new ManualCpm()
};
在这种情况下,campaign.ManualCpc 为 null,因为初始化 campaign.ManualCpm 属性会覆盖 campaign.ManualCpc 的上一次初始化。
转换为其他格式
转换为 JSON 格式
您可以使用 Google.Protobuf.JsonFormatter 将 protobuf 对象转换为 JSON 格式,也可以反向转换。在构建需要与其他系统(需要 JSON 或 XML 等基于文本的格式)对接的系统时,这非常有用。
using Google.Protobuf;
GoogleAdsRow row = new GoogleAdsRow()
{
Campaign = new Campaign()
{
Id = 123,
Name = "Campaign 1",
ResourceName = ResourceNames.Campaign(1234567890, 123)
}
};
// Serialize to JSON and back.
string json = JsonFormatter.Default.Format(row);
row = GoogleAdsRow.Parser.ParseJson(json);
转换为字节
您可以使用 Google.Protobuf 将对象序列化为字节,然后再反序列化。二进制序列化比 JSON 格式更节省内存和存储空间。
using Google.Protobuf;
GoogleAdsRow row = new GoogleAdsRow()
{
Campaign = new Campaign()
{
Id = 123,
Name = "Campaign 1",
ResourceName = ResourceNames.Campaign(1234567890, 123)
}
};
// Serialize to bytes and back.
byte[] bytes = row.ToByteArray();
row = GoogleAdsRow.Parser.ParseFrom(bytes);