@@ -306,6 +306,7 @@ public class ProtobufData {
306306 private boolean useWrapperForRawPrimitives ;
307307 private boolean generateStructForNulls ;
308308 private boolean generateIndexForUnions ;
309+ private boolean useJsonFieldNames ;
309310
310311 public ProtobufData () {
311312 this (new ProtobufDataConfig .Builder ().with (
@@ -332,6 +333,7 @@ public ProtobufData(ProtobufDataConfig protobufDataConfig) {
332333 this .useWrapperForRawPrimitives = protobufDataConfig .useWrapperForRawPrimitives ();
333334 this .generateStructForNulls = protobufDataConfig .generateStructForNulls ();
334335 this .generateIndexForUnions = protobufDataConfig .generateIndexForUnions ();
336+ this .useJsonFieldNames = protobufDataConfig .useJsonFieldNames ();
335337 }
336338
337339 /**
@@ -519,10 +521,14 @@ private Object fromConnectData(
519521 throw new DataException ("Invalid message name: " + scopedStructName );
520522 }
521523 for (Field field : schema .fields ()) {
522- String fieldName = scrubName (field .name ());
523- Object fieldCtx = getFieldType (ctx , fieldName );
524+ String fieldName = field .name ();
525+ if (useJsonFieldNames ) {
526+ fieldName = fromJsonCase (fieldName );
527+ }
528+ String scrubbedFieldName = scrubName (fieldName );
529+ Object fieldCtx = getFieldType (ctx , scrubbedFieldName );
524530 Object connectFieldVal = ignoreDefaultForNullables
525- ? struct .getWithoutDefault (field . name () ) : struct .get (field );
531+ ? struct .getWithoutDefault (fieldName ) : struct .get (field );
526532 Object fieldValue = fromConnectData (
527533 fieldCtx ,
528534 field .schema (),
@@ -539,10 +545,10 @@ private Object fromConnectData(
539545 fieldValue = union .getValue ();
540546 } else {
541547 fieldDescriptor = messageBuilder .getDescriptorForType ()
542- .findFieldByName (fieldName );
548+ .findFieldByName (scrubbedFieldName );
543549 }
544550 if (fieldDescriptor == null ) {
545- throw new DataException ("Cannot find field with name " + fieldName );
551+ throw new DataException ("Cannot find field with name " + scrubbedFieldName );
546552 }
547553 if (fieldValue != null ) {
548554 messageBuilder .setField (fieldDescriptor , fieldValue );
@@ -727,12 +733,16 @@ private MessageDefinition messageDefinitionFromConnectSchema(
727733 String fieldTag = fieldSchema .parameters () != null ? fieldSchema .parameters ()
728734 .get (PROTOBUF_TYPE_TAG ) : null ;
729735 int tag = fieldTag != null ? Integer .parseInt (fieldTag ) : index ++;
736+ String fieldName = field .name ();
737+ if (useJsonFieldNames ) {
738+ fieldName = fromJsonCase (fieldName );
739+ }
730740 FieldDefinition fieldDef = fieldDefinitionFromConnectSchema (
731741 ctx ,
732742 schema ,
733743 message ,
734744 fieldSchema ,
735- scrubName (field . name () ),
745+ scrubName (fieldName ),
736746 tag
737747 );
738748 if (fieldDef != null ) {
@@ -763,6 +773,22 @@ private MessageDefinition messageDefinitionFromConnectSchema(
763773 return message .build ();
764774 }
765775
776+ private static String fromJsonCase (final String str ) {
777+ final StringBuilder sb = new StringBuilder ();
778+ for (int i = 0 ; i < str .length (); i ++) {
779+ char c = str .charAt (i );
780+ if (Character .isUpperCase (c )) {
781+ if (i != 0 ) {
782+ sb .append ("_" );
783+ }
784+ sb .append (Character .toLowerCase (c ));
785+ } else {
786+ sb .append (c );
787+ }
788+ }
789+ return sb .toString ();
790+ }
791+
766792 private void oneofDefinitionFromConnectSchema (
767793 FromConnectContext ctx ,
768794 DynamicSchema .Builder schema ,
@@ -776,12 +802,17 @@ private void oneofDefinitionFromConnectSchema(
776802 String fieldTag = fieldSchema .parameters () != null ? fieldSchema .parameters ()
777803 .get (PROTOBUF_TYPE_TAG ) : null ;
778804 int tag = fieldTag != null ? Integer .parseInt (fieldTag ) : 0 ;
805+ String fieldName = field .name ();
806+ if (useJsonFieldNames ) {
807+ fieldName = fromJsonCase (fieldName );
808+ }
809+ String scrubbedFieldName = scrubName (fieldName );
779810 FieldDefinition fieldDef = fieldDefinitionFromConnectSchema (
780811 ctx ,
781812 schema ,
782813 message ,
783814 field .schema (),
784- scrubName ( field . name ()) ,
815+ scrubbedFieldName ,
785816 tag
786817 );
787818 if (fieldDef != null ) {
@@ -1363,7 +1394,8 @@ private void setStructField(
13631394 Struct result ,
13641395 FieldDescriptor fieldDescriptor
13651396 ) {
1366- final String fieldName = fieldDescriptor .getName ();
1397+ final String fieldName = useJsonFieldNames
1398+ ? fieldDescriptor .getJsonName () : fieldDescriptor .getName ();
13671399 final Field field = schema .field (fieldName );
13681400 if ((isPrimitiveOrRepeated (fieldDescriptor ) && !isOptional (fieldDescriptor ))
13691401 || (generateStructForNulls || message .hasField (fieldDescriptor ))) {
@@ -1425,7 +1457,9 @@ private SchemaBuilder toConnectSchema(
14251457 // Already added field as oneof
14261458 continue ;
14271459 }
1428- builder .field (fieldDescriptor .getName (), toConnectSchema (ctx , fieldDescriptor ));
1460+ final String fieldName = useJsonFieldNames
1461+ ? fieldDescriptor .getJsonName () : fieldDescriptor .getName ();
1462+ builder .field (fieldName , toConnectSchema (ctx , fieldDescriptor ));
14291463 }
14301464 }
14311465
0 commit comments