Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions pbjson-build/src/generator/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ pub fn generate_message<W: Write>(
btree_map_paths: &[String],
emit_fields: bool,
preserve_proto_field_names: bool,
emit_struct_fields_null: bool,
) -> Result<()> {
let rust_type = resolver.rust_type(&message.path);

Expand All @@ -54,6 +55,7 @@ pub fn generate_message<W: Write>(
writer,
emit_fields,
preserve_proto_field_names,
emit_struct_fields_null,
)?;
write_serialize_end(0, writer)?;

Expand Down Expand Up @@ -116,6 +118,7 @@ fn write_message_serialize<W: Write>(
writer: &mut W,
emit_fields: bool,
preserve_proto_field_names: bool,
emit_struct_fields_null: bool,
) -> Result<()> {
write_struct_serialize_start(indent, message, writer, emit_fields)?;

Expand All @@ -127,6 +130,7 @@ fn write_message_serialize<W: Write>(
writer,
emit_fields,
preserve_proto_field_names,
emit_struct_fields_null,
)?;
}

Expand Down Expand Up @@ -404,6 +408,7 @@ fn write_serialize_field<W: Write>(
writer: &mut W,
emit_fields: bool,
preserve_proto_field_names: bool,
emit_struct_fields_null: bool,
) -> Result<()> {
let as_ref = format!("&self.{}", field.rust_field_name());
let variable = Variable {
Expand Down Expand Up @@ -443,6 +448,22 @@ fn write_serialize_field<W: Write>(
writer,
preserve_proto_field_names,
)?;
if emit_struct_fields_null {
writeln!(writer, "{}}} else {{", Indent(indent))?;

let json_name = field.json_name();
let field_name = if preserve_proto_field_names {
field.name.as_str()
} else {
json_name.as_str()
};
writeln!(
writer,
"{}struct_ser.serialize_field(\"{}\", &None::<Option<()>>)?;",
Indent(indent + 1),
field_name,
)?;
}
writeln!(writer, "{}}}", Indent(indent))?;
}
FieldModifier::Repeated | FieldModifier::UseDefault => {
Expand Down
8 changes: 8 additions & 0 deletions pbjson-build/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ pub struct Builder {
emit_fields: bool,
use_integers_for_enums: bool,
preserve_proto_field_names: bool,
emit_struct_field_null: bool,
}

impl Builder {
Expand Down Expand Up @@ -200,6 +201,12 @@ impl Builder {
self
}

/// Output optional struct fields with None value as "null", instead of omitted.
pub fn emit_struct_field_null(&mut self) -> &mut Self {
self.emit_struct_field_null = true;
self
}

/// Generates code for all registered types where `prefixes` contains a prefix of
/// the fully-qualified path of the type
pub fn build<S: AsRef<str>>(&mut self, prefixes: &[S]) -> Result<()> {
Expand Down Expand Up @@ -289,6 +296,7 @@ impl Builder {
&self.btree_map_paths,
self.emit_fields,
self.preserve_proto_field_names,
self.emit_struct_field_null,
)?
}
}
Expand Down