Pdef java generator

pip install pdef-java==1.2.0


Pdef Java

Java code generator for Pdef compiler and Java implementation of descriptors, JSON format and HTTP RPC.



  • Code generator:

    $ pip install pdef-java

    Or download the release, unzip it and in the generator directory run:

    $ python setup.py install
  • Java package (maven):


Code generation

Pass a pdef package path or a url to the compiler:

$ pdefc generate-java https://raw.github.com/pdef/pdef/1.2/example/world.yaml \
    --out target/generated-sources

The generator uses absolute module names (package.module) as java package names. Use the --module argument to manually map pdef modules to java packages. Also it is possible to add namespace class prefixes via the --prefix argument.

$ pdefc generate-java https://raw.github.com/pdef/pdef/1.2/example/world.yaml \
    --prefix world:W \
    --module world:com.mycompany.world \
    --module world.space:com.mycompany.common \
    --out target/generated-sources


Generated messages implement equals, hashCode, copy constructors, a copy method which returns a deep copy of a message, and merging methods. The messages are not thread-safe. The examples are based on the pdef example package.

Messages have a fluent interface.

Human human = new Human()

Human copy0 = human.copy();
Human copy1 = new Human(human);

assert human.equals(copy0);
assert human.equals(copy1);

Messages support merging which deep copies set fields from a source message to destination one:

Human human = new Human()

Human another = new Human();

assert another.getName().equals(human.getName());

Messages try to be null-safe and return default values for null fields. If a null field is a collection or a message then it is initialized to an empty object on the first access.

// All getters return the default values when the fields are not present or null.
Human human = new Human();
assert human.getId() == 0;
assert human.getName().equals("");

// Special methods allow to check if the field is present.
assert human.hasId() == false;
assert human.hasName() == false;

// Collection and message getters initialize null fields to empty objects.
Continent continent = new Continent();
assert continent.hasHumans() == false;

// The collection is initialized to an empty one.
assert continent.getHumans().equals(Arrays.asList(human));
assert continent.hasHumans();

// Clear fields.

JSON Format

JSON serialization is based on the Jackson parser (not the data binding package):

// To a JSON-compatible map.
Map<String, Object> map = human.toMap();

// To JSON string.
String json = human.toJson();

// To pretty-printed JSON string.
json = human.toJson(true);

// Write to an output stream.
OutputStream stream = getOutput();
human.toJson(stream, true);  // indent=true/false.

// Write to a print writer.
PrintWriter writer = getWriter();
human.toJson(writer, true);


// From a JSON compatible map (only JSON primitives and collections).
Map<String, Object> map = new HashMap<>;
map.put("name", "John");
map.put("birthday", "2012-01-01T01:12:33Z");
map.put("sex", "male");

Human human0 = Human.fromMap(map);
assert human0.getName().equals("John");

// From a JSON-string:
String json = getJson();
Human human1 = Human.fromJson(json);

// Merging for parsing input streams and readers.

Use JsonFormat to read/write other pdef data types:

// Convert an int to a JSON string.
String json = JsonFormat.write(Descriptors.int32, 123);

// Write a list of integers to an output stream.
OutputStream output = getOutput();
ListDescriptor<Integer> listDescriptor = Descriptors.list(Descriptors.int32);
JsonFormat.write(output, listDescriptor, Arrays.asList(1, 2, 3));

// Read a message from an input stream.
InputStream input = getInput();
Human human = JsonFormat.read(Human.DESCRIPTOR, input);


Client and server implementations are thread-safe.

Create an HTTP RPC client based on the HttpUrlConnection.

RpcClient<World> client = new RpcClient<World>(World.DESCRIPTOR, "http://example.com/world/");
World world = client.proxy();

// Execute a remote method.
List<Human> humans = world.humans().all(10, 0); // limit=10, offset=0.

// Execute a void remote method.

Null primitive results are automatically converted into default values.

// It is null-safe to write:
int result = calculator.sum(1, 2);

Create an RPC client with a custom RpcSession:

RpcSession session = createCustomSession();
RpcClient<World> client = new RpcClient<World>(World.DESCRIPTOR, session);
World world = client.proxy();

Full client example:

RpcSession session = new HttpUrlConnectionRpcSession("http://example.com/world/");
RpcClient<World> client = new RpcClient<World>(World.DESCRIPTOR, session);
Invoker invoker = client;
World world = InvocationProxy.create(World.DESCRIPTOR, invoker);

To add custom headers or other HTTP logic subclass HttpUrlConnectionRpcSession, or implement a custom RpcSession.


Create an HTTP RPC handler:

World world = getWorldImplementation();
RpcHandler<World> handler = new RpcHandler<World>(World.DESCRIPTOR, world);
RpcServlet<World> servlet = new RpcServlet<World>(handler);
// Pass the servlet to your servlet container,
// or wrap in another servlet as a delegate.

Use a custom provider when you need to get a fresh service instance for each request:

Provider<World> provider = getWorldProvider();
RpcHandler<World> handler = new RpcHandler<World>(World.DESCRIPTOR, provider);

Primitive null arguments are automatically converted into the default values.

class MyHumans implements Humans {
    public List<world.Human> all(int limit, int offset) {
        // Null limit and offset are set to 0.
        return null;

Wrap an RpcServlet in another servlet as a delegate to add custom headers and custom HTTP logic (authentication, rate-limiting, etc).

RpcHandler takes a simple bean-like RpcRequest and returns an RpcResult which allows to use it with custom HTTP transports such as Netty.

License and Copyright

Copyright: 2013 Ivan Korobkov ivan.korobkov@gmail.com

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at:


Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.