-
Notifications
You must be signed in to change notification settings - Fork 570
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Document how to parse a .proto into a ProtoFile instance #2458
Comments
You can use If you don't need metadata or type resolution, you can use |
In case somebody finds this discussion, some more details: wire is a Kotlin multi-platform library, so to use it in a java application, you need the
Example code: import java.nio.file.FileSystems;
import java.util.List;
import com.squareup.wire.schema.Field;
import com.squareup.wire.schema.Location;
import com.squareup.wire.schema.MessageType;
import com.squareup.wire.schema.ProtoFile;
import com.squareup.wire.schema.Schema;
import com.squareup.wire.schema.SchemaLoader;
import com.squareup.wire.schema.Type;
public class ExampleMain {
public static void main(String[] args) {
SchemaLoader loader = new SchemaLoader(FileSystems.getDefault());
List<Location> sourcePath = List.of(Location.get("/<path to project>/src/main/proto"));
List<Location> protoPath = List.of();
loader.initRoots(sourcePath, protoPath);
Schema schema = loader.loadSchema();
List<ProtoFile> protoFiles = schema.getProtoFiles();
for (ProtoFile protoFile : protoFiles) {
System.out.println(protoFile);
}
Type type = schema.getType("com.company.Address");
if (type instanceof MessageType) {
MessageType messageType = (MessageType) type;
for (Field f : messageType.getDeclaredFields()) {
System.out.println(f);
}
}
}
} |
Hi, I've successfully parsed a proto file and now have instances of ProtoFile and a protobuf. How can I deserialize a message using ProtoFile? I'm specifically interested in obtaining a JSON string as output. Thank you. |
I got to this project via the now archived https://github.com/square/protoparser (which is a top hit when searching something like "proto parser in java"), from here for this, and am wondering (and starting to dig around the code...) to try to figure how exactly to use
wire-schema
in this project to parse a.proto
into aProtoFile
instance?Perhaps the README of this project, and/or of the old
square/protoparser
, could include a link to a simple test/example which illustrates this?The text was updated successfully, but these errors were encountered: