当前位置: 首页>>代码示例>>Java>>正文


Java Metadata.AsciiMarshaller方法代码示例

本文整理汇总了Java中io.grpc.Metadata.AsciiMarshaller方法的典型用法代码示例。如果您正苦于以下问题:Java Metadata.AsciiMarshaller方法的具体用法?Java Metadata.AsciiMarshaller怎么用?Java Metadata.AsciiMarshaller使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在io.grpc.Metadata的用法示例。


在下文中一共展示了Metadata.AsciiMarshaller方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: JSON_MARSHALLER

import io.grpc.Metadata; //导入方法依赖的package包/类
/**
 * A metadata marshaller that encodes objects as JSON using the google-gson library.
 *
 * <p>All non-ascii characters are unicode escaped to comply with {@code AsciiMarshaller}'s character range
 * requirements.
 *
 * @param clazz the type to serialize
 * @param <T>
 */
public static final <T> Metadata.AsciiMarshaller<T> JSON_MARSHALLER(Class<T> clazz) {
    return new Metadata.AsciiMarshaller<T>() {
        TypeToken<T> typeToken = TypeToken.of(clazz);
        private Gson gson = new Gson();

        @Override
        public String toAsciiString(T value) {
            try {
                try (StringWriter sw = new StringWriter()) {
                    gson.toJson(value, typeToken.getType(), new UnicodeEscapingAsciiWriter(sw));
                    return sw.toString();
                }
            } catch (IOException e) {
                throw new IllegalArgumentException(e);
            }
        }

        @Override
        public T parseAsciiString(String serialized) {
            return gson.fromJson(serialized, typeToken.getType());
        }
    };
}
 
开发者ID:salesforce,项目名称:grpc-java-contrib,代码行数:33,代码来源:MoreMetadata.java

示例2: jsonMarshallerRoundtrip

import io.grpc.Metadata; //导入方法依赖的package包/类
@Test
public void jsonMarshallerRoundtrip() {
    Foo foo = new Foo();
    foo.country = "France";
    List<Bar> bars = new ArrayList<>();
    Bar bar1 = new Bar();
    bar1.cheese = "Brë";
    bar1.age = 2;
    bars.add(bar1);
    Bar bar2 = new Bar();
    bar2.cheese = "Guda<>'";
    bar2.age = 4;
    bars.add(bar2);
    foo.bars = bars;

    Metadata.AsciiMarshaller<Foo> marshaller = MoreMetadata.JSON_MARSHALLER(Foo.class);
    String str = marshaller.toAsciiString(foo);
    assertThat(str).doesNotContain("ë");

    Foo foo2 = marshaller.parseAsciiString(str);
    assertThat(foo2).isEqualTo(foo);
}
 
开发者ID:salesforce,项目名称:grpc-java-contrib,代码行数:23,代码来源:MoreMetadataTest.java

示例3: jsonMarshallerPrimitiveRoundtrip

import io.grpc.Metadata; //导入方法依赖的package包/类
@Test
public void jsonMarshallerPrimitiveRoundtrip() {
    Metadata.AsciiMarshaller<Integer> marshaller = MoreMetadata.JSON_MARSHALLER(Integer.class);
    String s = marshaller.toAsciiString(42);
    assertThat(s).isEqualTo("42");

    Integer l = marshaller.parseAsciiString(s);
    assertThat(l).isEqualTo(42);
}
 
开发者ID:salesforce,项目名称:grpc-java-contrib,代码行数:10,代码来源:MoreMetadataTest.java

示例4: booleanMarshallerRountrip

import io.grpc.Metadata; //导入方法依赖的package包/类
@Test
public void booleanMarshallerRountrip() {
    Metadata.AsciiMarshaller<Boolean> marshaller = MoreMetadata.BOOLEAN_MARSHALLER;
    String s = marshaller.toAsciiString(Boolean.TRUE);
    assertThat(s).isEqualTo("true");

    Boolean b = marshaller.parseAsciiString(s);
    assertThat(b).isTrue();
}
 
开发者ID:salesforce,项目名称:grpc-java-contrib,代码行数:10,代码来源:MoreMetadataTest.java

示例5: longMarshallerRountrip

import io.grpc.Metadata; //导入方法依赖的package包/类
@Test
public void longMarshallerRountrip() {
    Metadata.AsciiMarshaller<Long> marshaller = MoreMetadata.LONG_MARSHALLER;
    String s = marshaller.toAsciiString(42L);
    assertThat(s).isEqualTo("42");

    Long l = marshaller.parseAsciiString(s);
    assertThat(l).isEqualTo(42L);
}
 
开发者ID:salesforce,项目名称:grpc-java-contrib,代码行数:10,代码来源:MoreMetadataTest.java

示例6: doubleMarshallerRountrip

import io.grpc.Metadata; //导入方法依赖的package包/类
@Test
public void doubleMarshallerRountrip() {
    Metadata.AsciiMarshaller<Double> marshaller = MoreMetadata.DOUBLE_MARSHALLER;
    String s = marshaller.toAsciiString(42.42);
    assertThat(s).isEqualTo("42.42");

    Double d = marshaller.parseAsciiString(s);
    assertThat(d).isEqualTo(42.42);
}
 
开发者ID:salesforce,项目名称:grpc-java-contrib,代码行数:10,代码来源:MoreMetadataTest.java


注:本文中的io.grpc.Metadata.AsciiMarshaller方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。