001/* 002 * Copyright 2015-2024 Ping Identity Corporation 003 * All Rights Reserved. 004 */ 005/* 006 * Copyright 2015-2024 Ping Identity Corporation 007 * 008 * Licensed under the Apache License, Version 2.0 (the "License"); 009 * you may not use this file except in compliance with the License. 010 * You may obtain a copy of the License at 011 * 012 * http://www.apache.org/licenses/LICENSE-2.0 013 * 014 * Unless required by applicable law or agreed to in writing, software 015 * distributed under the License is distributed on an "AS IS" BASIS, 016 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 017 * See the License for the specific language governing permissions and 018 * limitations under the License. 019 */ 020/* 021 * Copyright (C) 2015-2024 Ping Identity Corporation 022 * 023 * This program is free software; you can redistribute it and/or modify 024 * it under the terms of the GNU General Public License (GPLv2 only) 025 * or the terms of the GNU Lesser General Public License (LGPLv2.1 only) 026 * as published by the Free Software Foundation. 027 * 028 * This program is distributed in the hope that it will be useful, 029 * but WITHOUT ANY WARRANTY; without even the implied warranty of 030 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 031 * GNU General Public License for more details. 032 * 033 * You should have received a copy of the GNU General Public License 034 * along with this program; if not, see <http://www.gnu.org/licenses>. 035 */ 036package com.unboundid.util.json; 037 038 039 040import java.io.Serializable; 041 042import com.unboundid.util.NotMutable; 043import com.unboundid.util.NotNull; 044import com.unboundid.util.Nullable; 045import com.unboundid.util.ThreadSafety; 046import com.unboundid.util.ThreadSafetyLevel; 047import com.unboundid.util.Validator; 048 049 050 051/** 052 * This class provides a simple data structure that represents a field in a 053 * JSON object, containing a name and a value. This is primarily intended as a 054 * convenience when programmatically constructing JSON objects. 055 */ 056@NotMutable() 057@ThreadSafety(level=ThreadSafetyLevel.COMPLETELY_THREADSAFE) 058public final class JSONField 059 implements Serializable 060{ 061 /** 062 * The serial version UID for this serializable class. 063 */ 064 private static final long serialVersionUID = -1397826405959590851L; 065 066 067 068 // The value for this field. 069 @NotNull private final JSONValue value; 070 071 // The name for this field. 072 @NotNull private final String name; 073 074 075 076 /** 077 * Creates a new JSON field with the specified name and value. 078 * 079 * @param name The name for this field. It must not be {@code null}. 080 * @param value The value for this field. It must not be {@code null} 081 * (although it may be a {@link JSONNull} instance). 082 */ 083 public JSONField(@NotNull final String name, @NotNull final JSONValue value) 084 { 085 Validator.ensureNotNull(name); 086 Validator.ensureNotNull(value); 087 088 this.name = name; 089 this.value = value; 090 } 091 092 093 094 /** 095 * Creates a new JSON field with the specified name and a {@link JSONBoolean} 096 * value. 097 * 098 * @param name The name for this field. It must not be {@code null}. 099 * @param value The value for this field. It must not be {@code null}. 100 */ 101 public JSONField(@NotNull final String name, final boolean value) 102 { 103 this(name, (value ? JSONBoolean.TRUE : JSONBoolean.FALSE)); 104 } 105 106 107 108 /** 109 * Creates a new JSON field with the specified name and a {@link JSONNumber} 110 * value. 111 * 112 * @param name The name for this field. It must not be {@code null}. 113 * @param value The value for this field. It must not be {@code null}. 114 */ 115 public JSONField(@NotNull final String name, final long value) 116 { 117 this(name, new JSONNumber(value)); 118 } 119 120 121 122 /** 123 * Creates a new JSON field with the specified name and a {@link JSONNumber} 124 * value. 125 * 126 * @param name The name for this field. It must not be {@code null}. 127 * @param value The value for this field. It must not be {@code null}. 128 */ 129 public JSONField(@NotNull final String name, final double value) 130 { 131 this(name, new JSONNumber(value)); 132 } 133 134 135 136 /** 137 * Creates a new JSON field with the specified name and a {@link JSONString} 138 * value. 139 * 140 * @param name The name for this field. It must not be {@code null}. 141 * @param value The value for this field. It must not be {@code null}. 142 */ 143 public JSONField(@NotNull final String name, @NotNull final String value) 144 { 145 this(name, new JSONString(value)); 146 } 147 148 149 150 /** 151 * Retrieves the name for this field. 152 * 153 * @return The name for this field. 154 */ 155 @NotNull() 156 public String getName() 157 { 158 return name; 159 } 160 161 162 163 /** 164 * Retrieves the value for this field. 165 * 166 * @return The value for this field. 167 */ 168 @NotNull() 169 public JSONValue getValue() 170 { 171 return value; 172 } 173 174 175 176 /** 177 * Retrieves a hash code for this JSON field. 178 * 179 * @return A hash code for this JSON field. 180 */ 181 @Override() 182 public int hashCode() 183 { 184 return name.hashCode() + value.hashCode(); 185 } 186 187 188 189 /** 190 * Indicates whether the provided object is considered equal to this JSON 191 * field. 192 * 193 * @param o The object for which to make the determination. 194 * 195 * @return {@code true} if the provided object is a JSON field with the same 196 * name and an equivalent value, or {@code false} if not. 197 */ 198 @Override() 199 public boolean equals(@Nullable final Object o) 200 { 201 if (o == this) 202 { 203 return true; 204 } 205 206 if (o instanceof JSONField) 207 { 208 final JSONField f = (JSONField) o; 209 return (name.equals(f.name) && value.equals(f.value)); 210 } 211 212 return false; 213 } 214 215 216 217 /** 218 * Retrieves a string representation of this field. 219 * 220 * @return A string representation of this field. 221 */ 222 @Override() 223 @NotNull() 224 public String toString() 225 { 226 final StringBuilder buffer = new StringBuilder(); 227 toString(buffer); 228 return buffer.toString(); 229 } 230 231 232 233 /** 234 * Appends a string representation of this field to the provided buffer. 235 * 236 * @param buffer The buffer to which the information should be appended. 237 */ 238 public void toString(@NotNull final StringBuilder buffer) 239 { 240 JSONString.encodeString(name, buffer); 241 buffer.append(':'); 242 value.toString(buffer); 243 } 244}