Search in sources :

Example 6 with Root

use of org.apache.servicecomb.foundation.protobuf.internal.model.Root in project java-chassis by ServiceComb.

the class Protostuff method deserialize.

@Override
public Object deserialize(byte[] bytes) throws IOException {
    Input input = new ByteArrayInput(bytes, false);
    Root result = new Root();
    rootSchema.mergeFrom(input, result);
    return result;
}
Also used : ByteArrayInput(io.protostuff.ByteArrayInput) Input(io.protostuff.Input) ByteArrayInput(io.protostuff.ByteArrayInput) Root(org.apache.servicecomb.foundation.protobuf.internal.model.Root)

Example 7 with Root

use of org.apache.servicecomb.foundation.protobuf.internal.model.Root in project java-chassis by ServiceComb.

the class TestAnySchema method pack.

@Test
public void pack() throws Throwable {
    builder.setAny(Any.pack(ProtobufRoot.User.newBuilder().setName("n1").build()));
    check();
    Map<String, Object> map = new HashMap<>();
    map.put(ProtoConst.JSON_ID_NAME, "User");
    map.put("name", "n1");
    Root root = new Root();
    root.setAny(map);
    Assert.assertArrayEquals(protobufBytes, rootSerializer.serialize(root));
}
Also used : Root(org.apache.servicecomb.foundation.protobuf.internal.model.Root) ProtobufRoot(org.apache.servicecomb.foundation.protobuf.internal.model.ProtobufRoot) HashMap(java.util.HashMap) Test(org.junit.Test)

Example 8 with Root

use of org.apache.servicecomb.foundation.protobuf.internal.model.Root in project java-chassis by ServiceComb.

the class TestAnySchema method json_fromMapWithoutType.

@SuppressWarnings("unchecked")
@Test
public void json_fromMapWithoutType() throws Throwable {
    Map<String, Object> map = new HashMap<>();
    map.put("name", "n1");
    Root root = new Root();
    root.setAny(map);
    scbRootBytes = rootSerializer.serialize(root);
    root = rootDeserializer.deserialize(scbRootBytes);
    Assert.assertThat(root.getAny(), Matchers.instanceOf(Map.class));
    Assert.assertThat((Map<? extends String, ? extends String>) root.getAny(), Matchers.hasEntry("name", "n1"));
    RootDeserializer<Map<String, Object>> deserializer = protoMapper.createRootDeserializer("Root", Map.class);
    map = deserializer.deserialize(scbRootBytes);
    Assert.assertThat((Map<? extends String, ? extends String>) map.get("any"), Matchers.hasEntry("name", "n1"));
}
Also used : Root(org.apache.servicecomb.foundation.protobuf.internal.model.Root) ProtobufRoot(org.apache.servicecomb.foundation.protobuf.internal.model.ProtobufRoot) HashMap(java.util.HashMap) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.Test)

Example 9 with Root

use of org.apache.servicecomb.foundation.protobuf.internal.model.Root in project java-chassis by ServiceComb.

the class TestAnySchema method anys_json.

@Test
public void anys_json() throws IOException {
    Root root = new Root();
    root.setAnys(Arrays.asList("abc", "123"));
    scbRootBytes = rootSerializer.serialize(root);
    root = rootDeserializer.deserialize(scbRootBytes);
    Assert.assertThat(root.getAnys(), Matchers.contains("abc", "123"));
}
Also used : Root(org.apache.servicecomb.foundation.protobuf.internal.model.Root) ProtobufRoot(org.apache.servicecomb.foundation.protobuf.internal.model.ProtobufRoot) Test(org.junit.Test)

Example 10 with Root

use of org.apache.servicecomb.foundation.protobuf.internal.model.Root in project incubator-servicecomb-java-chassis by apache.

the class TestAnySchema method pack.

@Test
public void pack() throws Throwable {
    builder.setAny(Any.pack(ProtobufRoot.User.newBuilder().setName("n1").build()));
    check();
    Map<String, Object> map = new HashMap<>();
    map.put(ProtoConst.JSON_ID_NAME, "User");
    map.put("name", "n1");
    Root root = new Root();
    root.setAny(map);
    Assert.assertArrayEquals(protobufBytes, rootSerializer.serialize(root));
}
Also used : Root(org.apache.servicecomb.foundation.protobuf.internal.model.Root) ProtobufRoot(org.apache.servicecomb.foundation.protobuf.internal.model.ProtobufRoot) HashMap(java.util.HashMap) Test(org.junit.Test)

Aggregations

Root (org.apache.servicecomb.foundation.protobuf.internal.model.Root)12 ProtobufRoot (org.apache.servicecomb.foundation.protobuf.internal.model.ProtobufRoot)10 Test (org.junit.Test)10 HashMap (java.util.HashMap)4 Map (java.util.Map)4 ByteArrayInput (io.protostuff.ByteArrayInput)2 Input (io.protostuff.Input)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2