private static boolean isSimpleValue(IJSONNode node) { if (node == null) { return false; } return node.getNodeType() == IJSONNode.VALUE_BOOLEAN_NODE || node.getNodeType() == IJSONNode.VALUE_NULL_NODE || node.getNodeType() == IJSONNode.VALUE_NUMBER_NODE || node.getNodeType() == IJSONNode.VALUE_STRING_NODE; }
private static boolean isArray(IJSONNode node) { if (node == null) { return false; } int nodeType = node.getNodeType(); return nodeType == IJSONNode.ARRAY_NODE; }
private static boolean isObject(IJSONNode node) { if (node == null) { return false; } int nodeType = node.getNodeType(); return nodeType == IJSONNode.OBJECT_NODE; }
private static IJSONPair findByPath(IJSONNode node, String name) { if (node == null || node.getNodeType() != IJSONNode.OBJECT_NODE) { return null; } IJSONObject obj = (IJSONObject) node; for (int i = 0; i < obj.getLength(); i++) { try { IJSONNode n = (IJSONNode) obj.getClass().getMethod("item", int.class).invoke(obj, i); if (n.getNodeType() == IJSONNode.PAIR_NODE) { IJSONPair pair = (IJSONPair) n; if (name.equals(pair.getName())) { return pair; } } } catch (Exception e) { return null; } } return null; }
private static int getEndOffset(IJSONNode parent, boolean inside) { if (parent == null) { return 0; } switch (parent.getNodeType()) { case IJSONNode.OBJECT_NODE: if (parent.hasChildNodes()) { IJSONNode lastChild = parent.getLastChild(); boolean childInside = inside && (isSimpleValue(lastChild) || (lastChild.getNodeType() == IJSONNode.PAIR_NODE && isSimpleValue(((IJSONPair) lastChild).getValue()))); return getEndOffset(lastChild, childInside); } return parent.getStartOffset() + (inside ? 1 : 0); case IJSONNode.PAIR_NODE: if (!inside) { return parent.getEndOffset(); } IJSONPair pair = (IJSONPair) parent; IJSONValue value = (IJSONValue) pair.getValue(); if (value != null) { return getEndOffset(value, false); } return pair.getEndOffset(); case IJSONNode.VALUE_BOOLEAN_NODE: case IJSONNode.VALUE_NULL_NODE: case IJSONNode.VALUE_NUMBER_NODE: case IJSONNode.VALUE_STRING_NODE: return parent.getStartOffset() + parent.getFirstStructuredDocumentRegion().getFirstRegion().getLength(); default: return parent.getEndOffset(); } }