Java Code Examples for io.grpc.Metadata#AsciiMarshaller

The following examples show how to use io.grpc.Metadata#AsciiMarshaller . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MoreMetadata.java    From grpc-java-contrib with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * 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 <T> Metadata.AsciiMarshaller<T> JSON_MARSHALLER(Class<T> clazz) {
    return new Metadata.AsciiMarshaller<T>() {
        private 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());
        }
    };
}
 
Example 2
Source File: MoreMetadataTest.java    From grpc-java-contrib with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@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);
}
 
Example 3
Source File: MoreMetadataTest.java    From grpc-java-contrib with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@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);
}
 
Example 4
Source File: MoreMetadataTest.java    From grpc-java-contrib with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@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();
}
 
Example 5
Source File: MoreMetadataTest.java    From grpc-java-contrib with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@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);
}
 
Example 6
Source File: MoreMetadataTest.java    From grpc-java-contrib with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@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);
}