本文整理汇总了Java中net.sf.json.JSONSerializer.toJSON方法的典型用法代码示例。如果您正苦于以下问题:Java JSONSerializer.toJSON方法的具体用法?Java JSONSerializer.toJSON怎么用?Java JSONSerializer.toJSON使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类net.sf.json.JSONSerializer
的用法示例。
在下文中一共展示了JSONSerializer.toJSON方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: extractErrorMessage
import net.sf.json.JSONSerializer; //导入方法依赖的package包/类
@Override
protected String extractErrorMessage(String response) {
if (response != null && !response.isEmpty()) {
try {
JSON jsonResponse = JSONSerializer.toJSON(response, new JsonConfig());
if (jsonResponse instanceof JSONObject) {
JSONObject object = (JSONObject) jsonResponse;
JSONObject errorObj = object.getJSONObject("error");
if (errorObj.containsKey("message")) {
return errorObj.getString("message");
}
}
} catch (JSONException ex) {
log.debug("Cannot parse JSON error response: " + response);
}
}
return response;
}
示例2: jsonToLimitViolation
import net.sf.json.JSONSerializer; //导入方法依赖的package包/类
public static LimitViolation jsonToLimitViolation(String json, Network network) {
JSONObject jsonObj = (JSONObject) JSONSerializer.toJSON(json);
Map<String, String> limitViolation = (Map<String, String>) JSONObject.toBean(jsonObj, Map.class);
Country country = null;
if (limitViolation.containsKey("Country")) {
country = Country.valueOf(limitViolation.get("Country"));
}
float baseVoltage = Float.NaN;
if (limitViolation.containsKey("BaseVoltage")) {
baseVoltage = Float.parseFloat(limitViolation.get("BaseVoltage"));
}
float limitReduction = 1f;
if (limitViolation.containsKey("LimitReduction")) {
limitReduction = Float.parseFloat(limitViolation.get("LimitReduction"));
}
return new LimitViolation(limitViolation.get("Subject"),
LimitViolationType.valueOf(limitViolation.get("LimitType")),
Float.parseFloat(limitViolation.get("Limit")),
limitViolation.get("LimitName"),
limitReduction,
Float.parseFloat(limitViolation.get("Value")),
country,
baseVoltage);
}
示例3: testMarshalAndUnmarshal
import net.sf.json.JSONSerializer; //导入方法依赖的package包/类
@Test
public void testMarshalAndUnmarshal() throws Exception {
InputStream inStream = getClass().getResourceAsStream("testMessage1.xml");
String in = context.getTypeConverter().convertTo(String.class, inStream);
MockEndpoint mockJSON = getMockEndpoint("mock:json");
mockJSON.expectedMessageCount(1);
mockJSON.message(0).body().isInstanceOf(byte[].class);
MockEndpoint mockXML = getMockEndpoint("mock:xml");
mockXML.expectedMessageCount(1);
mockXML.message(0).body().isInstanceOf(String.class);
Object json = template.requestBody("direct:marshal", in);
String jsonString = context.getTypeConverter().convertTo(String.class, json);
JSONObject obj = (JSONObject) JSONSerializer.toJSON(jsonString);
assertEquals("JSONObject doesn't contain 7 keys", 7, obj.entrySet().size());
template.sendBody("direct:unmarshal", jsonString);
mockJSON.assertIsSatisfied();
mockXML.assertIsSatisfied();
}
示例4: testMarshalAndUnmarshalInline
import net.sf.json.JSONSerializer; //导入方法依赖的package包/类
@Test
public void testMarshalAndUnmarshalInline() throws Exception {
InputStream inStream = getClass().getResourceAsStream("testMessage1.xml");
String in = context.getTypeConverter().convertTo(String.class, inStream);
MockEndpoint mockJSON = getMockEndpoint("mock:jsonInline");
mockJSON.expectedMessageCount(1);
mockJSON.message(0).body().isInstanceOf(byte[].class);
MockEndpoint mockXML = getMockEndpoint("mock:xmlInline");
mockXML.expectedMessageCount(1);
mockXML.message(0).body().isInstanceOf(String.class);
Object json = template.requestBody("direct:marshalInline", in);
String jsonString = context.getTypeConverter().convertTo(String.class, json);
JSONObject obj = (JSONObject) JSONSerializer.toJSON(jsonString);
assertEquals("JSONObject doesn't contain 7 keys", 7, obj.entrySet().size());
template.sendBody("direct:unmarshalInline", jsonString);
mockJSON.assertIsSatisfied();
mockXML.assertIsSatisfied();
}
示例5: testUnmarshalJSONObject
import net.sf.json.JSONSerializer; //导入方法依赖的package包/类
@Test
public void testUnmarshalJSONObject() throws Exception {
InputStream inStream = getClass().getResourceAsStream("testMessage1.json");
String in = context.getTypeConverter().convertTo(String.class, inStream);
JSON json = JSONSerializer.toJSON(in);
MockEndpoint mockXML = getMockEndpoint("mock:xml");
mockXML.expectedMessageCount(1);
mockXML.message(0).body().isInstanceOf(String.class);
Object marshalled = template.requestBody("direct:unmarshal", json);
Document document = context.getTypeConverter().convertTo(Document.class, marshalled);
assertEquals("The XML document has an unexpected root node", "o", document.getDocumentElement().getLocalName());
mockXML.assertIsSatisfied();
}
示例6: testSomeOptionsToJSON
import net.sf.json.JSONSerializer; //导入方法依赖的package包/类
@Test
public void testSomeOptionsToJSON() throws Exception {
InputStream inStream = getClass().getClassLoader().getResourceAsStream("org/apache/camel/dataformat/xmljson/testMessage1.xml");
String in = context.getTypeConverter().convertTo(String.class, inStream);
MockEndpoint mockJSON = getMockEndpoint("mock:json");
mockJSON.expectedMessageCount(1);
mockJSON.message(0).body().isInstanceOf(byte[].class);
Object json = template.requestBody("direct:marshal", in);
String jsonString = context.getTypeConverter().convertTo(String.class, json);
JSONObject obj = (JSONObject) JSONSerializer.toJSON(jsonString);
assertEquals("JSON must contain 1 top-level element", 1, obj.entrySet().size());
assertTrue("Top-level element must be named root", obj.has("root"));
mockJSON.assertIsSatisfied();
}
示例7: testXmlWithTypeAttributesToJSON
import net.sf.json.JSONSerializer; //导入方法依赖的package包/类
@Test
public void testXmlWithTypeAttributesToJSON() throws Exception {
InputStream inStream = getClass().getClassLoader().getResourceAsStream("org/apache/camel/dataformat/xmljson/testMessage4.xml");
String in = context.getTypeConverter().convertTo(String.class, inStream);
MockEndpoint mockJSON = getMockEndpoint("mock:json");
mockJSON.expectedMessageCount(1);
mockJSON.message(0).body().isInstanceOf(byte[].class);
Object json = template.requestBody("direct:marshal", in);
String jsonString = context.getTypeConverter().convertTo(String.class, json);
JSONObject obj = (JSONObject) JSONSerializer.toJSON(jsonString);
assertEquals("JSON must contain 1 top-level element", 1, obj.entrySet().size());
assertTrue("Top-level element must be named root", obj.has("root"));
mockJSON.assertIsSatisfied();
}
示例8: setRequestData
import net.sf.json.JSONSerializer; //导入方法依赖的package包/类
@Override
public void setRequestData(String data) {
requestParameterJson = (JSONObject) JSONSerializer.toJSON(data);
String downloadType = requestParameterJson.optString("type");
// Set the content type for the response from the properties file
this.contentType = (propertyMap.get(downloadType));
if (downloadType == null || downloadType.isEmpty()) {
//Default type being csv format
this.fileExtension = ".csv";
downloadType = "csv";
} else {
this.fileExtension = "." + downloadType;
}
String beanName = settingsDownloadMap.get(downloadType);
iDownload = (IDownload) ApplicationContextAccessor.getBean(beanName);
CacheManager = this.getCacheManager();
CacheManager.setRequestData(data);
}
示例9: isSourceArrayValid
import net.sf.json.JSONSerializer; //导入方法依赖的package包/类
/**
* Validates the request parameter sourceArray
*
* @param sourceArray The request parameter sourceArray
* @return true if validated
*/
private boolean isSourceArrayValid(String sourceArray) {
JSONArray sourceJSON;
try {
sourceJSON = (JSONArray) JSONSerializer.toJSON(sourceArray);
} catch (JSONException ex) {
logger.error("JSONException : " + ex);
return false;
}
if (!prepareLists(sourceJSON)) {
throw new OperationFailedException("The requested resource(s) do not exist in the " + "repository. " +
"Aborting operation.");
}
extensionsList = getListOfExtensionsFromSettings();
return (extensionsList != null);
}
示例10: getIpAddress
import net.sf.json.JSONSerializer; //导入方法依赖的package包/类
public String getIpAddress(String containerName) throws IOException, InterruptedException, DockerClientException {
ByteArrayOutputStream stream = new ByteArrayOutputStream();
ByteArrayOutputStream errStream = new ByteArrayOutputStream();
Launcher.ProcStarter ps = launcher.new ProcStarter();
ps.cmdAsSingleString(String.format(DOCKER_INSPECT, containerName));
ps.stdout(stream).stderr(errStream);
ps = ps.pwd(build.getWorkspace()).envs(build.getEnvironment(listener));
Proc p = launcher.launch(ps);
if (p.join() != 0) {
throw new DockerClientException(getErrorMessage(errStream));
}
JSONArray info = (JSONArray) JSONSerializer.toJSON(stream.toString());
return (String) ((JSONObject) info.getJSONObject(0).get(NETWORK_SETTINGS_FIELD)).get(IP_ADDRESS_FIELD);
}
示例11: requestJSON
import net.sf.json.JSONSerializer; //导入方法依赖的package包/类
/**
* Return the JSON data object from the URL.
*/
public Vertex requestJSON(String url, String attribute, Map<String, String> headers, Network network) {
log("GET JSON", Level.INFO, url, attribute);
try {
String json = Utils.httpGET(url, headers);
log("JSON", Level.FINE, json);
JSON root = (JSON)JSONSerializer.toJSON(json.trim());
if (root == null) {
return null;
}
Object value = root;
if (attribute != null) {
value = ((JSONObject)root).get(attribute);
if (value == null) {
return null;
}
}
Vertex object = convertElement(value, network);
return object;
} catch (Exception exception) {
log(exception);
return null;
}
}
示例12: requestJSONAuth
import net.sf.json.JSONSerializer; //导入方法依赖的package包/类
/**
* GET the JSON data from the URL.
*/
public Vertex requestJSONAuth(String url, String user, String password, Network network) {
log("GET JSON Auth", Level.INFO, url);
try {
String json = Utils.httpAuthGET(url, user, password);
log("JSON", Level.FINE, json);
JSONObject root = (JSONObject)JSONSerializer.toJSON(json.trim());
if (root == null) {
return null;
}
Vertex object = convertElement(root, network);
return object;
} catch (Exception exception) {
log(exception);
return null;
}
}
示例13: postJSONAuth
import net.sf.json.JSONSerializer; //导入方法依赖的package包/类
/**
* Post the JSON object and return the JSON data from the URL.
*/
public Vertex postJSONAuth(String url, String user, String password, Vertex jsonObject, Network network) {
log("POST JSON Auth", Level.INFO, url);
try {
String data = convertToJSON(jsonObject);
log("POST JSON", Level.FINE, data);
String json = Utils.httpAuthPOST(url, user, password, "application/json", data);
log("JSON", Level.FINE, json);
JSONObject root = (JSONObject)JSONSerializer.toJSON(json.trim());
if (root == null) {
return null;
}
Vertex object = convertElement(root, network);
return object;
} catch (Exception exception) {
log(exception);
return null;
}
}
示例14: parseAndEncode
import net.sf.json.JSONSerializer; //导入方法依赖的package包/类
private void parseAndEncode(String name) throws IOException {
String msg = IOUtils.toString(XmlParser.class.getResourceAsStream(name));
ourLog.info(msg);
IParser p = ourCtx.newJsonParser();
Profile res = p.parseResource(Profile.class, msg);
String encoded = ourCtx.newJsonParser().setPrettyPrint(true).encodeResourceToString(res);
ourLog.info(encoded);
JSON expected = JSONSerializer.toJSON(msg.trim());
JSON actual = JSONSerializer.toJSON(encoded.trim());
String exp = expected.toString().replace("\\r\\n", "\\n"); // .replace("§", "§");
String act = actual.toString().replace("\\r\\n", "\\n");
ourLog.info("Expected: {}", exp);
ourLog.info("Actual : {}", act);
assertEquals(exp, act);
}
示例15: getChannelsByProjectId
import net.sf.json.JSONSerializer; //导入方法依赖的package包/类
/**
* Uses the authenticated web client to pull all channels for a given project
* from the api and convert them to POJOs
* @param projectId the project to get channels for
* @return a Set of Channels (should have at minimum one entry)
* @throws IllegalArgumentException when the web client receives a bad parameter
* @throws IOException When the AuthenticatedWebClient receives and error response code
*/
public Set<Channel> getChannelsByProjectId(String projectId) throws IllegalArgumentException, IOException {
HashSet<Channel> channels = new HashSet<Channel>();
AuthenticatedWebClient.WebResponse response = webClient.get("api/projects/" + projectId + "/channels");
if (response.isErrorCode()) {
throw new IOException(String.format("Code %s - %n%s", response.getCode(), response.getContent()));
}
JSONObject json = (JSONObject)JSONSerializer.toJSON(response.getContent());
for (Object obj : json.getJSONArray("Items")) {
JSONObject jsonObj = (JSONObject)obj;
String id = jsonObj.getString("Id");
String name = jsonObj.getString("Name");
String description = jsonObj.getString("Description");
boolean isDefault = jsonObj.getBoolean("IsDefault");
channels.add(new Channel(id, name, description, projectId, isDefault));
}
return channels;
}