本文整理汇总了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());
}
};
}
示例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);
}
示例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);
}
示例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();
}
示例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);
}
示例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);
}