public Object from(Json x) { try { return HGUtils.loadClass(graph, x.asString()); } catch (ClassNotFoundException e) { throw new HGException(e); } }
protected T back() { if (HGUtils.eq(key.getData(), initialKey.getData())) return null; try { OperationStatus status = cursor.cursor().getPrev(key, data, LockMode.DEFAULT); if (status == OperationStatus.SUCCESS) return converter.fromByteArray(data.getData(), data.getOffset(), data.getSize()); else return null; } catch (Throwable t) { closeNoException(); throw new HGException(t); } }
@SuppressWarnings("unchecked") public Object from(Json x) { Collection<Object> C; try { C = (Collection<Object>) HGUtils.loadClass(graph, x.at("javaType").asString()).newInstance(); for (Json j : x.at("data").asJsonList()) C.add(value(j)); } catch (Exception e) { throw new RuntimeException(e); } return C; }
public Json to(Object x) { String typeName = shortNameMap.containsX(x.getClass().getName()) ? shortNameMap.getY(x.getClass().getName()) : x.getClass().getName(); Json result = Json.object().set("javaType", typeName); if (x instanceof HGLink) { result.set("_link", array()); for (int i = 0; i < ((HGLink) x).getArity(); i++) result.at("_link").add(((HGLink) x).getTargetAt(i)); } if (x instanceof Collection) { result.set("_collection", array()); for (Object element : (Collection<?>) x) result.at("_collection").add(makeProperty(element)); } if (x instanceof Map) { result.set("_map", array()); for (Map.Entry<?, ?> e : ((Map<?, ?>) x).entrySet()) result .at("_map") .add( Json.object() .set("key", makeProperty(e.getKey())) .set("value", makeProperty(e.getValue()))); } try { if (props != null) // user provided list of properties for (String propname : props) { PropertyDescriptor desc = BonesOfBeans.getPropertyDescriptor(x, propname); Object value = BonesOfBeans.getProperty(x, desc); if (!ignoreNulls || value != null) result.set(propname, makeProperty(value)); } else // all introspected properties for (PropertyDescriptor desc : BonesOfBeans.getAllPropertyDescriptors(x).values()) if (desc.getReadMethod() != null && desc.getWriteMethod() != null) { Object value = BonesOfBeans.getProperty(x, desc); if (!ignoreNulls || value != null) result.set(desc.getName(), makeProperty(value)); } return result; } catch (Throwable ex) { HGUtils.throwRuntimeException(ex); } return null; // unreachable }
@SuppressWarnings("unchecked") public Object from(Json x) { if (!x.isObject()) return x.getValue(); Json typeName = x.at("javaType"); if ("mjson.Json".equals(typeName)) return x.at("value"); if (typeName == null) { // Not a bean, must be simply a map HashMap<String, Object> m = new HashMap<String, Object>(); for (Map.Entry<String, Json> e : x.asJsonMap().entrySet()) m.put(e.getKey(), from(e.getValue())); return m; } String fullName = shortNameMap.getX(typeName.asString()); if (fullName == null) fullName = typeName.asString(); if (fullName.equals(String.class.getName())) return x.at("value").getValue(); else if (fullName.equals(Boolean.class.getName())) return x.at("value").getValue(); try { Class<?> beanClass = HGUtils.loadClass(graph, fullName); if (Number.class.isAssignableFrom(beanClass)) return castNumber(beanClass, x.at("value")); // return beanClass.getConstructor(new // Class[]{String.class}).newInstance(x.at("value").getValue().toString()); else if (Boolean.class.isAssignableFrom(beanClass)) return x.at("value").asBoolean(); Object bean = null; x = x.at("value"); if (HGLink.class.isAssignableFrom(beanClass) && x.has("_link")) { HGHandle[] targets = new HGHandle[x.at("_link").asJsonList().size()]; for (int i = 0; i < targets.length; i++) targets[i] = value(x.at("_link").at(i)); bean = beanClass .getDeclaredConstructor(new Class[] {HGHandle[].class}) .newInstance(new Object[] {targets}); } else bean = beanClass.newInstance(); if (x.has("_collection")) for (Json element : x.at("_collection").asJsonList()) ((Collection<Object>) bean).add(value(element)); if (x.has("_map")) for (Json entry : x.at("_map").asJsonList()) ((Map<Object, Object>) bean).put(value(entry.at("key")), value(entry.at("value"))); for (Map.Entry<String, Json> entry : x.asJsonMap().entrySet()) { PropertyDescriptor descriptor = BonesOfBeans.getPropertyDescriptor(bean, entry.getKey()); if (descriptor == null) continue; Class<?> propertyClass = descriptor.getPropertyType(); Object value = null; if (descriptor instanceof IndexedPropertyDescriptor) { int length = entry.getValue().asJsonList().size(); Object A = Array.newInstance( ((IndexedPropertyDescriptor) descriptor).getIndexedPropertyType(), length); for (int i = 0; i < length; i++) Array.set(A, i, from(entry.getValue().at(i))); } else if (propertyClass.equals(Class.class)) { if (entry.getValue().isString()) value = HGUtils.loadClass(graph, entry.getValue().asString()); else if (!entry.getValue().isNull()) value = HGUtils.loadClass(graph, entry.getValue().at("value").asString()); } else if (entry.getValue().isNumber()) value = castNumber(propertyClass, entry.getValue()); else value = value(entry.getValue()); BonesOfBeans.setProperty(bean, entry.getKey(), value); } return bean; } catch (Exception ex) { throw new RuntimeException("Failed to JSON-deserialize bean of type " + fullName, ex); } }