如何将带连字符的Json字段转换为带下划线的Avro字段?

huangapple go评论80阅读模式
英文:

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('_', '-'))

huangapple
  • 本文由 发表于 2020年10月14日 20:28:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/64353280.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定