由于 Google Ads API 使用 Protobuf 作为其默认有效负载格式, 来了解一些使用 API 时的 Protobuf 规范和类型。
可选字段
Google Ads API 中的许多字段被标记为 optional
。这样,您就可以
区分字段值为空的情况和
未发回 字段的值。这些字段的行为
字段,不过,它们还提供了一些额外的方法来清除字段和
检查该字段是否已设置。
例如,Campaign
对象的 Name
字段被标记为可选。
因此,您可以使用以下方法来处理此字段。
// 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 Name field from
// being sent to the server in a subsequent API call.
campaign.ClearName();
// Set the campaign to empty string value. This value will be
// sent to the server if you use this object in a subsequent API call.
campaign.Name = "";
// This will throw a runtime error. Use ClearName() instead.
campaign.Name = null;
重复的类型
字段数组在 Google Ads API 中表示为只读
RepeatedField
。
例如,广告系列的 url_custom_parameters
字段就是重复字段,
因此它在 .NET 中表示为只读 RepeatedField<CustomParameter>
客户端库。
RepeatedField
会实现
IList<T>
界面。
填充 RepeatedField
字段有两种方法。
旧版 C#:使用 AddRange 方法添加值
相关示例如下。
Campaign campaign = new Campaign()
{
ResourceName = ResourceNames.Campaign(customerId, campaignId),
Status = CampaignStatus.Paused,
};
// Add values to UrlCustomParameters using AddRange method.
campaign.UrlCustomParameters.AddRange(new CustomParameter[]
{
new CustomParameter { Key = "season", Value = "christmas" },
new CustomParameter { Key = "promocode", Value = "NY123" }
});
较新的 C# 版本:使用集合初始化程序语法
// 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 }
};
类型之一
Google Ads API 中的某些字段标记为 OneOf
字段,这意味着
可以保存不同的类型,但一次只能有一个值。其中一个字段是
类似于 C 语言中的 union 类型。
.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 资源共享存储空间,因此一次分配可以覆盖之前的分配 轻微错误。例如,
Campaign campaign = new Campaign()
{
ManualCpc = new ManualCpc()
{
EnhancedCpcEnabled = true
},
ManualCpm = new ManualCpm()
{
}
};
在本例中,campaign.ManualCpc
现在null
自初始化
campaign.ManualCpm
字段覆盖之前的初始化,
campaign.ManualCpc
。
转换为其他格式
您可以轻松将 protobuf 对象转换为 JSON 格式,反之亦然。这是 在构建需要与其他系统交互的系统时, 需要 JSON 或 XML 等文本格式的数据。
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);
您还可以将对象序列化为字节,然后再返回。二进制序列化更强大 内存和存储效率高于 JSON 格式。
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);