BitSet loaded = sm.getLoaded();
StoreContext ctx = (StoreContext)sm.getGenericContext();
List<Attribute<?, ?>> attrs = MetamodelHelper.getAttributesInOrder(sm.getMetaData(), model);
for (int i = 0; i < attrs.size(); i++) {
FieldMetaData fmd = ((Members.Member<?, ?>) attrs.get(i)).fmd;
if (!loaded.get(fmd.getIndex()))
continue;
Object value = sm.fetch(fmd.getIndex());
switch (fmd.getDeclaredTypeCode()) {
case JavaTypes.BOOLEAN:
case JavaTypes.BYTE:
case JavaTypes.CHAR:
case JavaTypes.DOUBLE:
case JavaTypes.FLOAT:
case JavaTypes.INT:
case JavaTypes.LONG:
case JavaTypes.SHORT:
case JavaTypes.BOOLEAN_OBJ:
case JavaTypes.BYTE_OBJ:
case JavaTypes.CHAR_OBJ:
case JavaTypes.DOUBLE_OBJ:
case JavaTypes.FLOAT_OBJ:
case JavaTypes.INT_OBJ:
case JavaTypes.LONG_OBJ:
case JavaTypes.SHORT_OBJ:
case JavaTypes.BIGDECIMAL:
case JavaTypes.BIGINTEGER:
case JavaTypes.DATE:
case JavaTypes.NUMBER:
case JavaTypes.CALENDAR:
case JavaTypes.LOCALE:
case JavaTypes.STRING:
case JavaTypes.ENUM:
root.set(fmd.getName(),value);
break;
case JavaTypes.PC:
if (value == null) {
root.set(fmd.getName(), null);
} else {
root.set(fmd.getName(),encodeManagedInstance(ctx.getStateManager(value), visited,
indent+1, false, model));
}
break;
case JavaTypes.ARRAY:
Object[] values = (Object[])value;
value = Arrays.asList(values);
// no break;
case JavaTypes.COLLECTION:
if (value == null) {
root.set(fmd.getName(), null);
break;
}
Collection<?> members = (Collection<?>)value;
JSONObject.Array array = new JSONObject.Array();
root.set(fmd.getName(), array);
if (members.isEmpty()) {
break;
}
boolean basic = fmd.getElement().getTypeMetaData() == null;
for (Object o : members) {
if (o == null) {
array.add(null);
} else {
if (basic) {
array.add(o);
} else {
array.add(encodeManagedInstance(ctx.getStateManager(o), visited, indent+1, true,
model));
}
}
}
break;
case JavaTypes.MAP:
if (value == null) {
root.set(fmd.getName(), null);
break;
}
Set<Map.Entry> entries = ((Map)value).entrySet();
JSONObject.KVMap map = new JSONObject.KVMap();
root.set(fmd.getName(), map);
if (entries.isEmpty()) {
break;
}
boolean basicKey = fmd.getElement().getTypeMetaData() == null;
boolean basicValue = fmd.getValue().getTypeMetaData() == null;
for (Map.Entry<?,?> e : entries) {
Object k = e.getKey();
Object v = e.getValue();
if (!basicKey) {
k = encodeManagedInstance(ctx.getStateManager(k), visited, indent+1, true, model);
}
if (!basicValue) {
v = encodeManagedInstance(ctx.getStateManager(e.getValue()), visited,
indent+1, false, model);
}
map.put(k,v);
}
break;
case JavaTypes.INPUT_STREAM:
case JavaTypes.INPUT_READER:
root.set(fmd.getName(), streamToString(value));
break;
case JavaTypes.PC_UNTYPED:
case JavaTypes.OBJECT:
case JavaTypes.OID:
root.set(fmd.getName(), "***UNSUPPORTED***");
}
}
return root;
}