a json file dumper for a list of google structs - krickert/search-api GitHub Wiki
Below is an example Java utility method that takes in a list of Protobuf Struct
objects and a file path (as a string) and writes them out as a single JSON file. In this example, each Struct
is converted to its JSON representation using Protobuf’s JsonFormat.printer()
, and then all objects are wrapped in a JSON array.
import com.google.protobuf.Struct;
import com.google.protobuf.util.JsonFormat;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.List;
public final class JsonFileDumper {
private JsonFileDumper() {
// Utility class; prevent instantiation.
}
/**
* Converts a list of Protobuf Structs to a JSON array and writes it to the specified file.
*
* @param structs the list of Struct objects to convert
* @param filePath the file path where the JSON output will be written
* @throws IOException if writing to the file fails
*/
public static void dumpStructsToJsonFile(List<Struct> structs, String filePath) throws IOException {
// Use Protobuf's JsonFormat to print each Struct as JSON.
JsonFormat.Printer printer = JsonFormat.printer();
// Build a JSON array string.
StringBuilder sb = new StringBuilder();
sb.append("[\n");
for (int i = 0; i < structs.size(); i++) {
String json = printer.print(structs.get(i));
sb.append(json);
if (i < structs.size() - 1) {
sb.append(",\n"); // add a comma between objects
}
}
sb.append("\n]");
// Write the JSON array string to the specified file.
Files.write(Paths.get(filePath), sb.toString().getBytes(StandardCharsets.UTF_8));
}
// Example usage:
public static void main(String[] args) {
try {
// Create some sample Structs.
Struct struct1 = Struct.newBuilder()
.putFields("name", com.google.protobuf.Value.newBuilder().setStringValue("Alice").build())
.putFields("age", com.google.protobuf.Value.newBuilder().setNumberValue(30).build())
.build();
Struct struct2 = Struct.newBuilder()
.putFields("name", com.google.protobuf.Value.newBuilder().setStringValue("Bob").build())
.putFields("age", com.google.protobuf.Value.newBuilder().setNumberValue(25).build())
.build();
List<Struct> structs = List.of(struct1, struct2);
String outputPath = "output.json";
dumpStructsToJsonFile(structs, outputPath);
System.out.println("JSON file written to: " + outputPath);
} catch (IOException e) {
e.printStackTrace();
}
}
}
-
Conversion to JSON:
We useJsonFormat.printer().print(struct)
to convert each ProtobufStruct
into its JSON string representation. -
Building a JSON Array:
We build the output as a JSON array (i.e. wrapped in square brackets) with eachStruct
's JSON separated by commas. -
Writing to File:
TheFiles.write()
method from Java NIO writes the resulting JSON string to the file specified byfilePath
. -
Example Usage:
Themain
method provides an example of creating two sampleStruct
objects, dumping them to anoutput.json
file, and printing the file location.
This utility should give you a clean way to dump a list of Protobuf Struct
objects to a single JSON file.