下面列出了怎么用com.mongodb.util.JSONParseException的API类实例代码及写法,或者点击链接到github查看源代码。
static Object parseJSONExpr( String jsonExpr ) throws OdaException
{
try
{
return JSON.parse( jsonExpr );
}
catch( JSONParseException ex )
{
String errMsg = Messages.bind( Messages.driverUtil_parsingError,
jsonExpr );
DriverUtil.getLogger().log( Level.INFO, errMsg, ex ); // caller may choose to ignore it; log at INFO level
OdaException newEx = new OdaException( errMsg );
newEx.initCause( ex );
throw newEx;
}
}
@Override
public MongoQueryOptions buildQueryOptions(String rowLimit) {
MongoQueryOptions mongoQueryOptions = new MongoQueryOptions();
try {
mongoQueryOptions.setFilter(getQueryFrom(selectEditor));
mongoQueryOptions.setProjection(getQueryFrom(projectionEditor));
mongoQueryOptions.setSort(getQueryFrom(sortEditor));
} catch (JSONParseException ex) {
notifyOnErrorForOperator(selectEditor.getComponent(), ex);
}
if (StringUtils.isNotBlank(rowLimit)) {
mongoQueryOptions.setResultLimit(Integer.parseInt(rowLimit));
}
return mongoQueryOptions;
}
void notifyOnErrorForOperator(JComponent component, Exception ex) {
String message;
if (ex instanceof JSONParseException) {
message = StringUtils.removeStart(ex.getMessage(), "\n");
} else {
message = String.format("%s: %s", ex.getClass().getSimpleName(), ex.getMessage());
}
NonOpaquePanel nonOpaquePanel = new NonOpaquePanel();
JTextPane textPane = Messages.configureMessagePaneUi(new JTextPane(), message);
textPane.setFont(COURIER_FONT);
textPane.setBackground(MessageType.ERROR.getPopupBackground());
nonOpaquePanel.add(textPane, BorderLayout.CENTER);
nonOpaquePanel.add(new JLabel(MessageType.ERROR.getDefaultIcon()), BorderLayout.WEST);
JBPopupFactory.getInstance().createBalloonBuilder(nonOpaquePanel)
.setFillColor(MessageType.ERROR.getPopupBackground())
.createBalloon()
.show(new RelativePoint(component, new Point(0, 0)), Balloon.Position.above);
}
private boolean isInvalidJson(String body) {
if (StringUtils.isBlank(body)) {
return true;
}
try {
JSON.parse(body);
} catch (JSONParseException e) {
return true;
}
return false;
}
private boolean isValidJson(String json) {
if (StringUtils.isBlank(json)) {
return false;
} else {
try {
JSON.parse(json);
} catch (JSONParseException e) {
return false;
}
}
return true;
}
private boolean isInvalidJson(String json) {
if (StringUtils.isBlank(json)) {
return true;
}
try {
JSON.parse(json);
} catch (JSONParseException e) {
return true;
}
return false;
}
private boolean isValidJson(String json) {
if (StringUtils.isBlank(json)) {
return false;
} else {
try {
JSON.parse(json);
} catch (JSONParseException e) {
return false;
}
}
return true;
}
/**
* Read the current character, making sure that it is a hexidecimal character.
*
* @throws JSONParseException
* if the current character is not a hexidecimal character
*/
public void readHex() {
if (pos < s.length()
&& ((s.charAt(pos) >= '0' && s.charAt(pos) <= '9')
|| (s.charAt(pos) >= 'A' && s.charAt(pos) <= 'F') || (s.charAt(pos) >= 'a' && s
.charAt(pos) <= 'f'))) {
pos++;
} else {
throw new JSONParseException(s, pos);
}
}
/**
* Parses the next array.
*
* @return the array
* @throws JSONParseException
* if invalid JSON is found
*/
protected Object parseArray(String name) {
if (name != null) {
_callback.arrayStart(name);
} else {
_callback.arrayStart();
}
read('[');
int i = 0;
char current = get();
while (current != ']') {
String elemName = String.valueOf(i++);
Object elem = parse(elemName);
doCallback(elemName, elem);
if ((current = get()) == ',') {
read(',');
} else if (current == ']') {
break;
} else {
throw new JSONParseException(s, pos);
}
}
read(']');
return _callback.arrayDone();
}
@Override
public MongoQueryOptions buildQueryOptions(String rowLimit) {
MongoQueryOptions mongoQueryOptions = new MongoQueryOptions();
try {
mongoQueryOptions.setOperations(getQuery());
} catch (JSONParseException ex) {
notifyOnErrorForOperator(editor.getComponent(), ex);
}
if (StringUtils.isNotBlank(rowLimit)) {
mongoQueryOptions.setResultLimit(Integer.parseInt(rowLimit));
}
return mongoQueryOptions;
}
/**
* Parse an unknown type.
*
* @return Object the next item
* @throws JSONParseException
* if invalid JSON is found
*/
protected Object parse(String name) {
Object value = null;
char current = get();
switch (current) {
// null
case 'n':
read('n');
read('u');
read('l');
read('l');
value = null;
break;
// NaN
case 'N':
read('N');
read('a');
read('N');
value = Double.NaN;
break;
// true
case 't':
read('t');
read('r');
read('u');
read('e');
value = true;
break;
// false
case 'f':
read('f');
read('a');
read('l');
read('s');
read('e');
value = false;
break;
// string
case '\'':
case '\"':
value = parseString(true);
break;
// number
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
case '+':
case '-':
value = parseNumber();
break;
// array
case '[':
value = parseArray(name);
break;
// object
case '{':
value = parseObject(name);
break;
default:
throw new JSONParseException(s, pos);
}
return value;
}
/**
* Parses a string.
*
* @return the next string.
* @throws JSONParseException
* if invalid JSON is found
*/
public String parseString(boolean needQuote) {
char quot = 0;
if (check('\''))
quot = '\'';
else if (check('\"'))
quot = '\"';
else if (needQuote)
throw new JSONParseException(s, pos);
char current;
if (quot > 0)
read(quot);
StringBuilder buf = new StringBuilder();
int start = pos;
while (pos < s.length()) {
current = s.charAt(pos);
if (quot > 0) {
if (current == quot)
break;
} else {
if (current == ':' || current == ' ')
break;
}
if (current == '\\') {
pos++;
char x = get();
char special = 0;
switch (x) {
case 'u': { // decode unicode
buf.append(s.substring(start, pos - 1));
pos++;
int tempPos = pos;
readHex();
readHex();
readHex();
readHex();
int codePoint = Integer.parseInt(s.substring(tempPos, tempPos + 4),
16);
buf.append((char) codePoint);
start = pos;
continue;
}
case 'n':
special = '\n';
break;
case 'r':
special = '\r';
break;
case 't':
special = '\t';
break;
case 'b':
special = '\b';
break;
case '"':
special = '\"';
break;
case '\\':
special = '\\';
break;
}
buf.append(s.substring(start, pos - 1));
if (special != 0) {
pos++;
buf.append(special);
}
start = pos;
continue;
}
pos++;
}
buf.append(s.substring(start, pos));
if (quot > 0)
read(quot);
return buf.toString();
}
@Test(expected = JSONParseException.class)
public void parseBadJsonBodyToRow() {
final EventParser eventParser = new EventParser(MappingDefinition.load("/simple_body_row_json.json"));
eventParser.parse(EventBuilder.withBody("{???? \"foo\": \"bar\" }".getBytes(Charsets.UTF_8)));
}
/**
* Read the current character, making sure that it is the expected character.
* Advances the pointer to the next character.
*
* @param ch
* the character expected
*
* @throws JSONParseException
* if the current character does not match the given character
*/
public void read(char ch) {
if (!check(ch)) {
throw new JSONParseException(s, pos);
}
pos++;
}