Java Code Examples for org.kurento.jsonrpc.JsonUtils#fromJson()

The following examples show how to use org.kurento.jsonrpc.JsonUtils#fromJson() . 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: RomJsonConverterTest.java    From kurento-java with Apache License 2.0 6 votes vote down vote up
@Test
public void objectToJsonConversion() {

  JsonObject jsonObject = new JsonObject();
  jsonObject.addProperty("prop1", "XXX");
  jsonObject.addProperty("prop2", 33);
  jsonObject.addProperty("prop3", "YYY");
  jsonObject.addProperty("prop4", 5.5f);

  ComplexParam param = JsonUtils.fromJson(jsonObject, ComplexParam.class);

  assertEquals(param.getProp1(), "XXX");
  assertEquals(param.getProp2(), 33);
  assertEquals(param.getProp3(), "YYY");
  assertEquals(param.getProp4(), 5.5f, 0.01);
}
 
Example 2
Source File: RomJsonConverterTest.java    From kurento-java with Apache License 2.0 6 votes vote down vote up
@Test
public void propsToJsonConversion() {

  JsonObject jsonObject = new JsonObject();
  jsonObject.addProperty("prop1", "XXX");
  jsonObject.addProperty("prop2", 33);
  jsonObject.addProperty("prop3", "YYY");
  jsonObject.addProperty("prop4", 5.5f);

  Props props = JsonUtils.fromJson(jsonObject, Props.class);

  assertEquals(props.getProp("prop1"), "XXX");
  assertEquals(props.getProp("prop2"), 33);
  assertEquals(props.getProp("prop3"), "YYY");
  assertEquals(props.getProp("prop4"), 5.5f);
}
 
Example 3
Source File: RomJsonConverterTest.java    From kurento-java with Apache License 2.0 5 votes vote down vote up
@Test
public void stringListConversion() {

  JsonArray array = new JsonArray();
  array.add(new JsonPrimitive("XXX"));
  array.add(new JsonPrimitive("YYY"));
  array.add(new JsonPrimitive("ZZZ"));

  @SuppressWarnings("unchecked")
  List<String> list = JsonUtils.fromJson(array, List.class);

  assertEquals(list.get(0), "XXX");
  assertEquals(list.get(1), "YYY");
  assertEquals(list.get(2), "ZZZ");
}
 
Example 4
Source File: RomJsonConverterTest.java    From kurento-java with Apache License 2.0 5 votes vote down vote up
@Test
public void integerListConversion() {

  JsonArray array = new JsonArray();
  array.add(new JsonPrimitive(1));
  array.add(new JsonPrimitive(2));
  array.add(new JsonPrimitive(3));

  List<Integer> list = JsonUtils.fromJson(array, new TypeToken<List<Integer>>() {
  }.getType());

  assertEquals(list.get(0), (Integer) 1);
  assertEquals(list.get(1), (Integer) 2);
  assertEquals(list.get(2), (Integer) 3);
}
 
Example 5
Source File: RomJsonConverterTest.java    From kurento-java with Apache License 2.0 5 votes vote down vote up
@Test
public void floatListConversion() {

  JsonArray array = new JsonArray();
  array.add(new JsonPrimitive(0.1));
  array.add(new JsonPrimitive(0.2));
  array.add(new JsonPrimitive(0.3));

  List<Float> list = JsonUtils.fromJson(array, new TypeToken<List<Float>>() {
  }.getType());

  assertEquals(list.get(0), 0.1, 0.01);
  assertEquals(list.get(1), 0.2, 0.01);
  assertEquals(list.get(2), 0.3, 0.01);
}
 
Example 6
Source File: RomJsonConverterTest.java    From kurento-java with Apache License 2.0 5 votes vote down vote up
@Test
public void booleanListConversion() {

  JsonArray array = new JsonArray();
  array.add(new JsonPrimitive(true));
  array.add(new JsonPrimitive(false));
  array.add(new JsonPrimitive(true));

  @SuppressWarnings("unchecked")
  List<Boolean> list = JsonUtils.fromJson(array, List.class);

  assertEquals(list.get(0), true);
  assertEquals(list.get(1), false);
  assertEquals(list.get(2), true);
}
 
Example 7
Source File: RomJsonConverterTest.java    From kurento-java with Apache License 2.0 5 votes vote down vote up
@Test
public void stringToEnumListConversion() {

  JsonArray array = new JsonArray();
  array.add(new JsonPrimitive("CONST1"));
  array.add(new JsonPrimitive("CONST2"));
  array.add(new JsonPrimitive("CONST3"));

  List<EnumType> list = JsonUtils.fromJson(array, new TypeToken<List<EnumType>>() {
  }.getType());

  assertEquals(list.get(0), EnumType.CONST1);
  assertEquals(list.get(1), EnumType.CONST2);
  assertEquals(list.get(2), EnumType.CONST3);
}
 
Example 8
Source File: RomJsonConverterTest.java    From kurento-java with Apache License 2.0 5 votes vote down vote up
@Test
public void objectListToJsonConversion() {

  JsonObject jsonObject = new JsonObject();
  jsonObject.addProperty("prop1", "XXX");
  jsonObject.addProperty("prop2", 33);
  jsonObject.addProperty("prop3", "YYY");
  jsonObject.addProperty("prop4", 5.5f);

  JsonArray array = new JsonArray();
  array.add(jsonObject);

  JsonObject jsonObject2 = new JsonObject();
  jsonObject2.addProperty("prop1", "XXX2");
  jsonObject2.addProperty("prop2", 66);
  jsonObject2.addProperty("prop3", "YYY2");
  jsonObject2.addProperty("prop4", 11.5f);

  array.add(jsonObject2);

  List<ComplexParam> params = JsonUtils.fromJson(array, new TypeToken<List<ComplexParam>>() {
  }.getType());

  assertEquals(params.get(0).getProp1(), "XXX");
  assertEquals(params.get(0).getProp2(), 33);
  assertEquals(params.get(0).getProp3(), "YYY");
  assertEquals(params.get(0).getProp4(), 5.5f, 0.01);

  assertEquals(params.get(1).getProp1(), "XXX2");
  assertEquals(params.get(1).getProp2(), 66);
  assertEquals(params.get(1).getProp3(), "YYY2");
  assertEquals(params.get(1).getProp4(), 11.5f, 0.01);
}
 
Example 9
Source File: ProtocolManager.java    From kurento-java with Apache License 2.0 4 votes vote down vote up
private void processRequestMessage(ServerSessionFactory factory, JsonObject requestJsonObject,
    final ResponseSender responseSender, String transportId) throws IOException {

  final Request<JsonElement> request = JsonUtils.fromJsonRequest(requestJsonObject,
      JsonElement.class);

  switch (request.getMethod()) {
  case METHOD_CONNECT:

    log.debug("{} Req-> {} (transportId={})", label, request, transportId);
    processReconnectMessage(factory, request, responseSender, transportId);
    break;
  case METHOD_PING:
    log.trace("{} Req-> {} (transportId={})", label, request, transportId);
    processPingMessage(factory, request, responseSender, transportId);
    break;

  case METHOD_CLOSE:
    log.trace("{} Req-> {} (transportId={})", label, request, transportId);
    processCloseMessage(factory, request, responseSender, transportId);

    break;
  default:

    final ServerSession session = getOrCreateSession(factory, transportId, request);

    log.debug("{} Req-> {} [jsonRpcSessionId={}, transportId={}]", label, request,
        session.getSessionId(), transportId);

    // TODO, Take out this an put in Http specific handler. The main
    // reason is to wait for request before responding to the client.
    // And for no contaminate the ProtocolManager.
    if (request.getMethod().equals(Request.POLL_METHOD_NAME)) {

      Type collectionType = new TypeToken<List<Response<JsonElement>>>() {
      }.getType();

      List<Response<JsonElement>> responseList = JsonUtils.fromJson(request.getParams(),
          collectionType);

      for (Response<JsonElement> response : responseList) {
        session.handleResponse(response);
      }

      // Wait for some time if there is a request from server to
      // client

      // TODO Allow send empty responses. Now you have to send at
      // least an
      // empty string
      responseSender.sendResponse(new Response<Object>(request.getId(), Collections.emptyList()));

    } else {
      session.processRequest(new Runnable() {
        @Override
        public void run() {
          handlerManager.handleRequest(session, request, responseSender);
        }
      });
    }
    break;
  }

}
 
Example 10
Source File: JsonResponseUtils.java    From kurento-java with Apache License 2.0 3 votes vote down vote up
public static <E> E convertFromResult(JsonElement result, Type type) {

    if (type == Void.class || type == void.class) {
      return null;
    }

    JsonElement extractResult = extractValueFromResponse(result, type);

    return JsonUtils.fromJson(extractResult, type);
  }
 
Example 11
Source File: ProtocolManager.java    From kurento-java with Apache License 2.0 3 votes vote down vote up
public void processMessage(String messageJson, ServerSessionFactory factory,
    ResponseSender responseSender, String internalSessionId) throws IOException {

  JsonObject messagetJsonObject = JsonUtils.fromJson(messageJson, JsonObject.class);

  processMessage(messagetJsonObject, factory, responseSender, internalSessionId);
}