001package ezvcard.io.json;
002
003import java.util.List;
004import java.util.Map;
005import java.util.Objects;
006
007/*
008 Copyright (c) 2012-2026, Michael Angstadt
009 All rights reserved.
010
011 Redistribution and use in source and binary forms, with or without
012 modification, are permitted provided that the following conditions are met: 
013
014 1. Redistributions of source code must retain the above copyright notice, this
015 list of conditions and the following disclaimer. 
016 2. Redistributions in binary form must reproduce the above copyright notice,
017 this list of conditions and the following disclaimer in the documentation
018 and/or other materials provided with the distribution. 
019
020 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
021 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
022 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
023 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
024 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
025 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
026 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
027 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
028 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
029 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
030 */
031
032/**
033 * Represents a JSON value, array, or object.
034 * @author Michael Angstadt
035 */
036public class JsonValue {
037        private final boolean isNull;
038        private final Object value;
039        private final List<JsonValue> array;
040        private final Map<String, JsonValue> object;
041
042        /**
043         * Creates a JSON value (such as a string or integer).
044         * @param value the value
045         */
046        public JsonValue(Object value) {
047                this.value = value;
048                array = null;
049                object = null;
050                isNull = (value == null);
051        }
052
053        /**
054         * Creates a JSON array.
055         * @param array the array elements
056         */
057        public JsonValue(List<JsonValue> array) {
058                this.array = array;
059                value = null;
060                object = null;
061                isNull = (array == null);
062        }
063
064        /**
065         * Creates a JSON object.
066         * @param object the object fields
067         */
068        public JsonValue(Map<String, JsonValue> object) {
069                this.object = object;
070                value = null;
071                array = null;
072                isNull = (object == null);
073        }
074
075        /**
076         * Gets the JSON value.
077         * @return the value or null if it's not a JSON value
078         */
079        public Object getValue() {
080                return value;
081        }
082
083        /**
084         * Gets the JSON array elements.
085         * @return the array elements or null if it's not a JSON array
086         */
087        public List<JsonValue> getArray() {
088                return array;
089        }
090
091        /**
092         * Gets the JSON object.
093         * @return the object or null if it's not a JSON object
094         */
095        public Map<String, JsonValue> getObject() {
096                return object;
097        }
098
099        /**
100         * Determines if the value is "null" or not.
101         * @return true if the value is "null", false if not
102         */
103        public boolean isNull() {
104                return isNull;
105        }
106
107        @Override
108        public int hashCode() {
109                return Objects.hash(array, isNull, object, value);
110        }
111
112        @Override
113        public boolean equals(Object obj) {
114                if (this == obj) return true;
115                if (obj == null) return false;
116                if (getClass() != obj.getClass()) return false;
117                JsonValue other = (JsonValue) obj;
118                return Objects.equals(array, other.array) && isNull == other.isNull && Objects.equals(object, other.object) && Objects.equals(value, other.value);
119        }
120
121        @Override
122        public String toString() {
123                if (isNull) {
124                        return "NULL";
125                }
126
127                if (value != null) {
128                        return "VALUE = " + value;
129                }
130
131                if (array != null) {
132                        return "ARRAY = " + array;
133                }
134
135                if (object != null) {
136                        return "OBJECT = " + object;
137                }
138
139                return "";
140        }
141}