英文:
How to convert Json fields with dashes to Avro fields with underscores?
问题
以下是您提供的内容的翻译部分:
Avro模型类(目标):
@AvroGenerated
public class Contract extends SpecificRecordBase implements SpecificRecord {
public static final Schema SCHEMA$ = (new Parser()).parse("....."});
@Deprecated
public String contract_id;
public static Schema getClassSchema() {
return SCHEMA$;
}
}
JSON(源):
{"contract-id": "1372b63a_7c6a_4929_a99b_199fa870edc0"}
转换:
final Gson gson = new GsonBuilder()
.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_DASHES)
.setFieldNamingStrategy(new PackageFieldNamingStrategy())
.registerTypeAdapter(Double.class, new DoubleSerializer())
.create();
Contract avroObject = GSON_SERIALIZER.fromJson(data);
// 打印 avroObject.contract_id
当我使用.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_DASHES)
并且将破折号替换为下划线时,代码返回正确的值,但如果破折号被替换为下划线并且移除.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_DASHES)
,则会得到contract_id
的正确值。
英文:
I have a string with json with attributes contained dashes. I want to convert it to an Avro object with field with underscore. I tried to use Gson library but got a null instead of value.
Avro model class (target):
@AvroGenerated
public class Contract extends SpecificRecordBase implements SpecificRecord {
public static final Schema SCHEMA$ = (new Parser()).parse("....."});
@Deprecated
public String contract_id;
public static Schema getClassSchema() {
return SCHEMA$;
}
JSON (source):
{"contract-id": "1372b63a_7c6a_4929_a99b_199fa870edc0"}
Converting:
final Gson gson = new GsonBuilder()
.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_DASHES)
.setFieldNamingStrategy(new PackageFieldNamingStrategy())
.registerTypeAdapter(Double.class, new DoubleSerializer())
.create();
Contract avroObject = GSON_SERIALIZER.fromJson(data);
// printing avroObject.contract_id
I get null for contract_id.
If the dashes are replaced with underscores and .setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_DASHES)
removed
the code returns the correct value.
答案1
得分: 0
Policy FieldNamingPolicy.LOWER_CASE_WITH_DASHES
would work if the field was named as contractId
. Because it is not then you need to define a FieldNamingStrategy
of your own, for example like:
public static class MyStrategy implements FieldNamingStrategy {
@Override
public String translateName(Field f) {
return f.getName().replace('_', '-');
}
}
and use it like:
Gson gson = new GsonBuilder()
.setFieldNamingStrategy(new MyStrategy())
.create();
or simply when building Gson, inline without extra classes:
.setFieldNamingStrategy(f -> f.getName().replace('_', '-'))
英文:
Policy FieldNamingPolicy.LOWER_CASE_WITH_DASHES
would work if the field was named as contractId
. Because it is not then you need to define a FieldNamingStrategy
of your own, for example like:
public static class MyStrategy implements FieldNamingStrategy {
@Override
public String translateName(Field f) {
return f.getName().replace('_', '-');
}
}
and use it like:
Gson gson = new GsonBuilder()
.setFieldNamingStrategy(new MyStrategy())
.create();
or simply when building Gson, inline without extra classes:
.setFieldNamingStrategy( f -> f.getName().replace('_', '-'))
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论