本文整理汇总了Java中org.boon.json.JsonFactory.fromJson方法的典型用法代码示例。如果您正苦于以下问题:Java JsonFactory.fromJson方法的具体用法?Java JsonFactory.fromJson怎么用?Java JsonFactory.fromJson使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.boon.json.JsonFactory
的用法示例。
在下文中一共展示了JsonFactory.fromJson方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: outgoing
import org.boon.json.JsonFactory; //导入方法依赖的package包/类
@Test
public void outgoing() throws Exception {
Map<String, String> map = new HashMap<>();
map.put("token", TOKEN);
map.put("trigger_word", "@startuml");
String content = "hogehoge";
map.put("text", "@startuml\n" + content + "\[email protected]");
Request request = new MockUp<Request>() {
@Mock
Optional<String> form(String key) {
return Optional.ofNullable(map.get(key));
}
}.getMockInstance();
Response response = new MockUp<Response>() {
}.getMockInstance();
Object obj = this.target.outgoing(request, response);
assertNotNull(obj);
System.out.println(obj);
Msg msg = JsonFactory.fromJson(obj.toString(), Msg.class);
assertNotNull(msg.text);
assertTrue(msg.text.startsWith(HOST));
Transcoder t = Uml.transcoder();
String enc = t.encode(content);
assertTrue(msg.text.endsWith(enc));
}
示例2: schemaLessTest
import org.boon.json.JsonFactory; //导入方法依赖的package包/类
@Test
public void schemaLessTest() throws Exception
{
String name1 = "{\"name\":\"Travel Route1\", \"cities\": [ \"C1\", \"C2\", \"C3\" ], " +
"\"type\": \"route\" }";
String name2 = "{\"name\":\"First Town\", \"type\": \"city\"}";
String name3 = "{\"name\":\"Second Stop\", \"type\": \"city\"}";
String name4 = "{\"name\":\"Destination\", \"type\": \"city\"}";
try (PreparedStatement preparedStatement = con.prepareStatement("insert into default(key,value) values (?,?)"))
{
Map <String, String> jsonObject = (Map <String, String>)JsonFactory.fromJson(new StringReader(name1));
preparedStatement.setString(1,"name");
preparedStatement.setString(2, jsonObject.get("name"));
assertEquals(1, preparedStatement.executeUpdate());
jsonObject = (Map <String, String>)JsonFactory.fromJson(new StringReader(name2));
preparedStatement.setString(1,"name1");
preparedStatement.setString(2, jsonObject.get("name"));
assertEquals(1, preparedStatement.executeUpdate());
jsonObject = (Map <String, String>)JsonFactory.fromJson(new StringReader(name3));
preparedStatement.setString(1,"name2");
preparedStatement.setString(2, jsonObject.get("name"));
assertEquals(1, preparedStatement.executeUpdate());
jsonObject = (Map <String, String>)JsonFactory.fromJson(new StringReader(name4));
preparedStatement.setString(1,"name3");
preparedStatement.setString(2, jsonObject.get("name"));
assertEquals(1, preparedStatement.executeUpdate());
}
/*
try(Statement statement = con.createStatement())
{
ResultSet rs = statement.executeQuery("SELECT r.name as route_name, c as route_cities " +
"FROM default r NEST travel c ON KEYS r.cities" +
" WHERE r.name = \"Travel Route1\"");
assertTrue(rs.next());
}
*/
}
示例3: createCommand
import org.boon.json.JsonFactory; //导入方法依赖的package包/类
/** Return a Command object bearing a payload relevant to the given route,
* or die trying and throw an exception. Never return null.
*
* Example command-line test query:
* curl --data "state={\"r\":1.0,\"g\":0.5,\"b\":0.25}" localhost:8080/mixer0/layer0
*/
public Command createCommand(Request request)
throws UnsupportedEncodingException,
RoutingException,
CommandParserException,
SoftenedException, // generic wrapper for a boon JSON parser exception
ClassCastException // sometimes a Boon JSON parser exception in disguise
{
Query query = request.getQuery();
Path path = request.getPath();
String route = path.toString();
Class stateClass = this.commandMap.get(route);
if (stateClass == null) {
throw new RoutingException("No route for path: " + route);
} else {
String rawState = query.get("state");
if (rawState == null) {
throw new CommandParserException("The 'state' request param is required for " + route);
} else {
String json = URLDecoder.decode(rawState, "UTF-8");
if (json == null) {
throw new CommandParserException("Failed to URL-decode a raw JSON string for " + route);
} else {
// This works fine, but the JsonFactory for some reason wants a
// Class<T>, not a plain class. Causes an unchecked conversion warning.
Object state = JsonFactory.fromJson(json, stateClass);
if (state == null) {
throw new CommandParserException(
"Failed to deserialize a JSON command of length "
+ json.length() + " for " + route);
} else if (state.getClass() == stateClass) {
return new Command(route, state);
} else {
throw new CommandParserException(
"Failed to convert a command of length "
+ json.length() + " for " + route + " into a "
+ stateClass.getSimpleName() + ".");
}
}
}
}
}