com.metamx.common.parsers.JSONPathParser Maven / Gradle / Ivy
The newest version!
/*
* Licensed to Metamarkets Group Inc. (Metamarkets) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. Metamarkets licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package com.metamx.common.parsers;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.base.Charsets;
import com.jayway.jsonpath.Configuration;
import com.jayway.jsonpath.JsonPath;
import com.jayway.jsonpath.Option;
import com.jayway.jsonpath.spi.json.JacksonJsonProvider;
import com.jayway.jsonpath.spi.mapper.JacksonMappingProvider;
import com.metamx.common.Pair;
import com.metamx.common.StringUtils;
import java.math.BigInteger;
import java.nio.charset.CharsetEncoder;
import java.util.ArrayList;
import java.util.EnumSet;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
/**
* JSON parser class that uses the JsonPath library to access fields via path expressions.
*/
public class JSONPathParser implements Parser
{
private final Map> fieldPathMap;
private final boolean useFieldDiscovery;
private final ObjectMapper mapper;
private final CharsetEncoder enc = Charsets.UTF_8.newEncoder();
private final Configuration jsonPathConfig;
/**
* Constructor
*
* @param fieldSpecs List of field specifications.
* @param useFieldDiscovery If true, automatically add root fields seen in the JSON document to the parsed object Map.
* Only fields that contain a singular value or flat list (list containing no subobjects or lists) are automatically added.
* @param mapper Optionally provide an ObjectMapper, used by the parser for reading the input JSON.
*/
public JSONPathParser(List fieldSpecs, boolean useFieldDiscovery, ObjectMapper mapper)
{
this.fieldPathMap = generateFieldPaths(fieldSpecs);
this.useFieldDiscovery = useFieldDiscovery;
this.mapper = mapper == null ? new ObjectMapper() : mapper;
// Avoid using defaultConfiguration, as this depends on json-smart which we are excluding.
this.jsonPathConfig = Configuration.builder()
.jsonProvider(new JacksonJsonProvider())
.mappingProvider(new JacksonMappingProvider())
.options(EnumSet.of(Option.SUPPRESS_EXCEPTIONS))
.build();
}
@Override
public List getFieldNames()
{
return null;
}
@Override
public void setFieldNames(Iterable fieldNames)
{
}
/**
* @param input JSON string. The root must be a JSON object, not an array.
* e.g., {"valid": "true"} and {"valid":[1,2,3]} are supported
* but [{"invalid": "true"}] and [1,2,3] are not.
*
* @return A map of field names and values
*/
@Override
public Map parse(String input)
{
try {
Map map = new LinkedHashMap<>();
Map document = mapper.readValue(
input,
new TypeReference