骆驼案例中的gRPC json数据

时间:2020-12-21 06:37:21

标签: angular grpc

我从 gRPC 收到了一个 json 数据,它是 Pascal 案例。我希望它是骆驼式的。我正在使用 angular 访问 gRPC 服务。即使我将结果序列化并将结果转换为驼峰式大小写,角度的输出也始终是 Pascal 大小写。 详情如下。

protobuf 消息

message EmployeeRecord {
   string record = 1;
}

gRPC 服务

var serializedResult = JsonConvert.SerializeObject(fields, new JsonSerializerSettings
{
   ContractResolver = new CamelCasePropertyNamesContractResolver()
});

var result = new Protos.EmployeeRecord() { record = serializedResult };

return result;

角度代码

public getEmployees() : void{
    this.response = "";
    const service = new EmployeeServiceClient(this.gRPCUrl, null);
    const request = new EmployeeRecord();
    request.setMesssage('');

    service.fields(request, null, (err, response: EmployeeRecord) => {
        if (err) {
          console.log(err);
        }
        else {
          this.response = response.toObject().record;
        }
    });
  }

this.response 中的结果总是在 Pascal Case 中

2 个答案:

答案 0 :(得分:0)

您在服务器上序列化为 JSON。然后,您将 protobuf 消息的 string 字段中的 JSON 发送到 angular。

在传输过程中,字符串似乎不太可能发生变化。

很有可能您的服务器没有生成您期望的数据(检查日志记录)或者您在客户端中混淆了某些内容。

答案 1 :(得分:0)

我正在使用 grpcui 工具并注意到一个类似的问题:JSON 原始请求使用 import matplotlib.pyplot as plt import mplcursors import numpy as np import pandas as pd import seaborn as sns df = pd.DataFrame({"date": pd.date_range('20210101', periods=10), "no_of_dogs": np.random.randint(10, 30, 10)}) fig, ax = plt.subplots(figsize=(15, 5)) sns.barplot(x="date", y="no_of_dogs", data=df, palette="husl", ax=ax) x_dates = df['date'].dt.strftime('%Y-%m-%d') ax.set_xticklabels(labels=x_dates) cursor = mplcursors.cursor(hover=True) @cursor.connect("add") def on_add(sel): x, y, width, height = sel.artist[sel.target.index].get_bbox().bounds sel.annotation.set(text=f"{x_dates[round(x)]}\n{height:.0f}", position=(0, 20), anncoords="offset points") sel.annotation.xy = (x + width / 2, y + height) plt.show() 但正确的名称是 myExampleFieldName

也许 JSON 不允许下划线?