I need to create a Json with field names with dots '.' in javascript application (typescript)
Json expected output:
{
"tasks.max": "1",
"key.converter": "io.confluent.connect.avro.AvroConverter",
"topics": "eraseme_topic_schema_test_v05"
}
I have tried ts-jackson and jackson-js, Code example using ts-jackson
MyDomain.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;
}
But the properties with dots are created as subclasses in Json output instead a property field with dot.
{
tasks: { max: '1' },
key: { converter: { schema: [Object] } },
topics: 'eraseme_topic_schema_test_v05'
}
How can I set dots in Json fields name in Javascript using a Jackson-like library?
CodePudding user response:
This comment by joniba on the lodash github repo shows how to access properties containing dots using lodash:
// lodash set
const o = {}
_.set(o, 'a["b.c"]', 1)
// returns { a: { 'b.c': 1 } }
This works with lodash get:
// lodash get
const obj = {'a.b': 'value'};
_.get(obj, '["a.b"]'); // 'value'
So it should be possible to write the ts-jackson path
properties like this:
export class MyDomain{
@JsonProperty({path: '["tasks.max"]', elementType: String})
tasks_max: string;
@JsonProperty({path: '["key.converter"]'})
key_converter: string;
@JsonProperty({path: 'topics'})
topics: string;
}