當前位置: 首頁>>代碼示例>>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;未經允許,請勿轉載。