private void convertAttribute( SubDocument.Builder builder, String columnName, Object jsonValue, SubDocType subDocType) { String attName = getAttributeName(columnName); if (attName != null) { // it is an attribute SubDocAttribute attribute = subDocType.getAttribute(attName); Value subDocValue = ValueToJsonConverterProvider.getInstance() .getConverter(attribute.getType()) .toValue(jsonValue); builder.add(attribute, subDocValue); } else { // it is a special field if (columnName.equals(SubDocTable.DID_COLUMN_NAME)) { builder.setDocumentId((Integer) jsonValue); } else if (columnName.equals(SubDocTable.INDEX_COLUMN_NAME)) { Integer index; if (jsonValue == null) { index = 0; } else { index = (Integer) jsonValue; } builder.setIndex(index); } else { throw new IllegalArgumentException( "The given record contains the attribute " + columnName + " which is not recognized"); } } }
public SubDocument from(String subdocAsJson, SubDocType subDocType) { JsonReader reader = Json.createReader(new StringReader(subdocAsJson)); JsonObject jsonObject = reader.readObject(); SubDocument.Builder builder = new SubDocument.Builder(); for (Map.Entry<String, JsonValue> entry : jsonObject.entrySet()) { Object objectValue = jsonValueToObject(entry.getValue()); convertAttribute(builder, entry.getKey(), objectValue, subDocType); } return builder.build(); }