Java 类com.google.protobuf.util.JsonFormat.Printer 实例源码

项目:teleport    文件:GcsToDatastoreTest.java   
@Test
public void testGcsToDatastore_EntityToJson_noTransform() throws Exception {
  DoFnTester<String, Entity> fnTester = DoFnTester.of(JsonToEntity.newBuilder()
      .setJsTransformPath(StaticValueProvider.of(null))
      .setJsTransformFunctionName(StaticValueProvider.of(null))
      .build());
  List<Entity> output = fnTester.processBundle(mEntityJson);
  Entity outputEntity = output.get(0);

  Printer printer = JsonFormat.printer()
      .omittingInsignificantWhitespace()
      .usingTypeRegistry(
          TypeRegistry.newBuilder()
              .add(Entity.getDescriptor())
              .build());
  Assert.assertEquals(mEntityJson, printer.print(outputEntity));
}
项目:GDSC-SMLM    文件:SettingsManager.java   
/**
 * Write the message to a JSON string.
 *
 * @param message
 *            the message
 * @param flags
 *            the flags
 * @return the JSON string
 */
public static boolean toJSON(MessageOrBuilder message, Appendable output, int flags)
{
    try
    {
        if (printer == null)
            printer = JsonFormat.printer();
        Printer p = printer;
        if (BitFlags.anyNotSet(flags, FLAG_JSON_WHITESPACE))
            p = p.omittingInsignificantWhitespace();
        if (BitFlags.anySet(flags, FLAG_JSON_DEFAULT_VALUES))
            p = p.includingDefaultValueFields();
        p.appendTo(message, output);
        return true;
    }
    catch (IOException e)
    {
        if (BitFlags.anyNotSet(flags, FLAG_SILENT))
            IJ.log("Unable to write message: " + e.getMessage());
    }
    return false;
}
项目:GDSC-SMLM    文件:FilePeakResults.java   
private Printer addMessage(StringBuilder sb, Printer printer, String name, MessageOrBuilder msg)
{
    try
    {
        if (printer == null)
            printer = JsonFormat.printer().omittingInsignificantWhitespace()
            //.includingDefaultValueFields()
            ;
        sb.append(String.format("#%s %s\n", name, printer.print(msg)));
    }
    catch (InvalidProtocolBufferException e)
    {
        // This shouldn't happen so throw it
        throw new NotImplementedException("Unable to serialise the " + name + " settings", e);
    }
    return printer;
}
项目:grpc-java    文件:ProtoUtils.java   
/**
 * Create a {@code Marshaller} for json protos of the same type as {@code defaultInstance}.
 *
 * <p>This is an unstable API and has not been optimized yet for performance.
 */
@ExperimentalApi("https://github.com/grpc/grpc-java/issues/1786")
public static <T extends Message> Marshaller<T> jsonMarshaller(final T defaultInstance) {
  final Parser parser = JsonFormat.parser();
  final Printer printer = JsonFormat.printer();
  return jsonMarshaller(defaultInstance, parser, printer);
}
项目:GDSC-SMLM    文件:FilePeakResults.java   
protected String createResultsHeader()
{
    StringBuilder sb = new StringBuilder();

    addComment(sb, getHeaderTitle());
    sb.append(String.format("#FileVersion %s\n", getVersion()));

    Printer printer = null;

    // Add the standard details
    if (!TextUtils.isNullOrEmpty(getName()))
        sb.append(String.format("#Name %s\n", singleLine(getName())));
    if (getSource() != null)
        sb.append(String.format("#Source %s\n", singleLine(getSource().toXML())));
    if (getBounds() != null)
        sb.append(String.format("#Bounds x%d y%d w%d h%d\n", getBounds().x, getBounds().y, getBounds().width, getBounds().height));
    if (getCalibration() != null)
        printer = addMessage(sb, printer, "Calibration", getCalibration());
    if (!TextUtils.isNullOrEmpty(getConfiguration()))
        sb.append(String.format("#Configuration %s\n", singleLine(getConfiguration())));
    if (getPSF() != null)
        printer = addMessage(sb, printer, "PSF", getPSF());

    // Add any extra comments
    String[] comments = getHeaderComments();
    if (comments != null)
    {
        for (String comment : comments)
            addComment(sb, comment);
    }

    // Output the field names
    String[] fields = getFieldNames();
    if (fields != null)
    {
        sb.append('#');
        for (int i = 0; i < fields.length; i++)
        {
            if (i != 0)
                sb.append('\t');
            sb.append(fields[i]);
        }
        sb.append('\n');
    }

    addComment(sb, getHeaderEnd());

    return sb.toString();
}
项目:GDSC-SMLM    文件:PSFProtosTest.java   
@Test
public void canWriteAndReadString() throws ParseException, InvalidProtocolBufferException
{
    PSFProtos.PSF.Builder psfBuilder = PSFProtos.PSF.newBuilder();
    PSFParameter.Builder psfParamBuilder = PSFProtos.PSFParameter.newBuilder();
    psfBuilder.setPsfType(PSFType.TWO_AXIS_AND_THETA_GAUSSIAN_2D);
    psfParamBuilder.setName("X\"SD");
    psfParamBuilder.setUnit(PSFParameterUnit.DISTANCE);
    psfParamBuilder.setValue(1.1);
    psfBuilder.addParameters(psfParamBuilder);
    psfParamBuilder.setName("Y SD");
    psfParamBuilder.setUnit(PSFParameterUnit.DISTANCE);
    psfParamBuilder.setValue(1.2);
    psfBuilder.addParameters(psfParamBuilder);
    psfParamBuilder.setName("'Angle");
    psfParamBuilder.setUnit(PSFParameterUnit.ANGLE);
    psfParamBuilder.clearValue();
    psfBuilder.addParameters(psfParamBuilder);

    // Standard string
    String e = psfBuilder.toString();
    PSFProtos.PSF psf = psfBuilder.build();
    String o = psf.toString();
    System.out.printf(o);
    Assert.assertEquals(e, o);

    psfBuilder.clear();
    TextFormat.merge(o, psfBuilder);
    Assert.assertTrue("Merge string", psf.equals(psfBuilder.build()));

    // Short string
    String o2 = TextFormat.shortDebugString(psf);
    System.out.println(o2);

    psfBuilder.clear();
    TextFormat.merge(o2, psfBuilder);
    Assert.assertTrue("Merge short string", psf.equals(psfBuilder.build()));

    // JSON
    Printer printer = JsonFormat.printer().omittingInsignificantWhitespace();
    String json = printer.print(psf);
    System.out.println(json);
    json = JSONUtils.simplify(json);
    System.out.println(json);

    psfBuilder.clear();
    JsonFormat.parser().merge(json, psfBuilder);
    Assert.assertTrue("Merge JSON", psf.equals(psfBuilder.build()));
}