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