com.metamx.common.parsers.JSONParser Maven / Gradle / Ivy
The newest version!
/*
* Copyright 2011,2012 Metamarkets Group Inc.
*
* Licensed 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.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.base.Function;
import com.google.common.base.Throwables;
import com.google.common.collect.Lists;
import com.metamx.common.exception.FormattedException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
public class JSONParser implements Parser
{
private final ObjectMapper jsonMapper = new ObjectMapper();
private ArrayList fieldNames = null;
private static final Function valueFunction = new Function()
{
@Override
public String apply(JsonNode node)
{
// use getValueAsText for compatibility with older jackson implementations on EMR
return (node == null || node.isMissingNode() || node.isNull()) ? null : node.asText();
}
};
public JSONParser()
{
}
public JSONParser(Iterable fieldNames)
{
setFieldNames(fieldNames);
}
@Override
public List getFieldNames()
{
return fieldNames;
}
@Override
public void setFieldNames(Iterable fieldNames)
{
ParserUtils.validateFields(fieldNames);
this.fieldNames = Lists.newArrayList(fieldNames);
}
@Override
public Map parse(String input) throws FormattedException
{
try {
Map map = new LinkedHashMap();
JsonNode root = jsonMapper.readTree(input);
Iterator keysIter = (fieldNames == null ? root.fieldNames() : fieldNames.iterator());
while (keysIter.hasNext()) {
String key = keysIter.next();
JsonNode node = root.path(key);
if (node.isArray()) {
final List nodeValue = Lists.newArrayListWithExpectedSize(node.size());
for(final JsonNode subnode : node) {
final String subnodeValue = valueFunction.apply(subnode);
if(subnodeValue != null) {
nodeValue.add(subnodeValue);
}
}
map.put(key, nodeValue);
} else {
final String nodeValue = valueFunction.apply(node);
if(nodeValue != null) {
map.put(key, nodeValue);
}
}
}
return map;
}
catch (Exception e) {
Throwables.propagateIfPossible(e, FormattedException.class);
throw new FormattedException.Builder()
.withErrorCode(FormattedException.ErrorCode.UNPARSABLE_ROW)
.withMessage(e.getMessage())
.build();
}
}
}