001/*
002 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003 *
004 * Copyright (c) 2015-2017 Oracle and/or its affiliates. All rights reserved.
005 *
006 * The contents of this file are subject to the terms of either the GNU
007 * General Public License Version 2 only ("GPL") or the Common Development
008 * and Distribution License("CDDL") (collectively, the "License").  You
009 * may not use this file except in compliance with the License.  You can
010 * obtain a copy of the License at
011 * https://oss.oracle.com/licenses/CDDL+GPL-1.1
012 * or LICENSE.txt.  See the License for the specific
013 * language governing permissions and limitations under the License.
014 *
015 * When distributing the software, include this License Header Notice in each
016 * file and include the License file at LICENSE.txt.
017 *
018 * GPL Classpath Exception:
019 * Oracle designates this particular file as subject to the "Classpath"
020 * exception as provided by Oracle in the GPL Version 2 section of the License
021 * file that accompanied this code.
022 *
023 * Modifications:
024 * If applicable, add the following below the License Header, with the fields
025 * enclosed by brackets [] replaced by your own identifying information:
026 * "Portions Copyright [year] [name of copyright owner]"
027 *
028 * Contributor(s):
029 * If you wish your version of this file to be governed by only the CDDL or
030 * only the GPL Version 2, indicate your decision by adding "[Contributor]
031 * elects to include this software in this distribution under the [CDDL or GPL
032 * Version 2] license."  If you don't indicate a single choice of license, a
033 * recipient has the option to distribute your version of this file under
034 * either the CDDL, the GPL Version 2 or to extend the choice of license to
035 * its licensees as provided above.  However, if you add GPL Version 2 code
036 * and therefore, elected the GPL Version 2 license, then the option applies
037 * only if the new code is made subject to such option by the copyright
038 * holder.
039 */
040
041package javax.json;
042
043/**
044 * This interface represents an immutable implementation of a JSON Pointer
045 * as defined by
046 * <a target=_blank href="http://tools.ietf.org/html/rfc6901">RFC 6901</a>.
047 *
048 * <EMBED CLASS='external-html' DATA-FILE-ID=LICENSE DATA-CIETName=JsonPointer>
049 * 
050 * <BR /><BR />A JSON Pointer, when applied to a target {@link JsonValue},
051 * defines a reference location in the target.
052 * 
053 * <BR /><BR />An empty JSON Pointer string defines a reference to the target itself.
054 * 
055 * <BR /><BR />If the JSON Pointer string is non-empty, it must be a sequence
056 * of '/' prefixed tokens, and the target must either be a {@link JsonArray}
057 * or {@link JsonObject}. If the target is a {@code JsonArray}, the pointer
058 * defines a reference to an array element, and the last token specifies the index.
059 * If the target is a {@link JsonObject}, the pointer defines a reference to a
060 * name/value pair, and the last token specifies the name.
061 * 
062 * <BR /><BR />The method {@link #getValue getValue()} returns the referenced value.
063 * Methods {@link #add add()}, {@link #replace replace()},
064 * and {@link #remove remove()} execute operations specified in
065 * <a target=_blank href="http://tools.ietf.org/html/rfc6902">RFC 6902</a>.
066 *
067 * @see <a target=_blank href="http://tools.ietf.org/html/rfc6901">RFC 6901</a>
068 * @see <a target=_blank href="http://tools.ietf.org/html/rfc6902">RFC 6902</a>
069 *
070 * @since 1.1
071 */
072public interface JsonPointer {
073
074    /**
075     * Adds or replaces a value at the referenced location in the specified
076     * {@code target} with the specified {@code value}.
077     * 
078     * <BR /><BR /><OL CLASS=JDOL>
079     * <li> If the reference is the target (empty JSON Pointer string),
080     *      the specified {@code value}, which must be the same type as
081     *      specified {@code target}, is returned.
082     *      </li>
083     * <li> If the reference is an array element, the specified {@code value} is inserted
084     *      into the array, at the referenced index. The value currently at that location, and
085     *      any subsequent values, are shifted to the right (adds one to the indices).
086     *      Index starts with 0. If the reference is specified with a "-", or if the
087     *      index is equal to the size of the array, the value is appended to the array.
088     *      </li>
089     * <li> the reference is a name/value pair of a {@code JsonObject}, and the
090     *      referenced value exists, the value is replaced by the specified {@code value}.
091     *      If the value does not exist, a new name/value pair is added to the object.
092     *      </li>
093     * </ol>
094     *
095     * @param <T> the target type, must be a subtype of {@link JsonValue}
096     * @param target the target referenced by this {@code JsonPointer}
097     * @param value the value to be added
098     * @return the transformed {@code target} after the value is added.
099     * @throws NullPointerException if {@code target} is {@code null}
100     * @throws JsonException if the reference is an array element and
101     * the index is out of range ({@code index < 0 || index > array size}),
102     * or if the pointer contains references to non-existing objects or arrays.
103     */
104    <T extends JsonStructure> T add(T target, JsonValue value);
105
106    /**
107     * Removes the value at the reference location in the specified {@code target}.
108     *
109     * @param <T> the target type, must be a subtype of {@link JsonValue}
110     * @param target the target referenced by this {@code JsonPointer}
111     * @return the transformed {@code target} after the value is removed.
112     * @throws NullPointerException if {@code target} is {@code null}
113     * @throws JsonException if the referenced value does not exist,
114     *    or if the reference is the target.
115     */
116    <T extends JsonStructure> T remove(T target);
117
118    /**
119     * Replaces the value at the referenced location in the specified
120     * {@code target} with the specified {@code value}.
121     *
122     * @param <T> the target type, must be a subtype of {@link JsonValue}
123     * @param target the target referenced by this {@code JsonPointer}
124     * @param value the value to be stored at the referenced location
125     * @return the transformed {@code target} after the value is replaced.
126     * @throws NullPointerException if {@code target} is {@code null}
127     * @throws JsonException if the referenced value does not exist,
128     *    or if the reference is the target.
129     */
130    <T extends JsonStructure> T replace(T target, JsonValue value);
131
132    /**
133     * Returns {@code true} if there is a value at the referenced location in the specified {@code target}.
134     *
135     * @param target the target referenced by this {@code JsonPointer}
136     * @return {@code true} if this pointer points to a value in a specified {@code target}.
137     */
138    boolean containsValue(JsonStructure target);
139
140    /**
141     * Returns the value at the referenced location in the specified {@code target}.
142     *
143     * @param target the target referenced by this {@code JsonPointer}
144     * @return the referenced value in the target.
145     * @throws NullPointerException if {@code target} is null
146     * @throws JsonException if the referenced value does not exist
147     */
148    JsonValue getValue(JsonStructure target);
149}