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

The following examples show how to use com.fasterxml.jackson.core.JsonParser#readValueAs() . 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: FilterHintListJsonDeserializer.java    From pinpoint with Apache License 2.0 6 votes vote down vote up
@Override
public FilterHint deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException {
    if (!jp.getCurrentToken().isStructStart()) {
        ctxt.handleUnexpectedToken(RpcHint.class, jp);
    }
    // skip json start
    final JsonToken jsonToken = jp.nextToken();
    if (jsonToken == JsonToken.END_OBJECT) {
        return new FilterHint(Collections.emptyList());
    }


    List<RpcHint> rpcHintList = new ArrayList<>();
    while (true) {
        final RpcHint rpcHint = jp.readValueAs(RpcHint.class);
        rpcHintList.add(rpcHint);
        if (jp.nextToken() == JsonToken.END_OBJECT) {
            break;
        }
    }
    return new FilterHint(rpcHintList);
}
 
Example 2
Source File: RawKeyStdDeserializer.java    From gwt-jackson with Apache License 2.0 6 votes vote down vote up
@Override
public Key deserialize( JsonParser jp, DeserializationContext ctxt ) throws IOException {
    JsonNode node = jp.readValueAsTree();
    Key parent = null;
    if ( node.has( RawKeyConstant.PARENT ) ) {
        JsonParser parentJsonParser = node.get( RawKeyConstant.PARENT ).traverse();
        parentJsonParser.setCodec( jp.getCodec() );
        parent = parentJsonParser.readValueAs( Key.class );
    }
    String kind = null;
    if ( node.has( RawKeyConstant.KIND ) ) {
        kind = node.get( RawKeyConstant.KIND ).asText();
    }
    long id = node.get( RawKeyConstant.ID ).asLong();

    if ( id != 0 ) {
        return KeyFactory.createKey( parent, kind, id );
    }

    String name = null;
    if ( node.has( RawKeyConstant.NAME ) ) {
        name = node.get( RawKeyConstant.NAME ).asText();
    }
    return KeyFactory.createKey( parent, kind, name );
}
 
Example 3
Source File: FooExtSerializer.java    From powsybl-core with Mozilla Public License 2.0 6 votes vote down vote up
@Override
public FooExt deserialize(JsonParser parser, DeserializationContext deserializationContext) throws IOException {
    Boolean value = null;
    while (parser.nextToken() != JsonToken.END_OBJECT) {
        if (parser.getCurrentName().equals("value")) {
            parser.nextToken();
            value = parser.readValueAs(Boolean.class);
        } else {
            throw new PowsyblException("Unexpected field: " + parser.getCurrentName());
        }
    }

    if (value == null) {
        throw new PowsyblException("Value has not been read");
    }

    return new FooExt(value);
}
 
Example 4
Source File: RefStdDeserializer.java    From gwt-jackson with Apache License 2.0 6 votes vote down vote up
@Override
public Ref<?> deserialize( JsonParser jp, DeserializationContext ctxt ) throws IOException {
    if ( valueType == null ) {
        throw JsonMappingException.from( ctxt, "valueType can't be null." );
    }

    JsonNode jsonNode = jp.readValueAsTree();

    JsonParser keyJsonParser = jsonNode.get( RefConstant.KEY ).traverse();
    keyJsonParser.setCodec( jp.getCodec() );
    Key key = keyJsonParser.readValueAs( Key.class );

    Object value = null;
    if ( jsonNode.has( RefConstant.VALUE ) ) {
        JsonParser valueJsonParser = jsonNode.get( RefConstant.VALUE ).traverse();
        valueJsonParser.setCodec( jp.getCodec() );
        value = valueJsonParser.readValueAs( valueType.getRawClass() );
    }

    return value != null ? new DeadRef( key, value ) : new DeadRef( key );
}
 
Example 5
Source File: RealmsConfigurationLoader.java    From keycloak with Apache License 2.0 6 votes vote down vote up
private static void readUsers(RealmRepresentation r, JsonParser p) throws IOException {
    JsonToken t = p.nextToken();
    if (t != JsonToken.START_ARRAY) {
        throw new RuntimeException("Error reading field 'users'. Expected array of users [" + t + "]");
    }

    t = p.nextToken();
    while (t == JsonToken.START_OBJECT) {
        UserRepresentation u = p.readValueAs(UserRepresentation.class);
        if (!started && userSkipped(u.getUsername())) {
            log.info("User skipped: " + u.getUsername());
        } else {
            enqueueCreateUser(r, u);
        }
        t = p.nextToken();
        currentUser += 1;

        // every some users check to see pending errors
        // in order to short-circuit if any errors have occurred
        if (currentUser % ERROR_CHECK_INTERVAL == 0) {
            checkPendingErrors(u.getUsername());
        }
    }
}
 
Example 6
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 7
Source File: DynamicSimulationResultDeserializer.java    From powsybl-core with Mozilla Public License 2.0 5 votes vote down vote up
@Override
public DynamicSimulationResult deserialize(JsonParser parser, DeserializationContext ctx) throws IOException {
    boolean isOK = false;
    String logs = null;

    while (parser.nextToken() != JsonToken.END_OBJECT) {
        switch (parser.getCurrentName()) {
            case "version":
                parser.nextToken(); // skip
                break;

            case "isOK":
                parser.nextToken();
                isOK = parser.readValueAs(Boolean.class);
                break;

            case "logs":
                parser.nextToken();
                logs = parser.readValueAs(String.class);
                break;

            default:
                throw new AssertionError("Unexpected field: " + parser.getCurrentName());
        }
    }

    return new DynamicSimulationResultImpl(isOK, logs);
}
 
Example 8
Source File: AggregationOrListDeserializer.java    From heroic with Apache License 2.0 5 votes vote down vote up
@Override
public AggregationOrList deserialize(final JsonParser p, final DeserializationContext c)
    throws IOException {
    switch (p.getCurrentToken()) {
        case START_ARRAY:
            final List<Aggregation> chain = p.readValueAs(LIST_OF_AGGREGATIONS);
            return new AggregationOrList(Aggregations.chain(chain));
        case START_OBJECT:
            return new AggregationOrList(Optional.of(p.readValueAs(Aggregation.class)));
        default:
            throw c.wrongTokenException(p, JsonToken.START_OBJECT, null);
    }
}
 
Example 9
Source File: RealmsConfigurationLoader.java    From keycloak with Apache License 2.0 5 votes vote down vote up
private static void readClientRoles(RealmRepresentation r, JsonParser p) throws IOException {
    JsonToken t = p.nextToken();

    if (t != JsonToken.START_OBJECT) {
        throw new RuntimeException("Expected start_of_object on 'roles/client' [" + t + "]");
    }

    t = p.nextToken();

    int count = 0;
    while (t == JsonToken.FIELD_NAME) {
        String client = p.getCurrentName();

        t = p.nextToken();
        if (t != JsonToken.START_ARRAY) {
            throw new RuntimeException("Expected start_of_array on 'roles/client/" + client + " [" + t + "]");
        }

        t = p.nextToken();
        while (t != JsonToken.END_ARRAY) {
            RoleRepresentation u = p.readValueAs(RoleRepresentation.class);
            if (!seeking() || !skipClientRoles) {
                enqueueCreateClientRole(r, u, client);
            }
            t = p.nextToken();
            count += 1;

            // every some roles check to see pending errors
            // in order to short-circuit if any errors have occurred
            if (count % ERROR_CHECK_INTERVAL == 0) {
                checkPendingErrors(u.getName());
            }
        }
        t = p.nextToken();
    }
}
 
Example 10
Source File: StatusDeserializer.java    From allure-java with Apache License 2.0 5 votes vote down vote up
@Override
public Status deserialize(final JsonParser p, final DeserializationContext ctxt) throws IOException {
    final String value = p.readValueAs(String.class);
    return Stream.of(Status.values())
            .filter(status -> status.value().equalsIgnoreCase(value))
            .findAny()
            .orElse(null);
}
 
Example 11
Source File: ServletRequestParamReader.java    From endpoints-java with Apache License 2.0 5 votes vote down vote up
@Override
public Date deserialize(JsonParser jsonParser, DeserializationContext context)
    throws IOException {
  com.google.api.client.util.DateTime date =
      new com.google.api.client.util.DateTime(jsonParser.readValueAs(String.class));
  return new Date(date.getValue());
}
 
Example 12
Source File: Configuration.java    From incubator-taverna-language with Apache License 2.0 5 votes vote down vote up
protected JsonNode parseJson(String jsonString) {
	ObjectMapper mapper = new ObjectMapper();
	try {
		JsonParser parser = mapper.getFactory().createParser(jsonString);
		return parser.readValueAs(JsonNode.class);
	} catch (IOException e) {
		throw new IllegalArgumentException("Not valid JSON string", e);
	}
}
 
Example 13
Source File: FacetFilters.java    From algoliasearch-client-java-2 with MIT License 5 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
public Object deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
  List list = p.readValueAs(List.class);
  if (list.isEmpty()) {
    return new FacetFiltersAsListOfString();
  }
  if (list.get(0) instanceof String) {
    return new FacetFiltersAsListOfString(list);
  }
  return new FacetFiltersAsListOfList(list);
}
 
Example 14
Source File: TotalDeserializer.java    From elasticgeo with GNU General Public License v3.0 5 votes vote down vote up
@Override
public Long deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException, JsonProcessingException {
    try {
        return jsonParser.readValueAs(Long.class);
    }  catch (MismatchedInputException e) {
        JsonNode node = jsonParser.getCodec().readTree(jsonParser);
        return node.get("value").longValue();
    }
}
 
Example 15
Source File: AbstractClientCsdlEdmDeserializer.java    From olingo-odata4 with Apache License 2.0 5 votes vote down vote up
protected ClientCsdlReturnType parseReturnType(final JsonParser jp, final String elementName) throws IOException {
  final ClientCsdlReturnType returnType;
  if (elementName.equals(((FromXmlParser) jp).getStaxReader().getLocalName())) {
    returnType = new ClientCsdlReturnType();
    returnType.setType(jp.nextTextValue());
  } else {
    jp.nextToken();
    returnType = jp.readValueAs(ClientCsdlReturnType.class);
  }
  return returnType;
}
 
Example 16
Source File: MovieModule.java    From sdn-rx with Apache License 2.0 5 votes vote down vote up
@Override
public Roles deserialize(JsonParser jsonParser,
	DeserializationContext deserializationContext) throws IOException {

	return new Roles(jsonParser.readValueAs(new TypeReference<List<String>>() {
	}));
}
 
Example 17
Source File: PipelineOptionsFactoryTest.java    From beam with Apache License 2.0 4 votes vote down vote up
@Override
public JacksonIncompatible deserialize(
    JsonParser jsonParser, DeserializationContext deserializationContext)
    throws IOException, JsonProcessingException {
  return new JacksonIncompatible(jsonParser.readValueAs(String.class));
}
 
Example 18
Source File: IjProjectWriterTest.java    From buck with Apache License 2.0 4 votes vote down vote up
private <T> T readJson(ProjectFilesystem filesystem, Path path, TypeReference<T> typeReference)
    throws IOException {
  JsonParser parser = ObjectMappers.createParser(filesystem.newFileInputStream(path));
  return parser.readValueAs(typeReference);
}
 
Example 19
Source File: ClientCsdlSchema.java    From olingo-odata4 with Apache License 2.0 4 votes vote down vote up
@Override
protected ClientCsdlSchema doDeserialize(final JsonParser jp, final DeserializationContext ctxt)
        throws IOException {
  final ClientCsdlSchema schema = new ClientCsdlSchema();

  for (; jp.getCurrentToken() != JsonToken.END_OBJECT; jp.nextToken()) {
    final JsonToken token = jp.getCurrentToken();
    if (token == JsonToken.FIELD_NAME) {
      if ("Namespace".equals(jp.getCurrentName())) {
        schema.setNamespace(jp.nextTextValue());
      } else if ("Alias".equals(jp.getCurrentName())) {
        schema.setAlias(jp.nextTextValue());
      } else if ("ComplexType".equals(jp.getCurrentName())) {
        jp.nextToken();
        schema.getComplexTypes().add(jp.readValueAs(ClientCsdlComplexType.class));
      } else if ("EntityType".equals(jp.getCurrentName())) {
        jp.nextToken();
        schema.getEntityTypes().add(jp.readValueAs(ClientCsdlEntityType.class));
      } else if ("EnumType".equals(jp.getCurrentName())) {
        jp.nextToken();
        schema.getEnumTypes().add(jp.readValueAs(ClientCsdlEnumType.class));
      } else if ("EntityContainer".equals(jp.getCurrentName())) {
        jp.nextToken();
        ClientCsdlEntityContainer entityContainer = jp.readValueAs(ClientCsdlEntityContainer.class);
        schema.setEntityContainer(entityContainer);
      } else if ("Action".equals(jp.getCurrentName())) {
        jp.nextToken();
        schema.getActions().add(jp.readValueAs(ClientCsdlAction.class));
      } else if ("Function".equals(jp.getCurrentName())) {
        jp.nextToken();
        schema.getFunctions().add(jp.readValueAs(ClientCsdlFunction.class));
      } else if ("TypeDefinition".equals(jp.getCurrentName())) {
        jp.nextToken();
        schema.getTypeDefinitions().add(jp.readValueAs(ClientCsdlTypeDefinition.class));
      }
    } else if ("Annotations".equals(jp.getCurrentName())) {
      jp.nextToken();
      schema.getAnnotationGroups().add(jp.readValueAs(ClientCsdlAnnotations.class));
    } else if ("Annotation".equals(jp.getCurrentName())) {
      jp.nextToken();
      schema.getAnnotations().add(jp.readValueAs(ClientCsdlAnnotation.class));
    } else if ("Term".equals(jp.getCurrentName())) {
      jp.nextToken();
      schema.getTerms().add(jp.readValueAs(ClientCsdlTerm.class));
    }
  }

  return schema;
}
 
Example 20
Source File: JavaEngineTest.java    From yql-plus with Apache License 2.0 4 votes vote down vote up
protected List<TestEntry> parseTestTree(InputStream inputStream) throws IOException {
    JsonParser parser = JSON_FACTORY.createParser(inputStream);
    Spec spec = parser.readValueAs(Spec.class);
    return spec.compute();
}