Java Code Examples for com.fasterxml.jackson.core.JsonParser#nextToken()

The following examples show how to use com.fasterxml.jackson.core.JsonParser#nextToken() . 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: JsonProcessor.java    From odata with Apache License 2.0 6 votes vote down vote up
/**
 * Initialize processor, automatically scanning the input JSON.
 * @throws ODataUnmarshallingException If unable to initialize
 */
public void initialize() throws ODataUnmarshallingException {
    LOG.info("Parser is initializing");
    try {
        JsonParser jsonParser = JSON_FACTORY.createParser(inputJson);

        while (jsonParser.nextToken() != JsonToken.END_OBJECT) {
            String token = jsonParser.getCurrentName();
            if (token != null) {
                if (token.startsWith(ODATA)) {
                    processSpecialTags(jsonParser);
                } else if (token.endsWith(ODATA_BIND)) {
                    processLinks(jsonParser);
                } else {
                    process(jsonParser);
                }
            }
        }
    } catch (IOException e) {
        throw new ODataUnmarshallingException("It is unable to unmarshall", e);
    }
}
 
Example 2
Source File: ClientCsdlInclude.java    From olingo-odata4 with Apache License 2.0 6 votes vote down vote up
@Override
protected Include doDeserialize(final JsonParser jp, final DeserializationContext ctxt)
        throws IOException {

  final ClientCsdlInclude include = new ClientCsdlInclude();

  for (; jp.getCurrentToken() != JsonToken.END_OBJECT; jp.nextToken()) {
    final JsonToken token = jp.getCurrentToken();
    if (token == JsonToken.FIELD_NAME) {
      if ("Namespace".equals(jp.getCurrentName())) {
        include.setNamespace(jp.nextTextValue());
      } else if ("Alias".equals(jp.getCurrentName())) {
        include.setAlias(jp.nextTextValue());
      }
    }
  }
  return include;
}
 
Example 3
Source File: RealmsConfigurationLoader.java    From keycloak with Apache License 2.0 6 votes vote down vote up
private static void readRoles(RealmRepresentation r, JsonParser p) throws IOException {
    JsonToken t = p.nextToken();
    if (t != JsonToken.START_OBJECT) {
        throw new RuntimeException("Error reading field 'roles'. Expected start of object [" + t + "]");
    }

    t = p.nextToken();
    if (t != JsonToken.FIELD_NAME) {
        throw new RuntimeException("Error reading field 'roles'. Expected field 'realm' or 'client' [" + t + "]");
    }

    while (t != JsonToken.END_OBJECT) {
        switch (p.getCurrentName()) {
            case "realm":
                readRealmRoles(r, p);
                break;
            case "client":
                waitForClientsCompleted();
                readClientRoles(r, p);
                break;
            default:
                throw new RuntimeException("Unexpected field in roles: " + p.getCurrentName());
        }
        t = p.nextToken();
    }
}
 
Example 4
Source File: JsonReader.java    From dropbox-sdk-java with MIT License 6 votes vote down vote up
/**
 * Helper to read and parse the optional ".tag" field. If one is found, positions the parser
 * at the next field (or the closing brace); otherwise leaves the parser position unchanged.
 * Returns null if there isn't a ".tag" field; otherwise an array of strings (the tags).
 * Initially the parser must be positioned right after the opening brace.
 */
public static String[] readTags(JsonParser parser)
    throws IOException, JsonReadException
{
    if (parser.getCurrentToken() != JsonToken.FIELD_NAME) {
        return null;
    }
    if (!".tag".equals(parser.getCurrentName())) {
        return null;
    }
    parser.nextToken();
    if (parser.getCurrentToken() != JsonToken.VALUE_STRING) {
        throw new JsonReadException("expected a string value for .tag field", parser.getTokenLocation());
    }
    String tag = parser.getText();
    parser.nextToken();
    return tag.split("\\.");
}
 
Example 5
Source File: PermissionAccountList.java    From web3j-quorum with Apache License 2.0 6 votes vote down vote up
@Override
public List<PermissionAccountInfo> deserialize(
        JsonParser jsonParser, DeserializationContext deserializationContext)
        throws IOException {
    List<PermissionAccountInfo> acctList = new ArrayList<>();
    JsonToken nextToken = jsonParser.nextToken();

    if (nextToken == JsonToken.START_OBJECT) {
        Iterator<PermissionAccountInfo> acctInfoIterator =
                om.readValues(jsonParser, PermissionAccountInfo.class);
        while (acctInfoIterator.hasNext()) {
            acctList.add(acctInfoIterator.next());
        }
        return acctList;
    } else {
        return null;
    }
}
 
Example 6
Source File: ArgWrapperJavaType.java    From servicecomb-java-chassis with Apache License 2.0 6 votes vote down vote up
public Map<String, Object> readValue(ObjectMapper mapper, String json) throws IOException {
  Map<String, Object> args = new LinkedHashMap<>();

  JsonParser jp = mapper.getFactory().createParser(json);
  DeserializationContext deserializationContext = ObjectMapperUtils.createDeserializationContext(mapper, jp);

  jp.nextToken();
  for (String fieldName = jp.nextFieldName(); fieldName != null; fieldName = jp.nextFieldName()) {
    jp.nextToken();
    ArgInfo argInfo = argInfos.get(fieldName);
    if (argInfo == null) {
      continue;
    }

    if (argInfo.deserializer == null) {
      argInfo.deserializer = deserializationContext.findRootValueDeserializer(argInfo.javaType);
    }

    args.put(fieldName, argInfo.deserializer.deserialize(jp, deserializationContext));
  }

  return args;
}
 
Example 7
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 8
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 9
Source File: NldBindMap.java    From kripton with Apache License 2.0 5 votes vote down vote up
/**
 * parse with jackson
 */
@Override
public Nld parseOnJacksonAsString(JsonParser jacksonParser) throws Exception {
  Nld instance = new Nld();
  String fieldName;
  if (jacksonParser.getCurrentToken() == null) {
    jacksonParser.nextToken();
  }
  if (jacksonParser.getCurrentToken() != JsonToken.START_OBJECT) {
    jacksonParser.skipChildren();
    return instance;
  }
  while (jacksonParser.nextToken() != JsonToken.END_OBJECT) {
    fieldName = jacksonParser.getCurrentName();
    jacksonParser.nextToken();

    // Parse fields:
    switch (fieldName) {
        case "common":
          // field common (mapped with "common")
          if (jacksonParser.currentToken()!=JsonToken.VALUE_NULL) {
            instance.common=jacksonParser.getText();
          }
        break;
        case "official":
          // field official (mapped with "official")
          if (jacksonParser.currentToken()!=JsonToken.VALUE_NULL) {
            instance.official=jacksonParser.getText();
          }
        break;
        default:
          jacksonParser.skipChildren();
        break;}
  }
  return instance;
}
 
Example 10
Source File: Bean02BindMap.java    From kripton with Apache License 2.0 5 votes vote down vote up
/**
 * parse with jackson
 */
@Override
public Bean02 parseOnJackson(JsonParser jacksonParser) throws Exception {
  Bean02 instance = new Bean02();
  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 "id":
          // field id (mapped with "id")
          instance.setId(jacksonParser.getLongValue());
        break;
        case "text":
          // field text (mapped with "text")
          if (jacksonParser.currentToken()!=JsonToken.VALUE_NULL) {
            instance.setText(jacksonParser.getText());
          }
        break;
        default:
          jacksonParser.skipChildren();
        break;}
  }
  return instance;
}
 
Example 11
Source File: VoltageExtensionSerializer.java    From powsybl-core with Mozilla Public License 2.0 5 votes vote down vote up
@Override
public VoltageExtension deserialize(JsonParser parser, DeserializationContext deserializationContext) throws IOException {
    double value = Double.NaN;

    while (parser.nextToken() != JsonToken.END_OBJECT) {
        if (parser.getCurrentName().equals("preContingencyValue")) {
            parser.nextToken();
            value = parser.readValueAs(Float.class);
        } else {
            throw new PowsyblException("Unexpected field: " + parser.getCurrentName());
        }
    }

    return new VoltageExtension(value);
}
 
Example 12
Source File: Bean01BindMap.java    From kripton with Apache License 2.0 5 votes vote down vote up
/**
 * parse with jackson
 */
@Override
public Bean01 parseOnJacksonAsString(JsonParser jacksonParser) throws Exception {
  Bean01 instance = new Bean01();
  String fieldName;
  if (jacksonParser.getCurrentToken() == null) {
    jacksonParser.nextToken();
  }
  if (jacksonParser.getCurrentToken() != JsonToken.START_OBJECT) {
    jacksonParser.skipChildren();
    return instance;
  }
  while (jacksonParser.nextToken() != JsonToken.END_OBJECT) {
    fieldName = jacksonParser.getCurrentName();
    jacksonParser.nextToken();

    // Parse fields:
    switch (fieldName) {
        case "id":
          // field id (mapped with "id")
          instance.setId(PrimitiveUtils.readLong(jacksonParser.getText(), 0L));
        break;
        case "text":
          // field text (mapped with "text")
          if (jacksonParser.currentToken()!=JsonToken.VALUE_NULL) {
            instance.setText(jacksonParser.getText());
          }
        break;
        case "value":
          // field value (mapped with "value")
          if (jacksonParser.currentToken()!=JsonToken.VALUE_NULL) {
            instance.value=PrimitiveUtils.readDouble(jacksonParser.getText(), null);
          }
        break;
        default:
          jacksonParser.skipChildren();
        break;}
  }
  return instance;
}
 
Example 13
Source File: ShortBeanTable.java    From kripton with Apache License 2.0 5 votes vote down vote up
/**
 * for attribute value2 parsing
 */
public static LinkedList<Short> 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<Short> result=null;
    if (jacksonParser.currentToken()==JsonToken.START_ARRAY) {
      LinkedList<Short> collection=new LinkedList<>();
      Short item=null;
      while (jacksonParser.nextToken() != JsonToken.END_ARRAY) {
        if (jacksonParser.currentToken()==JsonToken.VALUE_NULL) {
          item=null;
        } else {
          item=jacksonParser.getShortValue();
        }
        collection.add(item);
      }
      result=collection;
    }
    return result;
  } catch(Exception e) {
    e.printStackTrace();
    throw(new KriptonRuntimeException(e.getMessage()));
  }
}
 
Example 14
Source File: BeanTable.java    From kripton with Apache License 2.0 5 votes vote down vote up
/**
 * for attribute valueSetString parsing
 */
public static Set<String> parseValueSetString(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();
    Set<String> result=null;
    if (jacksonParser.currentToken()==JsonToken.START_ARRAY) {
      HashSet<String> collection=new HashSet<>();
      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 15
Source File: DoubleDaoImpl.java    From kripton with Apache License 2.0 5 votes vote down vote up
/**
 * for param parser2 parsing
 */
private Double[] 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();
    Double[] result=null;
    if (jacksonParser.currentToken()==JsonToken.START_ARRAY) {
      ArrayList<Double> collection=new ArrayList<>();
      Double item=null;
      while (jacksonParser.nextToken() != JsonToken.END_ARRAY) {
        if (jacksonParser.currentToken()==JsonToken.VALUE_NULL) {
          item=null;
        } else {
          item=jacksonParser.getDoubleValue();
        }
        collection.add(item);
      }
      result=CollectionUtils.asDoubleArray(collection);
    }
    return result;
  } catch(Exception e) {
    e.printStackTrace();
    throw(new KriptonRuntimeException(e.getMessage()));
  }
}
 
Example 16
Source File: CurrentExtensionSerializer.java    From powsybl-core with Mozilla Public License 2.0 5 votes vote down vote up
@Override
public CurrentExtension deserialize(JsonParser parser, DeserializationContext deserializationContext) throws IOException {
    double value = Double.NaN;

    while (parser.nextToken() != JsonToken.END_OBJECT) {
        if (parser.getCurrentName().equals("preContingencyValue")) {
            parser.nextToken();
            value = parser.readValueAs(Double.class);
        } else {
            throw new PowsyblException("Unexpected field: " + parser.getCurrentName());
        }
    }

    return new CurrentExtension(value);
}
 
Example 17
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 18
Source File: CompanyBindMap.java    From kripton with Apache License 2.0 4 votes vote down vote up
/**
 * parse with jackson
 */
@Override
public Company parseOnJacksonAsString(JsonParser jacksonParser) throws Exception {
  Company instance = new Company();
  String fieldName;
  if (jacksonParser.getCurrentToken() == null) {
    jacksonParser.nextToken();
  }
  if (jacksonParser.getCurrentToken() != JsonToken.START_OBJECT) {
    jacksonParser.skipChildren();
    return instance;
  }
  while (jacksonParser.nextToken() != JsonToken.END_OBJECT) {
    fieldName = jacksonParser.getCurrentName();
    jacksonParser.nextToken();

    // Parse fields:
    switch (fieldName) {
        case "bs":
          // field bs (mapped with "bs")
          if (jacksonParser.currentToken()!=JsonToken.VALUE_NULL) {
            instance.bs=jacksonParser.getText();
          }
        break;
        case "catchPhrase":
          // field catchPhrase (mapped with "catchPhrase")
          if (jacksonParser.currentToken()!=JsonToken.VALUE_NULL) {
            instance.catchPhrase=jacksonParser.getText();
          }
        break;
        case "name":
          // field name (mapped with "name")
          if (jacksonParser.currentToken()!=JsonToken.VALUE_NULL) {
            instance.name=jacksonParser.getText();
          }
        break;
        default:
          jacksonParser.skipChildren();
        break;}
  }
  return instance;
}
 
Example 19
Source File: PropertyBindMap.java    From kripton with Apache License 2.0 4 votes vote down vote up
/**
 * parse with jackson
 */
@Override
public Property parseOnJacksonAsString(JsonParser jacksonParser) throws Exception {
  Property instance = new Property();
  String fieldName;
  if (jacksonParser.getCurrentToken() == null) {
    jacksonParser.nextToken();
  }
  if (jacksonParser.getCurrentToken() != JsonToken.START_OBJECT) {
    jacksonParser.skipChildren();
    return instance;
  }
  while (jacksonParser.nextToken() != JsonToken.END_OBJECT) {
    fieldName = jacksonParser.getCurrentName();
    jacksonParser.nextToken();

    // Parse fields:
    switch (fieldName) {
        case "name":
          // field name (mapped with "name")
          if (jacksonParser.currentToken()!=JsonToken.VALUE_NULL) {
            instance.name=jacksonParser.getText();
          }
        break;
        case "value":
          // field value (mapped with "value")
          if (jacksonParser.currentToken()!=JsonToken.VALUE_NULL) {
            instance.value=jacksonParser.getText();
          }
        break;
        case "content":
          // field content (mapped with "content")
          if (jacksonParser.currentToken()!=JsonToken.VALUE_NULL) {
            instance.content=jacksonParser.getText();
          }
        break;
        default:
          jacksonParser.skipChildren();
        break;}
  }
  return instance;
}
 
Example 20
Source File: TranslationBindMap.java    From kripton with Apache License 2.0 4 votes vote down vote up
/**
 * parse with jackson
 */
@Override
public Translation parseOnJackson(JsonParser jacksonParser) throws Exception {
  Translation instance = new Translation();
  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 "name":
          // field name (mapped with "name")
          if (jacksonParser.currentToken()!=JsonToken.VALUE_NULL) {
            instance.setName(jacksonParser.getText());
          }
        break;
        case "loop":
          // field loop (mapped with "loop")
          instance.setLoop(jacksonParser.getBooleanValue());
        break;
        case "rate":
          // field rate (mapped with "rate")
          instance.setRate(jacksonParser.getFloatValue());
        break;
        case "frames":
          // field frames (mapped with "frames")
          if (jacksonParser.currentToken()==JsonToken.START_ARRAY) {
            ArrayList<TranslationFrame> collection=new ArrayList<>();
            TranslationFrame item=null;
            while (jacksonParser.nextToken() != JsonToken.END_ARRAY) {
              if (jacksonParser.currentToken()==JsonToken.VALUE_NULL) {
                item=null;
              } else {
                item=translationFrameBindMap.parseOnJackson(jacksonParser);
              }
              collection.add(item);
            }
            instance.frames=collection;
          }
        break;
        default:
          jacksonParser.skipChildren();
        break;}
  }
  return instance;
}