Java Code Examples for com.fasterxml.jackson.core.JsonToken#START_ARRAY

The following examples show how to use com.fasterxml.jackson.core.JsonToken#START_ARRAY . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SessionStateDeserializer.java    From java-dcp-client with Apache License 2.0 7 votes vote down vote up
@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;
}
 
Example 2
Source File: BeanTable.java    From kripton with Apache License 2.0 5 votes vote down vote up
/**
 * 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()));
  }
}
 
Example 3
Source File: StringBeanTable.java    From kripton with Apache License 2.0 5 votes vote down vote up
/**
 * 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()));
  }
}
 
Example 4
Source File: GroupsSerialization.java    From heroic with Apache License 2.0 5 votes vote down vote up
@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);
}
 
Example 5
Source File: StringBeanTable.java    From kripton with Apache License 2.0 5 votes vote down vote up
/**
 * 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()));
  }
}
 
Example 6
Source File: WellKnownTypeMarshaller.java    From curiostack with MIT License 5 votes vote down vote up
@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);
  }
}
 
Example 7
Source File: JacksonStreamTest.java    From java-client-api with Apache License 2.0 5 votes vote down vote up
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;
}
 
Example 8
Source File: BindBean64SharedPreferences.java    From kripton with Apache License 2.0 5 votes vote down vote up
/**
 * 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()));
  }
}
 
Example 9
Source File: BindAppPreferences.java    From kripton with Apache License 2.0 5 votes vote down vote up
/**
 * 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()));
  }
}
 
Example 10
Source File: BindAppPreferences.java    From kripton with Apache License 2.0 5 votes vote down vote up
/**
 * 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()));
  }
}
 
Example 11
Source File: FloatDaoImpl.java    From kripton with Apache License 2.0 5 votes vote down vote up
/**
 * 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()));
  }
}
 
Example 12
Source File: BindBeanSharedPreferences.java    From kripton with Apache License 2.0 5 votes vote down vote up
/**
 * 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()));
  }
}
 
Example 13
Source File: BindAppPreferences.java    From kripton with Apache License 2.0 5 votes vote down vote up
/**
 * 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()));
  }
}
 
Example 14
Source File: BindBeanSharedPreferences.java    From kripton with Apache License 2.0 5 votes vote down vote up
/**
 * 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()));
  }
}
 
Example 15
Source File: ExecDeserializer.java    From postman2jmx with MIT License 5 votes vote down vote up
@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;
}
 
Example 16
Source File: BeanDaoImpl.java    From kripton with Apache License 2.0 5 votes vote down vote up
/**
 * 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()));
  }
}
 
Example 17
Source File: FloatBeanTable.java    From kripton with Apache License 2.0 5 votes vote down vote up
/**
 * 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()));
  }
}
 
Example 18
Source File: Bean2Table.java    From kripton with Apache License 2.0 5 votes vote down vote up
/**
 * 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()));
  }
}
 
Example 19
Source File: BindBean2SharedPreferences.java    From kripton with Apache License 2.0 5 votes vote down vote up
/**
 * 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()));
  }
}
 
Example 20
Source File: PersonBindMap.java    From kripton with Apache License 2.0 4 votes vote down vote up
/**
 * 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;
}