CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/maven-com-google-protobuf--protobuf-java-util

Utilities for Protocol Buffers including JSON format conversion, field mask operations, time-based utilities, and structured data manipulation.

Pending
Overview
Eval results
Files

json-format.mddocs/

JSON Format Conversion

Complete bidirectional conversion system between protobuf messages and JSON format, supporting the Proto3 JSON mapping specification. Provides configurable options for field naming, default value handling, type registries, and output formatting.

Capabilities

Printer Creation

Creates a printer instance with default configurations for converting protobuf messages to JSON.

/**
 * Creates a Printer with default configurations.
 * 
 * @return A new Printer instance with default settings
 */
public static JsonFormat.Printer printer();

Parser Creation

Creates a parser instance with default configurations for converting JSON to protobuf messages.

/**
 * Creates a Parser with default configuration.
 * 
 * @return A new Parser instance with default settings
 */
public static JsonFormat.Parser parser();

Printer Configuration

Configure printer behavior for various output requirements.

/**
 * Creates a new Printer using the given registry for resolving Any types.
 * 
 * @param registry TypeRegistry for resolving Any message types
 * @return New Printer with the specified type registry
 * @throws IllegalArgumentException if a registry is already set
 */
public JsonFormat.Printer usingTypeRegistry(JsonFormat.TypeRegistry registry);

/**
 * Creates a new Printer using the given registry for resolving Any types.
 * 
 * @param registry com.google.protobuf.TypeRegistry for resolving Any message types
 * @return New Printer with the specified type registry
 * @throws IllegalArgumentException if a registry is already set
 */
public JsonFormat.Printer usingTypeRegistry(com.google.protobuf.TypeRegistry registry);

/**
 * Creates a new Printer that will always print fields unless they are a message type or in a oneof.
 * Note: This method is deprecated.
 * 
 * @return New Printer that includes default value fields
 * @deprecated Use alwaysPrintFieldsWithNoPresence() instead
 */
@Deprecated
public JsonFormat.Printer includingDefaultValueFields();

/**
 * Creates a new Printer that will print specified default-valued fields.
 * 
 * @param fieldsToAlwaysOutput Set of field descriptors to always print
 * @return New Printer with specified default field behavior
 */
public JsonFormat.Printer includingDefaultValueFields(Set<FieldDescriptor> fieldsToAlwaysOutput);

/**
 * Creates a new Printer that prints fields with no presence even if default.
 * 
 * @return New Printer that prints fields without presence
 */
public JsonFormat.Printer alwaysPrintFieldsWithNoPresence();

/**
 * Creates a new Printer that prints enum field values as integers.
 * 
 * @return New Printer that prints enums as integers
 */
public JsonFormat.Printer printingEnumsAsInts();

/**
 * Creates a new Printer that uses original proto field names instead of camelCase.
 * 
 * @return New Printer that preserves proto field names
 */
public JsonFormat.Printer preservingProtoFieldNames();

/**
 * Creates a new Printer that omits insignificant whitespace in JSON output.
 * 
 * @return New Printer that produces compact JSON
 */
public JsonFormat.Printer omittingInsignificantWhitespace();

/**
 * Creates a new Printer that sorts map keys in JSON output.
 * 
 * @return New Printer that sorts map keys
 */
public JsonFormat.Printer sortingMapKeys();

Message to JSON Conversion

Convert protobuf messages to JSON format with various output options.

/**
 * Converts a protobuf message to Proto3 JSON format.
 * 
 * @param message The protobuf message to convert
 * @param output The output destination for JSON
 * @throws InvalidProtocolBufferException if message contains unresolvable Any types
 * @throws IOException if writing to output fails
 */
public void appendTo(MessageOrBuilder message, Appendable output) throws IOException;

/**
 * Converts a protobuf message to Proto3 JSON format as a string.
 * 
 * @param message The protobuf message to convert
 * @return JSON string representation of the message
 * @throws InvalidProtocolBufferException if message contains unresolvable Any types
 */
public String print(MessageOrBuilder message) throws InvalidProtocolBufferException;

Parser Configuration

Configure parser behavior for handling various JSON input formats.

/**
 * Creates a new Parser using the given registry for resolving Any types.
 * 
 * @param registry TypeRegistry for resolving Any message types
 * @return New Parser with the specified type registry
 * @throws IllegalArgumentException if a registry is already set
 */
public JsonFormat.Parser usingTypeRegistry(JsonFormat.TypeRegistry registry);

/**
 * Creates a new Parser using the given registry for resolving Any types.
 * 
 * @param registry com.google.protobuf.TypeRegistry for resolving Any message types
 * @return New Parser with the specified type registry
 * @throws IllegalArgumentException if a registry is already set
 */
public JsonFormat.Parser usingTypeRegistry(com.google.protobuf.TypeRegistry registry);

/**
 * Creates a new Parser that ignores unknown fields instead of throwing exceptions.
 * 
 * @return New Parser that ignores unknown fields
 */
public JsonFormat.Parser ignoringUnknownFields();

JSON to Message Conversion

Parse JSON strings into protobuf message builders.

/**
 * Parses Proto3 JSON format into a protobuf message.
 * 
 * @param json JSON string to parse
 * @param builder Message builder to populate
 * @throws InvalidProtocolBufferException if JSON is invalid or contains unknown fields
 */
public void merge(String json, Message.Builder builder) throws InvalidProtocolBufferException;

/**
 * Parses Proto3 JSON from a Reader into a protobuf message.
 * 
 * @param json Reader containing JSON data
 * @param builder Message builder to populate
 * @throws InvalidProtocolBufferException if JSON is invalid or contains unknown fields
 * @throws IOException if reading from input fails
 */
public void merge(Reader json, Message.Builder builder) throws IOException;

Type Registry Management

Manage type registries for resolving Any message types during JSON conversion.

/**
 * Returns an empty TypeRegistry.
 * 
 * @return Empty TypeRegistry instance
 */
public static JsonFormat.TypeRegistry getEmptyTypeRegistry();

/**
 * Creates a new TypeRegistry builder.
 * 
 * @return New TypeRegistry.Builder instance
 */
public static JsonFormat.TypeRegistry.Builder newBuilder();

/**
 * Finds a type by its full name.
 * 
 * @param name Full name of the message type
 * @return Descriptor for the type, or null if not found
 */
public Descriptor find(String name);

Type Registry Builder

Build type registries with message types for Any resolution.

/**
 * Adds a message type and all transitively imported types to the registry.
 * 
 * @param messageType Message descriptor to add
 * @return This builder for method chaining
 */
public JsonFormat.TypeRegistry.Builder add(Descriptor messageType);

/**
 * Adds multiple message types and all transitively imported types to the registry.
 * 
 * @param messageTypes Iterable of message descriptors to add
 * @return This builder for method chaining
 */
public JsonFormat.TypeRegistry.Builder add(Iterable<Descriptor> messageTypes);

/**
 * Builds the TypeRegistry.
 * 
 * @return New TypeRegistry instance
 */
public JsonFormat.TypeRegistry build();

Usage Examples:

import com.google.protobuf.util.JsonFormat;
import com.google.protobuf.InvalidProtocolBufferException;

// Basic JSON conversion
JsonFormat.Printer printer = JsonFormat.printer();
String jsonString = printer.print(myMessage);

JsonFormat.Parser parser = JsonFormat.parser();
MyMessage.Builder builder = MyMessage.newBuilder();
parser.merge(jsonString, builder);
MyMessage parsed = builder.build();

// Custom printer configuration
JsonFormat.Printer customPrinter = JsonFormat.printer()
    .preservingProtoFieldNames()
    .omittingInsignificantWhitespace()
    .printingEnumsAsInts();

String compactJson = customPrinter.print(myMessage);

// Parser with unknown field handling
JsonFormat.Parser tolerantParser = JsonFormat.parser()
    .ignoringUnknownFields();

MyMessage.Builder tolerantBuilder = MyMessage.newBuilder();
tolerantParser.merge(jsonWithExtraFields, tolerantBuilder);

// Type registry for Any types
JsonFormat.TypeRegistry typeRegistry = JsonFormat.TypeRegistry.newBuilder()
    .add(MyCustomMessage.getDescriptor())
    .add(AnotherMessage.getDescriptor())
    .build();

JsonFormat.Printer anyPrinter = JsonFormat.printer()
    .usingTypeRegistry(typeRegistry);

JsonFormat.Parser anyParser = JsonFormat.parser()
    .usingTypeRegistry(typeRegistry);

Install with Tessl CLI

npx tessl i tessl/maven-com-google-protobuf--protobuf-java-util

docs

durations.md

field-mask.md

index.md

json-format.md

structured-data.md

timestamps.md

tile.json