Skip to content
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

Closed
vorburger opened this issue May 12, 2023 · 3 comments
Closed

Document how to parse a .proto into a ProtoFile instance #2458

vorburger opened this issue May 12, 2023 · 3 comments

Comments

@vorburger
Copy link

vorburger commented May 12, 2023

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 a ProtoFile 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?

@oldergod
Copy link
Member

You can use SchemaLoader. Add your roots and call loadSchema. You'll get a Schema object which will have all info.

If you don't need metadata or type resolution, you can use ProtoParser.parse()

@jmini
Copy link

jmini commented Dec 27, 2023

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 wire-schema-jvm library (and not just wire-schema):

com.squareup.wire:wire-schema-jvm:4.9.3

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);
            }
        }
    }
}

@MPeli
Copy link

MPeli commented Mar 25, 2024

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants