本文整理汇总了Java中org.json.JSONStringer类的典型用法代码示例。如果您正苦于以下问题:Java JSONStringer类的具体用法?Java JSONStringer怎么用?Java JSONStringer使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
JSONStringer类属于org.json包,在下文中一共展示了JSONStringer类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: toJSONStringImpl
import org.json.JSONStringer; //导入依赖的package包/类
/**
* Implementation of the toJSONString method for an AbstractVertex
*/
public void toJSONStringImpl(JSONStringer stringer) throws JSONException {
super.toJSONStringImpl(stringer);
Set<Members> members_set = CollectionUtil.getAllExcluding(Members.values(), Members.PROBABILITIES);
Members members[] = new Members[members_set.size()];
members_set.toArray(members);
super.fieldsToJSONString(stringer, MarkovVertex.class, members);
// Probabilities Map
stringer.key(Members.PROBABILITIES.name()).object();
for (Probability type : Probability.values()) {
stringer.key(type.name()).array();
int i = type.ordinal();
for (int j = 0, cnt = this.probabilities[i].length; j < cnt; j++) {
stringer.value(this.probabilities[i][j]);
} // FOR
stringer.endArray();
} // FOR
stringer.endObject();
}
示例2: toJSONString
import org.json.JSONStringer; //导入依赖的package包/类
@Override
public String toJSONString() {
JSONStringer stringer = new JSONStringer();
try
{
stringer.object();
toJSONString(stringer);
stringer.endObject();
}
catch (JSONException e)
{
throw new RuntimeException("Failed to serialize " + this, e);
// System.exit(-1);
}
return stringer.toString();
}
示例3: toJSONString
import org.json.JSONStringer; //导入依赖的package包/类
/**
* @param <T>
* @param object
* @return
*/
public static String toJSONString(Object object) {
JSONStringer stringer = new JSONStringer();
try {
if (object instanceof JSONSerializable) {
stringer.object();
((JSONSerializable) object).toJSON(stringer);
stringer.endObject();
} else if (object != null) {
Class<?> clazz = object.getClass();
// stringer.key(clazz.getSimpleName());
JSONUtil.writeFieldValue(stringer, clazz, object);
}
} catch (JSONException e) {
throw new RuntimeException(e);
}
return (stringer.toString());
}
示例4: load
import org.json.JSONStringer; //导入依赖的package包/类
/**
* Load with the ability to override values
*/
public void load(File input_path, Database catalog_db, Map<String, String> override) throws IOException {
// First call the regular load() method to bring all of our options
this.load(input_path, catalog_db);
// Then construct a JSONObject from the map to override the parameters
if (override.isEmpty() == false) {
JSONStringer stringer = new JSONStringer();
try {
stringer.object();
for (Entry<String, String> e : override.entrySet()) {
stringer.key(e.getKey().toUpperCase()).value(e.getValue());
} // FOR
stringer.endObject();
this.fromJSON(new JSONObject(stringer.toString()), catalog_db);
} catch (JSONException ex) {
throw new IOException("Failed to load override parameters: " + override, ex);
}
}
}
示例5: toJSON
import org.json.JSONStringer; //导入依赖的package包/类
public String toJSON(List<User> users) throws JSONException {
try{
JSONStringer jsonStringer = new JSONStringer();
jsonStringer.object().key("lista").array().object().key("user").array();
for (User user:users){
jsonStringer.object();
jsonStringer.key("cpf").value(user.getCpf());
jsonStringer.key("name").value(user.getName());
jsonStringer.key("phone").value(user.getPhone());
jsonStringer.key("email").value(user.getEmail());
jsonStringer.key("company").value(user.getCompany());
jsonStringer.key("birthdate").value(user.getBirthdate());
jsonStringer.key("url").value(user.getUrl());
jsonStringer.key("location").value(user.getLocation());
jsonStringer.key("photo").value(user.getPhoto());
jsonStringer.endObject();
}
jsonStringer.endArray().endObject().endArray().endObject();
return jsonStringer.toString();
} catch (JSONException e){
return null;
}
}
示例6: toJSONString
import org.json.JSONStringer; //导入依赖的package包/类
@Override
public void toJSONString(JSONStringer stringer) throws JSONException {
super.toJSONString(stringer);
stringer.key(Members.KEY_ITERATE.name()).value(m_keyIterate);
stringer.key(Members.LOOKUP_TYPE.name()).value(m_lookupType.toString());
stringer.key(Members.SORT_DIRECTION.name()).value(m_sortDirection.toString());
stringer.key(Members.TARGET_INDEX_NAME.name()).value(m_targetIndexName);
stringer.key(Members.END_EXPRESSION.name());
stringer.value(m_endExpression);
stringer.key(Members.SEARCHKEY_EXPRESSIONS.name()).array();
for (AbstractExpression ae : m_searchkeyExpressions) {
assert (ae instanceof JSONString);
stringer.value(ae);
}
stringer.endArray();
}
示例7: toJSON
import org.json.JSONStringer; //导入依赖的package包/类
@Override
public void toJSON(JSONStringer stringer) throws JSONException {
// Go through once and find the greatest position where
// there are no more non-null values
int maxSize = 0;
for (int i = 0; i < this.histogram.length; i++) {
if (this.histogram[i] != NULL_COUNT) {
maxSize = i;
}
} // FOR
stringer.key(Members.HISTOGRAM.name()).array();
for (int i = 0; i <= maxSize; i++) {
stringer.value(this.histogram[i]);
} // FOR
stringer.endArray();
if (this.debug_names != null && this.debug_names.isEmpty() == false) {
stringer.key(Members.DEBUG.name()).object();
for (Entry<Object, String> e : this.debug_names.entrySet()) {
stringer.key(e.getKey().toString())
.value(e.getValue().toString());
} // FOR
stringer.endObject();
}
}
示例8: toBundle
import org.json.JSONStringer; //导入依赖的package包/类
public void toBundle(Bundle bundle) {
super.toBundle(bundle);
JSONStringer jSONStringer = new JSONStringer();
try {
jSONStringer.object();
jSONStringer.key("card_list");
jSONStringer.array();
for (WXCardItem wXCardItem : this.cardArrary) {
jSONStringer.object();
jSONStringer.key("card_id");
jSONStringer.value(wXCardItem.cardId);
jSONStringer.key("card_ext");
jSONStringer.value(wXCardItem.cardExtMsg == null ? "" : wXCardItem.cardExtMsg);
jSONStringer.endObject();
}
jSONStringer.endArray();
jSONStringer.endObject();
} catch (Exception e) {
e.printStackTrace();
}
bundle.putString("_wxapi_add_card_to_wx_card_list", jSONStringer.toString());
}
示例9: toJSONString
import org.json.JSONStringer; //导入依赖的package包/类
@Override
public String toJSONString() {
JSONStringer stringer = new JSONStringer();
try {
stringer.object();
super.toJSONString(stringer);
stringer.key(Members.EXECUTE_LIST.name()).array();
for (AbstractPlanNode node : m_list) {
stringer.value(node.getPlanNodeId().intValue());
}
stringer.endArray(); //end execution list
stringer.endObject(); //end PlanNodeList
} catch (JSONException e) {
// HACK ugly ugly to make the JSON handling
// in QueryPlanner generate a JSONException for a plan we know
// here that we can't serialize. Making this method throw
// JSONException pushes that exception deep into the bowels of
// Volt with no good place to catch it and handle the error.
// Consider this the coward's way out.
throw new RuntimeException("Failed to serialize PlanNodeList", e);
// return "This JSON error message is a lie";
}
return stringer.toString();
}
示例10: toJSON
import org.json.JSONStringer; //导入依赖的package包/类
@Override
public void toJSON(JSONStringer stringer) throws JSONException {
String exclude[] = {
(this.enableBasePartitions == false ? "basePartitions" : ""),
(this.enableResponseStatuses == false ? "responseStatuses" : ""),
};
// HACK
this.specexecs.setDebugLabels(null);
this.dtxns.setDebugLabels(null);
Field fields[] = JSONUtil.getSerializableFields(this.getClass(), exclude);
JSONUtil.fieldsToJSON(stringer, this, BenchmarkComponentResults.class, fields);
this.specexecs.setDebugLabels(this.transactions.getDebugLabels());
this.dtxns.setDebugLabels(this.transactions.getDebugLabels());
}
示例11: createKey
import org.json.JSONStringer; //导入依赖的package包/类
/**
* Returns a String key representation of the column as "Parent.Child"
*
* @param catalog_col
* @return
*/
public static <T extends CatalogType> String createKey(T catalog_item) {
// There is a 7x speed-up when we use the cache versus always
// constructing a new key
String ret = CACHE_CREATEKEY.get(catalog_item);
if (ret != null)
return (ret);
if (catalog_item == null)
return (null);
JSONStringer stringer = new JSONStringer();
try {
createKey(catalog_item, stringer);
// IMPORTANT: We have to convert all double quotes to single-quotes
// so that
// the keys don't get escaped when stored in other JSON objects
ret = stringer.toString().replace("\"", "'");
CACHE_CREATEKEY.put(catalog_item, ret);
} catch (JSONException ex) {
throw new RuntimeException("Failed to create catalog key for " + catalog_item, ex);
}
return (ret);
}
示例12: toJsonString
import org.json.JSONStringer; //导入依赖的package包/类
public String toJsonString() throws JSONException {
JSONStringer out = new JSONStringer();
out.object();
out.key(JSON_TYPE);
out.value(getTypeName());
if (!TextUtils.isEmpty(mBlockId)) {
out.key(JSON_BLOCK_ID);
out.value(mBlockId);
}
if (!TextUtils.isEmpty(mGroupId)) {
out.key(JSON_GROUP_ID);
out.value(mGroupId);
}
writeJsonAttributes(out);
// Workspace id is not included to reduce size over network.
out.endObject();
return out.toString();
}
示例13: onPostExecute
import org.json.JSONStringer; //导入依赖的package包/类
@Override
protected void onPostExecute(JSONStringer strCardInfo) {
if (strCardInfo != null) {
displayIdInfo(strCardInfo);
}
super.onPostExecute(strCardInfo);
}
示例14: encodeToString
import org.json.JSONStringer; //导入依赖的package包/类
String encodeToString() {
try {
if (activityInfo != null) {
// If it a launcher target, we only need component name, and user to
// recreate this.
return new JSONStringer()
.object()
.key(LAUNCH_INTENT_KEY).value(launchIntent.toUri(0))
.key(APP_SHORTCUT_TYPE_KEY).value(true)
.key(USER_HANDLE_KEY).value(UserManagerCompat.getInstance(mContext)
.getSerialNumberForUser(user))
.endObject().toString();
} else if (shortcutInfo != null) {
// If it a launcher target, we only need component name, and user to
// recreate this.
return new JSONStringer()
.object()
.key(LAUNCH_INTENT_KEY).value(launchIntent.toUri(0))
.key(DEEPSHORTCUT_TYPE_KEY).value(true)
.key(USER_HANDLE_KEY).value(UserManagerCompat.getInstance(mContext)
.getSerialNumberForUser(user))
.endObject().toString();
} else if (providerInfo != null) {
// If it a launcher target, we only need component name, and user to
// recreate this.
return new JSONStringer()
.object()
.key(LAUNCH_INTENT_KEY).value(launchIntent.toUri(0))
.key(APP_WIDGET_TYPE_KEY).value(true)
.key(USER_HANDLE_KEY).value(UserManagerCompat.getInstance(mContext)
.getSerialNumberForUser(user))
.endObject().toString();
}
if (launchIntent.getAction() == null) {
launchIntent.setAction(Intent.ACTION_VIEW);
} else if (launchIntent.getAction().equals(Intent.ACTION_MAIN) &&
launchIntent.getCategories() != null &&
launchIntent.getCategories().contains(Intent.CATEGORY_LAUNCHER)) {
launchIntent.addFlags(
Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
}
// This name is only used for comparisons and notifications, so fall back to activity
// name if not supplied
String name = ensureValidName(mContext, launchIntent, label).toString();
Bitmap icon = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON);
Intent.ShortcutIconResource iconResource =
data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE);
// Only encode the parameters which are supported by the API.
JSONStringer json = new JSONStringer()
.object()
.key(LAUNCH_INTENT_KEY).value(launchIntent.toUri(0))
.key(NAME_KEY).value(name);
if (icon != null) {
byte[] iconByteArray = Utilities.flattenBitmap(icon);
json = json.key(ICON_KEY).value(
Base64.encodeToString(
iconByteArray, 0, iconByteArray.length, Base64.DEFAULT));
}
if (iconResource != null) {
json = json.key(ICON_RESOURCE_NAME_KEY).value(iconResource.resourceName);
json = json.key(ICON_RESOURCE_PACKAGE_NAME_KEY)
.value(iconResource.packageName);
}
return json.endObject().toString();
} catch (JSONException e) {
e.printStackTrace();
return null;
}
}
示例15: toJSON
import org.json.JSONStringer; //导入依赖的package包/类
@Override
public void toJSON(JSONStringer stringer) throws JSONException {
// CLASSNAME
stringer.key(Members.CLASSNAME.name()).value(this.getClass().getCanonicalName());
// MARKOV GRAPHS
stringer.key(Members.MARKOVS.name()).object();
for (Integer id : this.markovs.keySet()) {
// Roll through each id and create a new JSONObject per id
if (debug.val)
LOG.debug("Serializing " + this.markovs.get(id).size() + " graphs for id " + id);
stringer.key(id.toString()).object();
for (Entry<Procedure, MarkovGraph> e : this.markovs.get(id).entrySet()) {
// Optimization: Hex-encode all of the MarkovGraphs so that we don't get crushed
// when trying to read them all back at once when we create the JSONObject
try {
FastSerializer fs = new FastSerializer(false, false); // C++ needs little-endian
fs.write(e.getValue().toJSONString().getBytes());
String hexString = fs.getHexEncodedBytes();
stringer.key(CatalogKey.createKey(e.getKey())).value(hexString);
} catch (Exception ex) {
String msg = String.format("Failed to serialize %s MarkovGraph for Id %d", e.getKey(), id);
LOG.fatal(msg);
throw new JSONException(ex);
}
} // FOR
stringer.endObject();
} // FOR
stringer.endObject();
}