Skip to content

Instantly share code, notes, and snippets.

@AndyFaibishenko
Created April 1, 2015 03:48
Show Gist options
  • Save AndyFaibishenko/3d27c78613b4880e41f0 to your computer and use it in GitHub Desktop.
Save AndyFaibishenko/3d27c78613b4880e41f0 to your computer and use it in GitHub Desktop.
convertFieldMapToJSON method
private static void convertFieldMapToJSON(DataDictionary dataDictionary,
FieldMap fieldmap, ObjectNode node) throws FieldNotFound {
// First process the "regular" fields.
Iterator<Field<?>> fieldIterator = fieldmap.iterator();
while (fieldIterator.hasNext()) {
Field field = (Field) fieldIterator.next();
String value = fieldmap.getString(field.getTag());
if (!isGroupCountField(dataDictionary, field)) {
node.put(String.valueOf(field.getTag()), value);
}
}
// Now process the repeating groups
Iterator groupsKeys = fieldmap.groupKeyIterator();
while (groupsKeys.hasNext()) {
int groupCountTag = ((Integer) groupsKeys.next()).intValue();
Group group = new Group(groupCountTag, 0);
ArrayNode repeatingGroup = node.putArray(String.valueOf(groupCountTag));
int i = 1;
while (fieldmap.hasGroup(i, groupCountTag)) {
fieldmap.getGroup(i, group);
ObjectNode groupNode = repeatingGroup.addObject();
// call this method recursively to process each repeating group to handle
// nested repeating groups
convertFieldMapToJSON(dataDictionary, group, groupNode);
i++;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment