public String toString() { return asString(0).toString(); }
private static StringBuilder encodeField(String field, Object value, int indent) { return new StringBuilder() .append(quoteFieldName(field)) .append(VALUE_SEPARATOR) .append(quoteFieldValue(value, indent)); }
private static StringBuilder quoteFieldValue(Object o, int indent) { if (o == null) return new StringBuilder(NULL_LITERAL); if (o instanceof Number) return new StringBuilder(o.toString()); if (o instanceof MyJSON) return ((MyJSON)o).asString(indent); return quoted(o.toString()); }
@Override public StringBuilder asString(int indent) { StringBuilder buf = new StringBuilder().append(OBJECT_START); if (!Strings.isNullOrEmpty(_id)) { buf.append(encodeField(_ref ? REF_MARKER : ID_MARKER, ior(), 0)); if (_values.entrySet().size() > 0) buf.append(FIELD_SEPARATOR).append(NEWLINE); } if (_ref) { return buf.append(OBJECT_END); } StringBuilder tab = newIndent(indent+1); int i = 0; for (Map.Entry<String, Object> e : _values.entrySet()) { buf.append(tab).append(encodeField(e.getKey(), e.getValue(), indent+1)); if (i++ < _values.entrySet().size() - 1) buf.append(FIELD_SEPARATOR).append(NEWLINE); } buf.append(NEWLINE) .append(newIndent(indent)) .append(OBJECT_END); return buf; }
@Override public String processServiceMethodResult(Object result, Class resultType) throws Exception { MyJSONObject root = new MyJSONObject(); if (result instanceof Entity) { Entity entity = (Entity) result; MyJSONObject entityObject = _process(entity); root.set("result", entityObject); } else if (result instanceof Collection) { if (!checkCollectionItemTypes((Collection) result, Entity.class)) throw new IllegalArgumentException("Items that are not instances of Entity found in service method result"); //noinspection unchecked ArrayList list = new ArrayList((Collection) result); MetaClass metaClass; if (!list.isEmpty()) metaClass = ((Entity) list.get(0)).getMetaClass(); else metaClass = AppBeans.get(Metadata.class).getClasses().iterator().next(); MyJSONObject.Array processed = _process(list, metaClass, null); root.set("result", processed); } else { if (result != null && resultType != Void.TYPE) { Datatype datatype = getDatatype(resultType); root.set("result", datatype != null ? datatype.format(result) : result.toString()); } else { root.set("result", null); } } return root.toString(); }
public static void main(String[] args) throws Exception { MyJSONObject o = new MyJSONObject("Person-1234", false); MyJSONObject r = new MyJSONObject("Person-1234", true); MyJSONObject f = new MyJSONObject("Person-2345", false); Array a = new Array(); a.add(f); a.add(3456); a.add(null); a.add(r); a.add(null); KVMap map = new KVMap(); map.put("k1", r); map.put("k2", f); map.put("k3", null); map.put("k4", 3456); map.put(null, 6789); f.set("name", "Mary"); f.set("age", 30); f.set("friend", r); o.set("name", "John"); o.set("age", 20); o.set("friend", f); o.set("friends", a); o.set("map", map); System.err.println(o); } }
public void write(PrintWriter writer) { writer.println(toString()); } public String toString() {
MyJSONObject root = new MyJSONObject(idof(entity), false); root.set("__securityToken", Base64.getEncoder().encodeToString(securityToken)); String[] filteredAttributes = BaseEntityInternalAccess.getFilteredAttributes(baseGenericIdEntity); root.set("__filteredAttributes", array); case DATATYPE: if (value != null) { root.set(property.getName(), property.getRange().asDatatype().format(value)); } else if (!DynamicAttributesUtils.isDynamicAttribute(property.getName())) { root.set(property.getName(), null); if (value != null) { root.set(property.getName(), property.getRange().asEnumeration().format(value)); } else { root.set(property.getName(), null); root.set(property.getName(), null); } else { root.set(property.getName(), encodeInstance((Entity) value, visited, property.getRange().asClass(), propertyView)); root.set(property.getName(), null); break; root.set(property.getName(), array);
@Override public String process(Entity entity, MetaClass metaclass, View view) throws Exception { MyJSONObject jsonObject = encodeInstance(entity, new HashSet<Entity>(), metaclass, view); return jsonObject.toString(); }