本文整理汇总了Java中play.libs.Json.fromJson方法的典型用法代码示例。如果您正苦于以下问题:Java Json.fromJson方法的具体用法?Java Json.fromJson怎么用?Java Json.fromJson使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类play.libs.Json
的用法示例。
在下文中一共展示了Json.fromJson方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: patch
import play.libs.Json; //导入方法依赖的package包/类
/**
* @return One alert.
*/
public Result patch(String id) throws Exception {
AlarmStatus alarm = Json.fromJson(request().body().asJson(), AlarmStatus.class);
// validate input
if (!(alarm.status.equalsIgnoreCase("open") ||
alarm.status.equalsIgnoreCase("closed") ||
alarm.status.equalsIgnoreCase("acknowledged"))) {
return badRequest(
"Status must be `open`, acknowledged`, or `closed`. " +
"Value provided: " + alarm.status);
}
return ok(toJson(new AlarmApiModel(this.alarms.update(id, alarm.status))));
}
示例2: post
import play.libs.Json; //导入方法依赖的package包/类
/**
* create a new item
*
* @return result
*/
public Result post(String collectionId) throws Exception {
String key = keyGenerator.generate();
ensureValidId(collectionId, key);
JsonNode jsonBody = request().body().asJson();
ValueServiceModel inputModel = Json.fromJson(jsonBody, ValueServiceModel.class);
ValueServiceModel result = this.container.create(collectionId, key, inputModel);
return ok(toJson(new ValueApiModel(result)));
}
示例3: put
import play.libs.Json; //导入方法依赖的package包/类
/**
* create or update an item
*
* @return result
*/
public Result put(String collectionId, String key) throws Exception {
ensureValidId(collectionId, key);
JsonNode jsonBody = request().body().asJson();
ValueServiceModel inputModel = Json.fromJson(jsonBody, ValueServiceModel.class);
ValueServiceModel result = (inputModel.ETag == null) ?
this.container.create(collectionId, key, inputModel) :
this.container.upsert(collectionId, key, inputModel);
return ok(toJson(new ValueApiModel(result)));
}
示例4: getTest
import play.libs.Json; //导入方法依赖的package包/类
@Test(timeout = 100000)
public void getTest() throws Exception {
String testKey = keyGenerator.generate();
String testValue = keyGenerator.generate();
ValueServiceModel model = new ValueServiceModel(testKey, testValue);
Mockito.when(mockStorage.get(Mockito.any(String.class), Mockito.any(String.class)))
.thenReturn(model);
String resultJson = TestUtils.getString(controller.get(keyGenerator.generate(), keyGenerator.generate()));
ValueServiceModel result = Json.fromJson(Json.parse(resultJson), ValueServiceModel.class);
assertEquals(result.Data, testValue);
}
示例5: createTest
import play.libs.Json; //导入方法依赖的package包/类
@Test(timeout = 100000)
public void createTest() throws Exception {
String testValue = keyGenerator.generate();
String testKey = keyGenerator.generate();
ValueServiceModel model = new ValueServiceModel(testKey, testValue);
Mockito.when(mockStorage.create(Mockito.any(String.class), Mockito.any(String.class), Mockito.any(ValueServiceModel.class)))
.thenReturn(model);
TestUtils.setRequest(String.format("{\"Data\":\"%s\"}", keyGenerator.generate(), keyGenerator.generate()));
String resultJson = TestUtils.getString(controller.put(keyGenerator.generate(), keyGenerator.generate()));
ValueServiceModel result = Json.fromJson(Json.parse(resultJson), ValueServiceModel.class);
assertEquals(result.Data, testValue);
}
示例6: updateTest
import play.libs.Json; //导入方法依赖的package包/类
@Test(timeout = 100000)
public void updateTest() throws Exception {
String testKey = keyGenerator.generate();
String testValue = keyGenerator.generate();
ValueServiceModel model = new ValueServiceModel(testKey, testValue);
Mockito.when(mockStorage.upsert(Mockito.any(String.class), Mockito.any(String.class), Mockito.any(ValueServiceModel.class)))
.thenReturn(model);
TestUtils.setRequest(String.format("{\"ETag\":\"%s\",\"Data\":\"%s\"}", keyGenerator.generate(), keyGenerator.generate()));
String resultJson = TestUtils.getString(controller.put(keyGenerator.generate(), keyGenerator.generate()));
ValueServiceModel result = Json.fromJson(Json.parse(resultJson), ValueServiceModel.class);
assertEquals(result.Data, testValue);
}
示例7: mapRequest
import play.libs.Json; //导入方法依赖的package包/类
/**
* Method to map request
*
* @param requestData JsonNode
* @param obj Class<T>
* @exception RuntimeException
* @return <T>
*/
public static <T> Object mapRequest(JsonNode requestData, Class<T> obj) throws RuntimeException {
try {
return Json.fromJson(requestData, obj);
} catch (Exception e) {
ProjectLogger.log("ControllerRequestMapper error : " + e.getMessage(), e);
ProjectLogger.log("Request Data" + requestData.toString(),
LoggerEnum.INFO.name());
throw new ProjectCommonException(ResponseCode.invalidData.getErrorCode(),
ResponseCode.invalidData.getErrorMessage(),
ResponseCode.CLIENT_ERROR.getResponseCode());
}
}