Skip to content
Open
Changes from 8 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
56 changes: 24 additions & 32 deletions android/code_generator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -257,11 +257,11 @@ bool CodeGenerator::Generate(
printer.Print(
"\n"
"import com.fasterxml.jackson.annotation.JsonProperty;\n"
"import com.fasterxml.jackson.core.JsonProcessingException;\n"
"import com.fasterxml.jackson.annotation.JsonValue;\n"
"import com.fasterxml.jackson.databind.ObjectMapper;\n"
"\n"
"import java.io.IOException;\n"
"import java.io.InputStream;\n"
"import java.io.Serializable;\n"
"import java.util.List;\n"
"\n");

Expand Down Expand Up @@ -296,17 +296,26 @@ void CodeGenerator::GenDescriptor(
const google::protobuf::Descriptor *message,
google::protobuf::io::Printer *printer)
{
printer->Print("public static class $name$ {\n\n",
printer->Print("public static class $name$ implements Serializable {\n\n",
"name", message->name());
printer->Indent();

printer->Print("private static final long serialVersionUID = -$number$L;\n\n",
"number", std::to_string(rand() % 9999999999999 + 1000000000000));

for (int i = 0; i < message->field_count(); ++i) {
const google::protobuf::FieldDescriptor *field = message->field(i);
printer->Print("@JsonProperty(\"$name$\") $type$ $name$;\n",
printer->Print("public @JsonProperty(\"$name$\") $type$ $name$;\n",
"name", field->camelcase_name(),
"type", AndroidTypeForField(field, false));
}
printer->Print("\n");

if (0 != message->field_count()) {
printer->Print("public $name$() { /* no-op */ }\n\n",
"name", message->name());
}

printer->Print("public $name$(",
"name", message->name());
for (int i = 0, lastI = message->field_count() - 1; i <= lastI; ++i) {
Expand All @@ -328,9 +337,6 @@ void CodeGenerator::GenDescriptor(
printer->Outdent();
printer->Print("}\n\n");

CodeGenerator::GenMessage_toWriter(message, printer);
printer->Print("\n");

CodeGenerator::GenMessage_fromReader(message, printer);
printer->Print("\n");

Expand Down Expand Up @@ -368,13 +374,13 @@ void CodeGenerator::GenMessage_fromReader(
const google::protobuf::Descriptor *message,
google::protobuf::io::Printer *printer)
{
printer->Print("public static $name$ fromReader(InputStream inputStream) {\n",
printer->Print("public static $name$ fromReader(String json) {\n",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There should be no more from reader

"name", message->name());
printer->Indent();

printer->Print("try {\n");
printer->Indent();
printer->Print("return new ObjectMapper().readValue(inputStream, $name$.class);\n",
printer->Print("return new ObjectMapper().readValue(json, $name$.class);\n",
"name", message->name());
printer->Outdent();
printer->Print("} catch (IOException e) {\n");
Expand Down Expand Up @@ -489,26 +495,6 @@ void CodeGenerator::GenMessageBuilder(
printer->Print("}\n");
}

void CodeGenerator::GenMessage_toWriter(
const google::protobuf::Descriptor *message,
google::protobuf::io::Printer *printer)
{
printer->Print("public String toWriter() {\n");
printer->Indent();

printer->Print("try {\n");
printer->Indent();
printer->Print("return new ObjectMapper().writer().withDefaultPrettyPrinter().writeValueAsString(this);\n");
printer->Outdent();
printer->Print("} catch (JsonProcessingException e) {\n");
printer->Indent();
printer->Print("e.printStackTrace();\n");
printer->Outdent();
printer->Print("}\n\nreturn null;\n");
printer->Outdent();
printer->Print("}\n");
}

void CodeGenerator::GenMessage_equality(
const google::protobuf::Descriptor *message,
google::protobuf::io::Printer *printer)
Expand All @@ -528,8 +514,8 @@ void CodeGenerator::GenMessage_equality(

for (int i = 0, lastI = message->field_count() - 1; i <= lastI; ++i) {
const google::protobuf::FieldDescriptor *field = message->field(i);
printer->Print("this.$name$ == castObject.$name$",
"name", field->camelcase_name());
printer->Print(field->type() != google::protobuf::FieldDescriptor::TYPE_STRING ? "this.$name$ == castObject.$name$" : "this.$name$.equals(castObject.$name$)",
"name", field->camelcase_name());
if (i != lastI) {
printer->Print(" &&\n");
}
Expand Down Expand Up @@ -560,9 +546,15 @@ void CodeGenerator::GenEnum(
printer->Indent();
for (int i = 0; i < enum_desc->value_count(); ++i) {
string number = to_string(enum_desc->value(i)->number());
printer->Print("$key$,\n",
printer->Print("$key$",
"key", ToCamelCase(enum_desc->value(i)->name(), false));
printer->Print(i == enum_desc->value_count() - 1 ? ";\n\n" : ",\n");
}
printer->Print("@JsonValue\npublic int value() {\n");
printer->Indent();
printer->Print("return ordinal();\n");
printer->Outdent();
printer->Print("}\n");
printer->Outdent();
printer->Print("}\n\n");
}
Expand Down