转换为JSON字符串

@param obj 被转为JSON的对象
@return JSON字符串
198343
public static String toJsonStr(Object obj) {
		if (null == obj) {
			return null;
		}
		if (obj instanceof String) {
			return (String) obj;
		}
		return toJsonStr(parse(obj));
	}
990369
public static String convertSerializableToString(Serializable obj) {
        ByteArrayOutputStream baos = null;
        ObjectOutputStream oos = null;
        try {
            baos = new ByteArrayOutputStream();
            oos = new ObjectOutputStream(baos);
            oos.writeObject(obj);

            return new String(baos.toByteArray());
        } catch (IOException e) {
            return null;
        } finally {
            closeQuietly(baos, oos);
            closeQuietly(oos);
        }
    }
367577
public static String toJSONString(Object o) {
    try {
      return objectMapper.writer().without(SerializationFeature.INDENT_OUTPUT).writeValueAsString(o);
    } catch (JsonProcessingException e) {
      throw new RuntimeException(e);
    }
  }
1345203
@javax.annotation.Nonnull
  public JsonObject getJson() {
    @javax.annotation.Nonnull final JsonObject json = new JsonObject();
    json.addProperty("min", min);
    json.addProperty("max", max);
    json.addProperty("negatives", negatives);
    json.addProperty("positives", positives);
    json.addProperty("zeros", zeros);
    json.addProperty("sum0", sum0);
    json.addProperty("sum1", sum1);
    json.addProperty("sum2", sum2);
    json.addProperty("sumLog", sumLog);
    return json;
  }
1166038
@Override
    public byte[] serialize(Object obj) throws IOException {
		KryoHolder kryoHolder = null;
		if (obj == null)
			throw new RuntimeException("obj can not be null");
		try {
			kryoHolder = KryoPoolImpl.getInstance().get();
			kryoHolder.output.clear(); // clear Output -->每次调用的时候 重置
			kryoHolder.kryo.writeClassAndObject(kryoHolder.output, obj);
			return kryoHolder.output.toBytes();// 无法避免拷贝 ~~~
		} catch (RuntimeException e) {
			throw new RuntimeException(e);
		} finally {
			if (kryoHolder != null) {
				KryoPoolImpl.getInstance().offer(kryoHolder);
			}
			// obj = null; //GC
		}
    }
358773
private static String toJson(Object x) {
    try {
      if (x == null || x instanceof String || x instanceof Number || x instanceof Character
          || x.getClass().isArray() || x instanceof Iterable<?>) {
        return new JSONObject().put("JSON", x).toString(2);
      } else if (x instanceof Map<?, ?>) {
        return (new JSONObject((Map<?, ?>) x)).toString(2);
      } else if (x instanceof JSONObject) {
        return ((JSONObject) x).toString(2);
      } else {
        return (new JSONObject(x)).toString(2);
      }
    } catch (Exception e) {
      throw new RuntimeException("x=" + x, e);
    }
  }
6564
public static String toJSONString(final Object obj) {
        try {
            return mapper.writeValueAsString(obj);
        } catch (final JsonProcessingException e) {
            logger.error("Cannot to JSON string", e);
        }
        return null;
    }
1243234
private static Object toJson(Object obj, JsonBuilderFactory factory) {
		if (obj instanceof JsonObject) {
			return ((JsonObject)obj).toJson(factory.createJsonBuilder());
		} else if (obj.getClass().isArray()) {
			List<Object> r = new ArrayList<>();
			int len = Array.getLength(obj);
			for (int i = 0; i < len; ++i) {
				Object ao = Array.get(obj, i);
				r.add(toJson(ao, factory));
			}
			return r;
		} else if (obj instanceof Collection) {
			Collection<?> coll = (Collection<?>)obj;
			List<Object> r = new ArrayList<>();
			for (Object ao : coll) {
				r.add(toJson(ao, factory));
			}
			return r;
		} else if (obj instanceof Map) {
			Map<?, ?> m = (Map<?, ?>)obj;
			Map<String, Object> r = new LinkedHashMap<>();
			for (Map.Entry<?, ?> e : m.entrySet()) {
				String key = toJson(e.getKey(), factory).toString();
				Object value = toJson(e.getValue(), factory);
				r.put(key, value);
			}
			return r;
		}
		return obj;
	}
989624
public void finishJson(final Object obj) {
        this.contentType = this.jsonContentType;
        if (this.recycleListener != null) this.output = obj;
        finish(request.getJsonConvert().convertTo(getBodyBufferSupplier(), obj));
    }
1084836
private static Object toJson(JsonObject obj, JsonBuilderFactory factory) {
		return obj.toJson(factory.createJsonBuilder());
	}
723602
public static void json(Object obj, final String[] properties,
                            Writer writer, boolean writeClass,
                            Converter<Object, Map<String, Object>> mc) throws IOException {
        if (obj == null)
            writer.write(NULL);
        else
            json(obj, properties, new JSONWriter(writer), writeClass, mc);
    }
611258
public static byte[] toByteArray(Serializable obj) {
        byte[] bytes = null;
        ObjectOutputStream oos = null;
        ByteArrayOutputStream baos = null;
        try {
            baos = new ByteArrayOutputStream();
            oos = new ObjectOutputStream(baos);
            oos.writeObject(obj);
            oos.flush();
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            if (oos != null) {
                try {
                    oos.close();
                    bytes = baos.toByteArray();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }
        return bytes;
    }
1264606
Object asJSON() {
        final List<Object> json = new ArrayList<>();
        for (Join join : joins) { json.add(join.asJSON()); }
        return json;
    }
408833
public static JsonNode toJson(Object obj, ClassLoader classLoader) {
        return SerializationUtils.toJson(obj, classLoader);
    }
600820
public static String toJson(Object obj) {
		ObjectMapper mapper = new ObjectMapper();
		try {
			return mapper.writeValueAsString(obj);
		} catch (IOException e) {
			JK.throww(e);
			return null;
		}
	}
1096020
public String toJson(Object object) {

		try {
			return mapper.writeValueAsString(object);
		} catch (IOException e) {
			logger.warn("write to json string error:" + object, e);
			return null;
		}
	}
1071924
public JSONObject toJson() throws JSONException {
    JSONObject json = new JSONObject();

    for (OperaArgument argument : this) {
      json.put(argument.getArgument(), argument.getValue());
    }

    return json;
  }