Skip to content

Commit

Permalink
c# code gen doc
Browse files Browse the repository at this point in the history
Signed-off-by: Clemens Vasters <clemens@vasters.com>
  • Loading branch information
clemensv committed May 12, 2024
1 parent 1aaa889 commit b93a13d
Show file tree
Hide file tree
Showing 4 changed files with 684 additions and 2 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -431,6 +431,7 @@ Parameters:
- `--system-text-json-annotation`: (optional) If set, the tool will add System.Text.Json annotations to the C# classes.
- `--newtonsoft-json-annotation`: (optional) If set, the tool will add Newtonsoft.Json annotations to the C# classes.
- `--pascal-properties`: (optional) If set, the tool will use PascalCase properties in the C# classes.
- `--namespace`: (optional) The namespace to use in the generated C# classes.

Conversion notes:
- The tool generates C# classes that represent the Avro schema as data classes.
Expand All @@ -439,10 +440,13 @@ Conversion notes:
C# classes. Because the [`JSON Schema to Avro`](#convert-json-schema-to-avro-schema) conversion generally
preserves the JSON Schema structure in the Avro schema, the generated C# classes
can be used to serialize and deserialize data that is valid per the input JSON schema.
- Only the `--system-text-json-annotation` option generates classes that support type unions.
- The classes are generated into a directory structure that reflects the Avro namespace
structure. The tool drops a minimal, default `.csproj` project file into the given
directory if none exists.

Read more about the C# code generation in the [C# code generation documentation](csharpcodegen.md).


### Generate Java code from Avro schema

Expand Down
4 changes: 3 additions & 1 deletion avrotize/avrotize.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ def main():
a2csharp_parser.add_argument('--system-text-json-annotation', action='store_true', help='Use System.Text.Json annotations', default=False)
a2csharp_parser.add_argument('--newtonsoft-json-annotation', action='store_true', help='Use Newtonsoft.Json annotations', default=False)
a2csharp_parser.add_argument('--pascal-properties', action='store_true', help='Use PascalCase properties', default=False)
a2csharp_parser.add_argument('--namespace', type=str, help='C# namespace', required=False)

a2java_parser = subparsers.add_parser('a2java', help='Convert Avro schema to Java classes')
a2java_parser.add_argument('--avsc', type=str, help='Path to the Avro schema file', required=True)
Expand Down Expand Up @@ -188,8 +189,9 @@ def main():
system_text_json_annotation = args.system_text_json_annotation
newtonsoft_json_annotation = args.newtonsoft_json_annotation
pascal_properties = args.pascal_properties
namespace = args.namespace
print(f'Converting Avro {avro_schema_path} to C# {csharp_path}')
convert_avro_to_csharp(avro_schema_path, csharp_path, avro_annotation=avro_annotation, system_text_json_annotation=system_text_json_annotation, newtonsoft_json_annotation=newtonsoft_json_annotation, pascal_properties=pascal_properties)
convert_avro_to_csharp(avro_schema_path, csharp_path, base_namespace=namespace, avro_annotation=avro_annotation, system_text_json_annotation=system_text_json_annotation, newtonsoft_json_annotation=newtonsoft_json_annotation, pascal_properties=pascal_properties)
elif args.command == 'a2java':
avro_schema_path = args.avsc
java_path = args.java
Expand Down
3 changes: 2 additions & 1 deletion avrotize/avrotocsharp.py
Original file line number Diff line number Diff line change
Expand Up @@ -590,7 +590,8 @@ def generate_embedded_union(self, class_name: str, field_name: str, avro_type: L
f"public partial class {class_name}\n{{\n" + \
f"{INDENT}/// <summary>\n{INDENT}/// Union class for {field_name}\n{INDENT}/// </summary>\n"
if self.system_text_json_annotation:
f"{INDENT}[System.Text.Json.Serialization.JsonConverter(typeof({union_class_name}))]\n"
class_definition += \
f"{INDENT}[System.Text.Json.Serialization.JsonConverter(typeof({union_class_name}))]\n"
class_definition += \
f"{INDENT}public sealed class {union_class_name}"
if self.system_text_json_annotation:
Expand Down
Loading

0 comments on commit b93a13d

Please sign in to comment.