com.cefriel.template.io.json.JSONReader Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of mapping-template Show documentation
Show all versions of mapping-template Show documentation
A template-based component exploiting Apache Velocity to define
declarative mappings for schema and data transformations.
/*
* Copyright (c) 2019-2023 Cefriel.
*
* 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.cefriel.template.io.json;
import com.cefriel.template.io.Reader;
import com.jayway.jsonpath.Configuration;
import com.jayway.jsonpath.JsonPath;
import com.jayway.jsonpath.Option;
import com.jayway.jsonpath.PathNotFoundException;
import net.minidev.json.JSONArray;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.*;
public class JSONReader implements Reader {
private final Logger log = LoggerFactory.getLogger(JSONReader.class);
Object document;
private boolean verbose;
public JSONReader(String json) {
Configuration conf = Configuration.defaultConfiguration()
.addOptions(Option.ALWAYS_RETURN_LIST);
document = conf.jsonProvider().parse(json);
}
public JSONReader(File file) throws IOException {
if (Files.exists(file.toPath())) {
Configuration conf = Configuration.defaultConfiguration()
.addOptions(Option.ALWAYS_RETURN_LIST);
document = conf.jsonProvider().parse(Files.readString(Paths.get(file.getPath())));
} else
throw new IllegalArgumentException("FILE: " + file.getPath() + " FOR JSONREADER DOES NOT EXIST");
}
@Override
public void setQueryHeader(String header) {}
@Override
public void appendQueryHeader(String s) {}
@Override
public List
© 2015 - 2024 Weber Informatics LLC | Privacy Policy