下面列出了com.fasterxml.jackson.core.JsonToken#isStructStart ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Iterates through the JSON structure and collects weather data.
*/
private void handleToken(JsonParser jp, String property, Weather weather) throws Exception {
JsonToken token = jp.getCurrentToken();
String prop = PropertyResolver.add(property, jp.getCurrentName());
if (token.isStructStart()) {
boolean isObject = token == JsonToken.START_OBJECT || token == JsonToken.END_OBJECT;
Weather forecast = !isObject ? weather : startIfForecast(weather, prop);
while (!jp.nextValue().isStructEnd()) {
handleToken(jp, prop, forecast);
}
if (isObject) {
endIfForecast(weather, forecast, prop);
}
} else {
try {
setValue(weather, prop, jp.getValueAsString());
} catch (Exception ex) {
logger.error("Error setting property '{}' with value '{}' ({})", prop, jp.getValueAsString(),
ex.getMessage());
}
}
}
/**
* Helper used to skip to the end of the current object. Useful for forwards compatibility.
*
* @param jparser The parser to use for extraction.
* @throws IOException If there is an error parsing.
*/
protected void ignoreRestOfObject(JsonParser jparser)
throws IOException
{
if (jparser.getCurrentToken().isStructEnd()) {
return;
}
int open = 1;
/* Since proper matching of start/end markers is handled
* by nextToken(), we'll just count nesting levels here
*/
while (true) {
JsonToken t = jparser.nextToken();
if (t == null) {
throw new IllegalStateException("Expected " + JsonToken.END_OBJECT + " found " + jparser.getText());
}
if (t.isStructStart()) {
++open;
}
else if (t.isStructEnd()) {
if (--open == 0) {
return;
}
}
}
}
private int nestingOffset(JsonToken t) {
if (t.isStructStart()) {
return 1;
} else if (t.isStructEnd()) {
return -1;
} else {
return 0;
}
}
private boolean needsAcknowledgement(JsonParser parser) throws IOException{
boolean acknowledge = false;
boolean readAcknowledge = false;
int open = 1;
while(true){
JsonToken t = parser.nextToken();
if(readAcknowledge){
readAcknowledge = false;
if(t==JsonToken.VALUE_TRUE)
acknowledge = true;
else if(t==JsonToken.VALUE_FALSE)
acknowledge = false;
}
if(t==null)
throw new JsonParseException("unexpected EOF", parser.getCurrentLocation());
else if(t.isStructStart())
open++;
else if(t.isStructEnd()){
if(--open==0)
return acknowledge;
}else if(open==1 && t==JsonToken.FIELD_NAME && parser.getText().equals("acknowledge")){
readAcknowledge = true;
}
}
}
/**
* Skips a field name if necessary, returning the current token then, which must be
* the start of an Array or Object: '{' or '['.
*/
public static JsonToken peekStructStart(JsonParser par) throws IOException {
JsonToken token = peekToken(par);
if (token.isStructStart()) {
return token;
} else {
throw new JsonParseException(par, "Expected start of array or object");
}
}
/** {@inheritDoc} */
@Override public String deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
JsonToken startTok = p.getCurrentToken();
if (startTok.isStructStart()) {
StringWriter wrt = new StringWriter(4096);
JsonGenerator gen = factory.createGenerator(wrt);
JsonToken tok = startTok, endTok = startTok == START_ARRAY ? END_ARRAY : END_OBJECT;
int cnt = 1;
while (cnt > 0) {
writeToken(tok, p, gen);
tok = p.nextToken();
if (tok == startTok)
cnt++;
else if (tok == endTok)
cnt--;
}
gen.close();
return wrt.toString();
}
return p.getValueAsString();
}