下面列出了com.fasterxml.jackson.core.JsonToken#START_ARRAY 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
public SessionState deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
JsonToken current = p.getCurrentToken();
// we ignore the version for now, since there is only one. go directly to the array of states.
while (current != JsonToken.START_ARRAY) {
current = p.nextToken();
}
current = p.nextToken();
int i = 0;
SessionState ss = new SessionState();
while (current != null && current != JsonToken.END_ARRAY) {
PartitionState ps = p.readValueAs(PartitionState.class);
ss.set(i++, ps);
current = p.nextToken();
}
return ss;
}
/**
* for attribute valueMapStringBean parsing
*/
protected Map<String, Bean> parseValueMapStringBean(String input) {
if (input==null) {
return null;
}
KriptonJsonContext context=KriptonBinder.jsonBind();
try (JacksonWrapperParser wrapper=context.createParser(input)) {
JsonParser jacksonParser=wrapper.jacksonParser;
// START_OBJECT
jacksonParser.nextToken();
// value of "element"
jacksonParser.nextValue();
Map<String, Bean> result=null;
if (jacksonParser.currentToken()==JsonToken.START_ARRAY) {
HashMap<String, Bean> collection=new HashMap<>();
String key=null;
Bean value=null;
while (jacksonParser.nextToken() != JsonToken.END_ARRAY) {
jacksonParser.nextValue();
key=jacksonParser.getText();
jacksonParser.nextValue();
if (jacksonParser.currentToken()==JsonToken.START_OBJECT) {
value=beanBindMap.parseOnJackson(jacksonParser);
}
collection.put(key, value);
key=null;
value=null;
jacksonParser.nextToken();
}
result=collection;
}
return result;
} catch(Exception e) {
e.printStackTrace();
throw(new KriptonRuntimeException(e.getMessage()));
}
}
/**
* for attribute valueIntegerSet parsing
*/
protected LinkedHashSet<Integer> parseValueIntegerSet(String input) {
if (input==null) {
return null;
}
KriptonJsonContext context=KriptonBinder.jsonBind();
try (JacksonWrapperParser wrapper=context.createParser(input)) {
JsonParser jacksonParser=wrapper.jacksonParser;
// START_OBJECT
jacksonParser.nextToken();
// value of "element"
jacksonParser.nextValue();
LinkedHashSet<Integer> result=null;
if (jacksonParser.currentToken()==JsonToken.START_ARRAY) {
LinkedHashSet<Integer> collection=new LinkedHashSet<>();
Integer item=null;
while (jacksonParser.nextToken() != JsonToken.END_ARRAY) {
if (jacksonParser.currentToken()==JsonToken.VALUE_NULL) {
item=null;
} else {
item=jacksonParser.getIntValue();
}
collection.add(item);
}
result=collection;
}
return result;
} catch(Exception e) {
e.printStackTrace();
throw(new KriptonRuntimeException(e.getMessage()));
}
}
/**
* for attribute valueFloatSet parsing
*/
public static HashSet<Float> parseValueFloatSet(byte[] input) {
if (input==null) {
return null;
}
KriptonJsonContext context=KriptonBinder.jsonBind();
try (JacksonWrapperParser wrapper=context.createParser(input)) {
JsonParser jacksonParser=wrapper.jacksonParser;
// START_OBJECT
jacksonParser.nextToken();
// value of "element"
jacksonParser.nextValue();
HashSet<Float> result=null;
if (jacksonParser.currentToken()==JsonToken.START_ARRAY) {
HashSet<Float> collection=new HashSet<>();
Float item=null;
while (jacksonParser.nextToken() != JsonToken.END_ARRAY) {
if (jacksonParser.currentToken()==JsonToken.VALUE_NULL) {
item=null;
} else {
item=jacksonParser.getFloatValue();
}
collection.add(item);
}
result=collection;
}
return result;
} catch(Exception e) {
e.printStackTrace();
throw(new KriptonRuntimeException(e.getMessage()));
}
}
/**
* for attribute value2 parsing
*/
public static LinkedList<Float> parseValue2(byte[] input) {
if (input==null) {
return null;
}
KriptonJsonContext context=KriptonBinder.jsonBind();
try (JacksonWrapperParser wrapper=context.createParser(input)) {
JsonParser jacksonParser=wrapper.jacksonParser;
// START_OBJECT
jacksonParser.nextToken();
// value of "element"
jacksonParser.nextValue();
LinkedList<Float> result=null;
if (jacksonParser.currentToken()==JsonToken.START_ARRAY) {
LinkedList<Float> collection=new LinkedList<>();
Float item=null;
while (jacksonParser.nextToken() != JsonToken.END_ARRAY) {
if (jacksonParser.currentToken()==JsonToken.VALUE_NULL) {
item=null;
} else {
item=jacksonParser.getFloatValue();
}
collection.add(item);
}
result=collection;
}
return result;
} catch(Exception e) {
e.printStackTrace();
throw(new KriptonRuntimeException(e.getMessage()));
}
}
/**
* for param parser1 parsing
*/
private Map<String, Byte> parser1(byte[] input) {
if (input==null) {
return null;
}
KriptonJsonContext context=KriptonBinder.jsonBind();
try (JacksonWrapperParser wrapper=context.createParser(input)) {
JsonParser jacksonParser=wrapper.jacksonParser;
// START_OBJECT
jacksonParser.nextToken();
// value of "element"
jacksonParser.nextValue();
Map<String, Byte> result=null;
if (jacksonParser.currentToken()==JsonToken.START_ARRAY) {
HashMap<String, Byte> collection=new HashMap<>();
String key=null;
Byte value=null;
while (jacksonParser.nextToken() != JsonToken.END_ARRAY) {
jacksonParser.nextValue();
key=jacksonParser.getText();
jacksonParser.nextValue();
if (jacksonParser.currentToken()!=JsonToken.VALUE_NULL) {
value=jacksonParser.getByteValue();
}
collection.put(key, value);
key=null;
value=null;
jacksonParser.nextToken();
}
result=collection;
}
return result;
} catch(Exception e) {
e.printStackTrace();
throw(new KriptonRuntimeException(e.getMessage()));
}
}
@Override
public ExecDeserializer.ExecData deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException, JsonProcessingException {
ExecDeserializer.ExecData execData = new ExecDeserializer.ExecData();
if (jsonParser.getCurrentToken() == JsonToken.VALUE_STRING) {
execData.addValue(jsonParser.getValueAsString());
} else if (jsonParser.getCurrentToken() == JsonToken.START_ARRAY) {
ObjectMapper mapper = new ObjectMapper();
List<String> values = mapper.readValue(jsonParser, List.class);
execData.setValues(values);
}
return execData;
}
/**
* for attribute valueTimeList parsing
*/
protected List<Time> parseValueTimeList(String input) {
if (input==null) {
return null;
}
KriptonJsonContext context=KriptonBinder.jsonBind();
try (JacksonWrapperParser wrapper=context.createParser(input)) {
JsonParser jacksonParser=wrapper.jacksonParser;
// START_OBJECT
jacksonParser.nextToken();
// value of "element"
jacksonParser.nextValue();
List<Time> result=null;
if (jacksonParser.currentToken()==JsonToken.START_ARRAY) {
ArrayList<Time> collection=new ArrayList<>();
Time item=null;
while (jacksonParser.nextToken() != JsonToken.END_ARRAY) {
if (jacksonParser.currentToken()==JsonToken.VALUE_NULL) {
item=null;
} else {
item=SQLTimeUtils.read(jacksonParser.getText());
}
collection.add(item);
}
result=collection;
}
return result;
} catch(Exception e) {
e.printStackTrace();
throw(new KriptonRuntimeException(e.getMessage()));
}
}
/**
* for attribute stringList parsing
*/
protected List<String> parseStringList(String input) {
if (input==null) {
return null;
}
KriptonJsonContext context=KriptonBinder.jsonBind();
try (JacksonWrapperParser wrapper=context.createParser(input)) {
JsonParser jacksonParser=wrapper.jacksonParser;
// START_OBJECT
jacksonParser.nextToken();
// value of "element"
jacksonParser.nextValue();
List<String> result=null;
if (jacksonParser.currentToken()==JsonToken.START_ARRAY) {
ArrayList<String> collection=new ArrayList<>();
String item=null;
while (jacksonParser.nextToken() != JsonToken.END_ARRAY) {
if (jacksonParser.currentToken()==JsonToken.VALUE_NULL) {
item=null;
} else {
item=jacksonParser.getText();
}
collection.add(item);
}
result=collection;
}
return result;
} catch(Exception e) {
e.printStackTrace();
throw(new KriptonRuntimeException(e.getMessage()));
}
}
/**
* for attribute valueLongTypeArray parsing
*/
public static long[] parseValueLongTypeArray(byte[] input) {
if (input==null) {
return null;
}
KriptonJsonContext context=KriptonBinder.jsonBind();
try (JacksonWrapperParser wrapper=context.createParser(input)) {
JsonParser jacksonParser=wrapper.jacksonParser;
// START_OBJECT
jacksonParser.nextToken();
// value of "element"
jacksonParser.nextValue();
long[] result=null;
if (jacksonParser.currentToken()==JsonToken.START_ARRAY) {
ArrayList<Long> collection=new ArrayList<>();
Long item=null;
while (jacksonParser.nextToken() != JsonToken.END_ARRAY) {
if (jacksonParser.currentToken()==JsonToken.VALUE_NULL) {
item=null;
} else {
item=jacksonParser.getLongValue();
}
collection.add(item);
}
result=CollectionUtils.asLongTypeArray(collection);
}
return result;
} catch(Exception e) {
e.printStackTrace();
throw(new KriptonRuntimeException(e.getMessage()));
}
}
/**
* for param parser2 parsing
*/
private Float[] parser2(byte[] input) {
if (input==null) {
return null;
}
KriptonJsonContext context=KriptonBinder.jsonBind();
try (JacksonWrapperParser wrapper=context.createParser(input)) {
JsonParser jacksonParser=wrapper.jacksonParser;
// START_OBJECT
jacksonParser.nextToken();
// value of "element"
jacksonParser.nextValue();
Float[] result=null;
if (jacksonParser.currentToken()==JsonToken.START_ARRAY) {
ArrayList<Float> collection=new ArrayList<>();
Float item=null;
while (jacksonParser.nextToken() != JsonToken.END_ARRAY) {
if (jacksonParser.currentToken()==JsonToken.VALUE_NULL) {
item=null;
} else {
item=jacksonParser.getFloatValue();
}
collection.add(item);
}
result=CollectionUtils.asFloatArray(collection);
}
return result;
} catch(Exception e) {
e.printStackTrace();
throw(new KriptonRuntimeException(e.getMessage()));
}
}
/**
* for attribute stringList parsing
*/
protected List<String> parseStringList(String input) {
if (input==null) {
return null;
}
KriptonJsonContext context=KriptonBinder.jsonBind();
try (JacksonWrapperParser wrapper=context.createParser(input)) {
JsonParser jacksonParser=wrapper.jacksonParser;
// START_OBJECT
jacksonParser.nextToken();
// value of "element"
jacksonParser.nextValue();
List<String> result=null;
if (jacksonParser.currentToken()==JsonToken.START_ARRAY) {
ArrayList<String> collection=new ArrayList<>();
String item=null;
while (jacksonParser.nextToken() != JsonToken.END_ARRAY) {
if (jacksonParser.currentToken()==JsonToken.VALUE_NULL) {
item=null;
} else {
item=jacksonParser.getText();
}
collection.add(item);
}
result=collection;
}
return result;
} catch(Exception e) {
e.printStackTrace();
throw(new KriptonRuntimeException(e.getMessage()));
}
}
/**
* for attribute stringArray parsing
*/
protected String[] parseStringArray(String input) {
if (input==null) {
return null;
}
KriptonJsonContext context=KriptonBinder.jsonBind();
try (JacksonWrapperParser wrapper=context.createParser(input)) {
JsonParser jacksonParser=wrapper.jacksonParser;
// START_OBJECT
jacksonParser.nextToken();
// value of "element"
jacksonParser.nextValue();
String[] __stringArray=null;
if (jacksonParser.currentToken()==JsonToken.START_ARRAY) {
ArrayList<String> collection=new ArrayList<>();
String item=null;
while (jacksonParser.nextToken() != JsonToken.END_ARRAY) {
if (jacksonParser.currentToken()==JsonToken.VALUE_NULL) {
item=null;
} else {
item=jacksonParser.getText();
}
collection.add(item);
}
__stringArray=CollectionUtils.asArray(collection, new String[collection.size()]);
}
return __stringArray;
} catch(Exception e) {
e.printStackTrace();
throw(new KriptonRuntimeException(e.getMessage()));
}
}
/**
* for attribute valueCharArray parsing
*/
protected Character[] parseValueCharArray(String input) {
if (input==null) {
return null;
}
KriptonJsonContext context=KriptonBinder.jsonBind();
try (JacksonWrapperParser wrapper=context.createParser(input)) {
JsonParser jacksonParser=wrapper.jacksonParser;
// START_OBJECT
jacksonParser.nextToken();
// value of "element"
jacksonParser.nextValue();
Character[] result=null;
if (jacksonParser.currentToken()==JsonToken.START_ARRAY) {
ArrayList<Character> collection=new ArrayList<>();
Character item=null;
while (jacksonParser.nextToken() != JsonToken.END_ARRAY) {
if (jacksonParser.currentToken()==JsonToken.VALUE_NULL) {
item=null;
} else {
item=Character.valueOf((char)jacksonParser.getIntValue());
}
collection.add(item);
}
result=CollectionUtils.asCharacterArray(collection);
}
return result;
} catch(Exception e) {
e.printStackTrace();
throw(new KriptonRuntimeException(e.getMessage()));
}
}
private List<OrderItem> getOrderItems(JsonParser parser) throws IOException {
List<OrderItem> orderItems = new ArrayList<>();
while ( parser.nextValue() != null ) {
if ( parser.getCurrentToken() == JsonToken.START_ARRAY &&
"orderItems".equals(parser.getCurrentName()) )
{
while ( parser.nextValue() == JsonToken.START_OBJECT ) {
OrderItem item = getOrderItem(parser);
orderItems.add(item);
}
return orderItems;
}
}
return null;
}
@Override
public void doMerge(JsonParser parser, int currentDepth, Message.Builder messageBuilder)
throws IOException {
JsonToken token = parser.currentToken();
if (token != JsonToken.START_ARRAY) {
throw new InvalidProtocolBufferException("Expect an array but found: " + parser.getText());
}
ListValue.Builder builder = (ListValue.Builder) messageBuilder;
while (parser.nextValue() != JsonToken.END_ARRAY) {
Value.Builder valueBuilder = builder.addValuesBuilder();
ValueMarshaller.INSTANCE.mergeValue(parser, currentDepth + 1, valueBuilder);
}
}
/**
* for attribute value2 parsing
*/
public static String[] parseValue2(byte[] input) {
if (input==null) {
return null;
}
KriptonJsonContext context=KriptonBinder.jsonBind();
try (JacksonWrapperParser wrapper=context.createParser(input)) {
JsonParser jacksonParser=wrapper.jacksonParser;
// START_OBJECT
jacksonParser.nextToken();
// value of "element"
jacksonParser.nextValue();
String[] result=null;
if (jacksonParser.currentToken()==JsonToken.START_ARRAY) {
ArrayList<String> collection=new ArrayList<>();
String item=null;
while (jacksonParser.nextToken() != JsonToken.END_ARRAY) {
if (jacksonParser.currentToken()==JsonToken.VALUE_NULL) {
item=null;
} else {
item=jacksonParser.getText();
}
collection.add(item);
}
result=CollectionUtils.asArray(collection, new String[collection.size()]);
}
return result;
} catch(Exception e) {
e.printStackTrace();
throw(new KriptonRuntimeException(e.getMessage()));
}
}
@Override
public Groups deserialize(JsonParser p, DeserializationContext c) throws IOException {
/* fallback to default parser if object */
if (p.getCurrentToken() == JsonToken.START_ARRAY) {
final List<String> groups = p.readValueAs(LIST_OF_STRINGS);
return new Groups(ImmutableSet.copyOf(groups));
}
if (p.getCurrentToken() == JsonToken.VALUE_STRING) {
return new Groups(ImmutableSet.of(p.getText()));
}
throw c.wrongTokenException(p, JsonToken.START_ARRAY, null);
}
/**
* for attribute value2 parsing
*/
public static String[] parseValue2(byte[] input) {
if (input==null) {
return null;
}
KriptonJsonContext context=KriptonBinder.jsonBind();
try (JacksonWrapperParser wrapper=context.createParser(input)) {
JsonParser jacksonParser=wrapper.jacksonParser;
// START_OBJECT
jacksonParser.nextToken();
// value of "element"
jacksonParser.nextValue();
String[] result=null;
if (jacksonParser.currentToken()==JsonToken.START_ARRAY) {
ArrayList<String> collection=new ArrayList<>();
String item=null;
while (jacksonParser.nextToken() != JsonToken.END_ARRAY) {
if (jacksonParser.currentToken()==JsonToken.VALUE_NULL) {
item=null;
} else {
item=jacksonParser.getText();
}
collection.add(item);
}
result=CollectionUtils.asArray(collection, new String[collection.size()]);
}
return result;
} catch(Exception e) {
e.printStackTrace();
throw(new KriptonRuntimeException(e.getMessage()));
}
}
/**
* parse with jackson
*/
@Override
public Person parseOnJackson(JsonParser jacksonParser) throws Exception {
Person instance = new Person();
String fieldName;
if (jacksonParser.currentToken() == null) {
jacksonParser.nextToken();
}
if (jacksonParser.currentToken() != JsonToken.START_OBJECT) {
jacksonParser.skipChildren();
return instance;
}
while (jacksonParser.nextToken() != JsonToken.END_OBJECT) {
fieldName = jacksonParser.getCurrentName();
jacksonParser.nextToken();
// Parse fields:
switch (fieldName) {
case "birthday":
// field birthday (mapped with "birthday")
if (jacksonParser.currentToken()!=JsonToken.VALUE_NULL) {
instance.birthday=DateUtils.read(jacksonParser.getText());
}
break;
case "name":
// field name (mapped with "name")
if (jacksonParser.currentToken()!=JsonToken.VALUE_NULL) {
instance.name=jacksonParser.getText();
}
break;
case "surname":
// field surname (mapped with "surname")
if (jacksonParser.currentToken()!=JsonToken.VALUE_NULL) {
instance.surname=jacksonParser.getText();
}
break;
case "tags":
// field tags (mapped with "tags")
if (jacksonParser.currentToken()==JsonToken.START_ARRAY) {
ArrayList<String> collection=new ArrayList<>();
String item=null;
while (jacksonParser.nextToken() != JsonToken.END_ARRAY) {
if (jacksonParser.currentToken()==JsonToken.VALUE_NULL) {
item=null;
} else {
item=jacksonParser.getText();
}
collection.add(item);
}
instance.tags=collection;
}
break;
default:
jacksonParser.skipChildren();
break;}
}
return instance;
}