Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* Copyright 2011 the original author or authors.
* 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.jayway.jsonpath;
import com.jayway.jsonpath.internal.PathToken;
import com.jayway.jsonpath.spi.JsonProvider;
import com.jayway.jsonpath.spi.JsonProviderFactory;
import com.jayway.jsonpath.spi.MappingProviderFactory;
import org.apache.commons.io.IOUtils;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.*;
import static java.util.Arrays.asList;
import static org.apache.commons.lang.Validate.notEmpty;
import static org.apache.commons.lang.Validate.notNull;
/**
* A JsonModel represents a parsed JSON document that provides easy and efficient read operations. In contrast to the
* static read operations provided by {@link JsonPath} a JsonModel will only parse the document once.
*
* @author Kalle Stenflo
*/
public class JsonModel {
private Object jsonObject;
private JsonProvider jsonProvider;
// --------------------------------------------------------
//
// Constructors
//
// --------------------------------------------------------
private JsonModel(String jsonObject, JsonProvider jsonProvider) {
notNull(jsonObject, "json can not be null");
this.jsonProvider = jsonProvider;
this.jsonObject = jsonProvider.parse(jsonObject);
}
/**
* Creates a new JsonModel based on a json document.
* Note that the jsonObject must either a {@link List} or a {@link Map}
*
* @param jsonObject the json object
* @param jsonProvider
*/
private JsonModel(Object jsonObject, JsonProvider jsonProvider) {
notNull(jsonObject, "json can not be null");
if (!(jsonObject instanceof Map) && !(jsonObject instanceof List)) {
throw new IllegalArgumentException("Invalid container object");
}
this.jsonProvider = jsonProvider;
this.jsonObject = jsonObject;
}
/**
* Creates a new JsonModel based on an {@link InputStream}
*
* @param jsonInputStream the input stream
* @param jsonProvider
*/
private JsonModel(InputStream jsonInputStream, JsonProvider jsonProvider) {
notNull(jsonInputStream, "jsonInputStream can not be null");
this.jsonProvider = jsonProvider;
this.jsonObject = jsonProvider.parse(jsonInputStream);
}
/**
* Creates a new JsonModel by fetching the content from the provided URL
*
* @param jsonURL the URL to read
* @param jsonProvider
* @throws IOException failed to load URL
*/
private JsonModel(URL jsonURL, JsonProvider jsonProvider) throws IOException {
notNull(jsonURL, "jsonURL can not be null");
InputStream jsonInputStream = null;
try {
jsonInputStream = jsonURL.openStream();
this.jsonObject = jsonProvider.parse(jsonInputStream);
this.jsonProvider = jsonProvider;
} finally {
IOUtils.closeQuietly(jsonInputStream);
}
}
// --------------------------------------------------------
//
// Getters
//
// --------------------------------------------------------
public Object getJsonObject() {
return this.jsonObject;
}
// --------------------------------------------------------
//
// Model readers
//
// --------------------------------------------------------
@SuppressWarnings({"unchecked"})
public T get(String jsonPath, Filter... filters) {
return (T) get(JsonPath.compile(jsonPath, filters));
}
@SuppressWarnings({"unchecked"})
public T get(JsonPath jsonPath) {
notNull(jsonPath, "jsonPath can not be null");
return (T) jsonPath.read(jsonObject);
}
// --------------------------------------------------------
//
// Model writers
//
// --------------------------------------------------------
public ArrayOps opsForArray(String jsonPath) {
return opsForArray(JsonPath.compile(jsonPath));
}
public ArrayOps opsForArray(JsonPath jsonPath) {
notNull(jsonPath, "jsonPath can not be null");
List