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

The following examples show how to use com.fasterxml.jackson.core.JsonParser#isExpectedStartObjectToken() . 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: SdkPojoDeserializer.java    From cloudformation-cli-java-plugin with Apache License 2.0 6 votes vote down vote up
private Map<String, Object> readMap(SdkField<?> field, JsonParser p, DeserializationContext ctxt) throws IOException {
    if (!p.isExpectedStartObjectToken()) {
        throw new JsonMappingException(p, "Expected start of object token for Map got " + p.currentToken());
    }

    Map<String, Object> value = new LinkedHashMap<>();
    MapTrait trait = field.getTrait(MapTrait.class);
    SdkField<?> valueType = trait.valueFieldInfo();
    while (p.nextToken() != JsonToken.END_OBJECT) {
        if (p.currentToken() != JsonToken.FIELD_NAME) {
            throw new JsonMappingException(p, "Expecting String key for map got " + p.currentToken());
        }
        String fieldName = p.getCurrentName();
        // progress to next token
        p.nextToken();
        value.put(fieldName, readObject(valueType, p, ctxt));
    }
    return value;
}
 
Example 2
Source File: StdCallbackContext.java    From cloudformation-cli-java-plugin with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
private Map<String, Object> readMap(Class<?> type, JsonParser p, DeserializationContext ctxt) throws IOException {
    if (!p.isExpectedStartObjectToken()) {
        throw new JsonParseException(p, "Expected start of object for Map got " + p.currentToken());
    }
    try {
        Map<String, Object> value = (Map<String, Object>) type.getDeclaredConstructor().newInstance();
        JsonToken next = p.nextToken();
        while (next != JsonToken.END_OBJECT) {
            if (next != JsonToken.FIELD_NAME) {
                throw new JsonParseException(p, "Key was not present " + next);
            }
            String key = p.currentName();
            p.nextToken(); // position to next
            Object val = readObject(p, ctxt);
            value.put(key, val);
            next = p.nextToken();
        }
        return value;
    } catch (InstantiationException | IllegalAccessException | NoSuchMethodException | InvocationTargetException e) {
        throw new JsonMappingException(p, "Can not create empty map for class " + type + " @ " + p.getCurrentLocation(),
                                       e);
    }
}
 
Example 3
Source File: SdkPojoDeserializer.java    From cloudformation-cli-java-plugin with Apache License 2.0 5 votes vote down vote up
private SdkPojo readPojo(final SdkPojo pojo, JsonParser p, DeserializationContext ctxt) throws IOException {
    if (!p.isExpectedStartObjectToken()) {
        throw new JsonMappingException(p, "Expected to be in START_OBJECT got " + p.currentToken());
    }

    Map<String, SdkField<?>> fieldMap = getFields(pojo);
    JsonToken next = p.nextToken();
    ObjectMapper codec = (ObjectMapper) p.getCodec();
    while (next != JsonToken.END_OBJECT) {
        /*
         * if (next != JsonToken.FIELD_NAME) { throw new JsonMappingException(p,
         * "Expecting to get FIELD_NAME token, got " + next); }
         */
        String fieldName = p.getCurrentName();
        SdkField<?> sdkField = fieldMap.get(fieldName);
        if (sdkField == null) {
            if (codec.isEnabled(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES)) {
                throw new JsonMappingException(p, "Unknown property encountered " + fieldName);
            }
            // we need to skip this
            next = p.nextToken();
            if (next == JsonToken.START_ARRAY || next == JsonToken.START_OBJECT) {
                p.skipChildren();
            }
            // all others, just proceed to next token
            next = p.nextToken();
            continue;
        }
        // progress to next thing to read
        p.nextToken();
        // Okay we need to parse the field and set it
        Object value = readObject(sdkField, p, ctxt);
        sdkField.set(pojo, value);
        next = p.nextToken();
    }
    return pojo instanceof SdkBuilder ? (SdkPojo) ((SdkBuilder) pojo).build() : pojo;
}
 
Example 4
Source File: JSONSchemaPropsOrStringArraySerDe.java    From kubernetes-client with Apache License 2.0 5 votes vote down vote up
@Override
public JSONSchemaPropsOrStringArray deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException {
  JSONSchemaPropsOrStringArrayBuilder builder = new JSONSchemaPropsOrStringArrayBuilder();
  if (jsonParser.isExpectedStartObjectToken()) {
    builder.withSchema(
      jsonParser.readValueAs(JSONSchemaProps.class));
  } else if (jsonParser.isExpectedStartArrayToken()) {
    builder.withProperty(jsonParser.<List<String>>readValueAs(new TypeReference<List<String>>() {}));
  }
  return builder.build();
}
 
Example 5
Source File: JSONSchemaPropsOrBoolSerDe.java    From kubernetes-client with Apache License 2.0 5 votes vote down vote up
@Override
public JSONSchemaPropsOrBool deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException {
  JSONSchemaPropsOrBoolBuilder builder = new JSONSchemaPropsOrBoolBuilder();
  if (jsonParser.isExpectedStartObjectToken()) {
    builder.withSchema(
      jsonParser.readValueAs(JSONSchemaProps.class));
    builder.withAllows(true);
  } else {
    builder.withAllows(jsonParser.getBooleanValue());
  }
  return builder.build();
}
 
Example 6
Source File: JSONSchemaPropsOrArraySerDe.java    From kubernetes-client with Apache License 2.0 5 votes vote down vote up
@Override
public JSONSchemaPropsOrArray deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException {
  JSONSchemaPropsOrArrayBuilder builder = new JSONSchemaPropsOrArrayBuilder();
  if (jsonParser.isExpectedStartObjectToken()) {
    builder.withSchema(
      jsonParser.readValueAs(JSONSchemaProps.class));
  } else if (jsonParser.isExpectedStartArrayToken()) {
    builder.withJSONSchemas(jsonParser.<List<JSONSchemaProps>>readValueAs(new TypeReference<List<JSONSchemaProps>>() {}));
  }
  return builder.build();
}