[英]JSON Property names with dots in Javascript using Jackson-like library
我需要創建一個 Json,其字段名稱帶有點“。” 在 javascript 應用程序(打字稿)中
Json 預期 output:
{
"tasks.max": "1",
"key.converter": "io.confluent.connect.avro.AvroConverter",
"topics": "eraseme_topic_schema_test_v05"
}
我試過 ts-jackson 和 jackson-js,代碼示例使用 ts-jackson
我的域.ts
@Serializable()
import { JsonProperty,Serializable } from 'ts-jackson';
export class MyDomain{
@JsonProperty({path: 'tasks.max', elementType: String})
tasks_max: string;
@JsonProperty({path: 'key.converter'})
key_converter: string;
@JsonProperty({path: 'topics'})
topics: string;
}
但是帶點的屬性在 Json output 中創建為子類,而不是帶點的屬性字段。
{
tasks: { max: '1' },
key: { converter: { schema: [Object] } },
topics: 'eraseme_topic_schema_test_v05'
}
如何使用類似 Jackson 的庫在 Javascript 中的 Json 字段名稱中設置點?
joniba 對 lodash github repo 的評論顯示了如何使用 lodash 訪問包含點的屬性:
// lodash set
const o = {}
_.set(o, 'a["b.c"]', 1)
// returns { a: { 'b.c': 1 } }
這適用於 lodash get:
// lodash get
const obj = {'a.b': 'value'};
_.get(obj, '["a.b"]'); // 'value'
所以應該可以像這樣編寫 ts-jackson path
屬性:
export class MyDomain{
@JsonProperty({path: '["tasks.max"]', elementType: String})
tasks_max: string;
@JsonProperty({path: '["key.converter"]'})
key_converter: string;
@JsonProperty({path: 'topics'})
topics: string;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.