Convert a POJO into a serialized JsonValue object

@param o the POJO to serialize
@return
769946
public static JsonValue serializeAsJson(Object o) throws IOException {
        try {
            return findFieldsToSerialize(o).mainObject;
        } catch (IllegalStateException ise) {
            // the reflective attempt to build the object failed.
            throw new IOException("Unable to build JSON for Object", ise);
        } catch (JsonException e) {
            throw new IOException("Unable to build JSON for Object", e);
        }
    }
970803
public JSONObject toJson() throws IOException, JSONException {
    JSONObject json = new JSONObject();

    for (CapabilityInstance option : options.values()) {
      Object value = option.getValue();

      if (value == null) {
        continue;
      }

      if (value instanceof OperaProfile) {
        value = ((OperaProfile) value).toJson();
      } else if (value instanceof OperaArguments && ((OperaArguments) value).size() > 0) {
        value = ((OperaArguments) value).toJson();
      } else if (value instanceof Boolean) {
        value = (Boolean) value ? 1 : 0;
      } else {
        value = String.valueOf(value);
      }

      json.put(option.getIdentifier(), value);
    }

    return json;
  }
1170034
public static byte[] convertObjectToByteArray(Object o) throws IOException{
		ByteArrayOutputStream bos = new ByteArrayOutputStream();
		ObjectOutputStream oos = new ObjectOutputStream(bos);
		oos.writeObject(o);
		
		return bos.toByteArray();
	}
1042068
public static String renderJson(Object o) {

        Gson gson = new GsonBuilder().disableHtmlEscaping().setPrettyPrinting()
                .create();
        return gson.toJson(o);
    }
171236
public static <T> Object jsonToObject(String json, Class<T> clas) {
		ObjectMapper mapper = new ObjectMapper();
		try {
			return mapper.readValue(json, clas);
		} catch (IOException e) {
			JK.throww(e);
			return null;
		}
	}
196880
String _serialize(Object o) {
        String body = null;
            try {
                body = new String(serialize(o));
            } catch (IOException e) {
                throw new ServiceException(ErrorCode.HTTP_CLIENT_ERROR,
                        ErrorCode.HTTP_CLIENT_ERROR.getMessageTemplate(),
                        "serialize failed.");
            }
        return body;
    }
987600
public static String serialize(final Object obj) throws IOException {
		final ObjectMapper mapper = new ObjectMapper();
        mapper.disable(MapperFeature.USE_GETTERS_AS_SETTERS);
		return mapper.writeValueAsString(obj);
		
	}
902228
public final void sendObjectToSocket(Object o) {
		Session sess = this.getSession();
		if (sess != null) {
			String json;
			try {
				json = this.mapper.writeValueAsString(o);
			} catch (JsonProcessingException e) {
				ClientSocketAdapter.LOGGER.error("Failed to serialize object", e);
				return;
			}
			sess.getRemote().sendString(json, new WriteCallback() {
				
				@Override
				public void writeSuccess() {
					ClientSocketAdapter.LOGGER.info("Send data to socket");
				}
				
				@Override
				public void writeFailed(Throwable x) {
					ClientSocketAdapter.LOGGER.error("Error sending message to socket", x);
				}
			});
		}
	}
1043968
public static byte[] getBytes(Object o)
    {
        if (o != null)
        {
            return getBytes(o, o.getClass());
        }

        return null;
    }
1066605
protected JsonWriter writeObject(Object object) throws IOException {
      if (object == null) {
         nullValue();
      } else if (object instanceof JsonSerializable) {
         JsonSerializable structuredSerializable = Cast.as(object);
         if (object instanceof JsonArraySerializable) {
            beginArray();
         } else {
            beginObject();
         }
         structuredSerializable.toJson(this);
         if (object instanceof JsonArraySerializable) {
            endArray();
         } else {
            endObject();
         }
      } else if (object instanceof Number) {
         value(Cast.<Number>as(object));
      } else if (object instanceof String) {
         value(Cast.<String>as(object));
      } else if (object instanceof Boolean) {
         value(Cast.<Boolean>as(object).toString());
      } else if (object instanceof Enum) {
         value(Cast.<Enum>as(object).name());
      } else if (object instanceof EnumValue) {
         value(Cast.<EnumValue>as(object).name());
      } else if (object instanceof Map) {
         value(Cast.<Map<String, ?>>as(object));
      } else if (object.getClass().isArray()) {
         value(Cast.<Object[]>as(object));
      } else if (object instanceof Multimap) {
         value(Cast.<Multimap<String, ?>>as(object).asMap());
      } else if (object instanceof Counter) {
         value(Cast.<Counter<String>>as(object).asMap());
      } else if (object instanceof Iterable) {
         value(Cast.<Iterable>as(object));
      } else if (object instanceof Iterator) {
         value(Cast.<Iterator>as(object));
      } else {
         value(Convert.convert(object, String.class));
      }
      return this;
   }
136712
public static Val of(Object o) {
      if (o != null && o instanceof Val) {
         return Cast.as(o);
      }
      return new Val(o);
   }
424097
protected byte[] serialize(Object o) {
    if (o == null) {
      throw new NullPointerException("Can't serialize null");
    }
    byte[] rv=null;
    ByteArrayOutputStream bos = null;
    ObjectOutputStream os = null;
    try {
      bos = new ByteArrayOutputStream();
      os = new ObjectOutputStream(bos);
      os.writeObject(o);
      os.close();
      bos.close();
      rv = bos.toByteArray();
    } catch (IOException e) {
      throw new IllegalArgumentException("Non-serializable object", e);
    } finally {
      CloseUtil.close(os);
      CloseUtil.close(bos);
    }
    return rv;
  }
691090
@JsonValue
	public Object toJsonValue() {
		if (s != null)
			return s;
		if (n != null)
			return n;
		return null;
	}
268969
@JsonValue
	public Object toJsonValue() {
		if (definition != null)
			return definition;
		if (reference != null)
			return reference;
		if (_null != null)
			return _null;
		return null;
	}
1222177
public String toJson() throws IOException {
        ObjectMapper mapper = new ObjectMapper();
        ByteArrayOutputStream out = new ByteArrayOutputStream();

        mapper.writeValue(out, this);
        return out.toString();
    }
579945
public static byte[] serialize(final Object o, final boolean zip) throws IOException {
        final ByteArrayOutputStream baos = new ByteArrayOutputStream();
        OutputStream os = baos;
        ObjectOutputStream oos = null;
        try {
            if (zip) {
                os = new GZIPOutputStream(os);
            }
            oos = new ObjectOutputStream(os);
            oos.writeObject(o);
            oos.flush();
        } finally {
            if (oos != null) {
                oos.close();
            }
            os.close();
        }

        return baos.toByteArray();
    }
423087
public static String toJson(Object value) throws JsonProcessingException {
		if (value == null)
			return null;
		return _mapper.writeValueAsString(value);
	}