001package net.tnemc.item; 002 003/* 004 * The New Item Library Minecraft Server Plugin 005 * 006 * Copyright (C) 2022 - 2025 Daniel "creatorfromhell" Vidmar 007 * 008 * This program is free software; you can redistribute it and/or 009 * modify it under the terms of the GNU Lesser General Public 010 * License as published by the Free Software Foundation; either 011 * version 3 of the License, or (at your option) any later version. 012 * 013 * This program is distributed in the hope that it will be useful, 014 * but WITHOUT ANY WARRANTY; without even the implied warranty of 015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 016 * Lesser General Public License for more details. 017 * 018 * You should have received a copy of the GNU Lesser General Public License 019 * along with this program; if not, write to the Free Software Foundation, 020 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 021 */ 022 023import org.json.simple.JSONArray; 024import org.json.simple.JSONObject; 025 026import java.util.ArrayList; 027import java.util.List; 028import java.util.UUID; 029 030public class JSONHelper { 031 032 private JSONObject object; 033 034 public JSONHelper(final JSONObject object) { 035 036 this.object = object; 037 } 038 039 public boolean has(final String identifier) { 040 041 return object.containsKey(identifier); 042 } 043 044 public boolean isNull(final String identifier) { 045 046 return object.get(identifier) == null; 047 } 048 049 public JSONHelper getHelper(final String identifier) { 050 051 return new JSONHelper(getJSON(identifier)); 052 } 053 054 public JSONObject getJSON(final String identifier) { 055 056 return (JSONObject)object.get(identifier); 057 } 058 059 public Short getShort(final String identifier) { 060 061 return Short.valueOf(getString(identifier)); 062 } 063 064 public Float getFloat(final String identifier) { 065 066 return Float.valueOf(getString(identifier)); 067 } 068 069 public Double getDouble(final String identifier) { 070 071 return Double.valueOf(getString(identifier)); 072 } 073 074 public Integer getInteger(final String identifier) { 075 076 return Integer.valueOf(getString(identifier)); 077 } 078 079 public Boolean getBoolean(final String identifier) { 080 081 return Boolean.valueOf(getString(identifier)); 082 } 083 084 public Long getLong(final String identifier) { 085 086 return Long.valueOf(getString(identifier)); 087 } 088 089 public String getString(final String identifier) { 090 091 return object.get(identifier).toString(); 092 } 093 094 public UUID getUUID(final String identifier) { 095 096 return UUID.fromString(object.get(identifier).toString()); 097 } 098 099 /** 100 * Parses a JSON array into a List<String>. 101 * 102 * @param identifier The key of the JSON array. 103 * @return A List<String> containing the elements of the JSON array. 104 */ 105 public List<String> getStringList(final String identifier) { 106 final List<String> result = new ArrayList<>(); 107 if(has(identifier)) { 108 109 final JSONArray array = (JSONArray) object.get(identifier); 110 for(final Object obj : array) { 111 112 result.add(obj.toString()); 113 } 114 } 115 return result; 116 } 117 118 /** 119 * Parses a JSON array into a List<Integer>. 120 * 121 * @param identifier The key of the JSON array. 122 * @return A List<Integer> containing the elements of the JSON array. 123 */ 124 public List<Integer> getIntegerList(final String identifier) { 125 final List<Integer> result = new ArrayList<>(); 126 if(has(identifier)) { 127 128 final JSONArray array = (JSONArray) object.get(identifier); 129 for(final Object obj : array) { 130 131 result.add(Integer.parseInt(obj.toString())); 132 } 133 } 134 return result; 135 } 136 137 /** 138 * Parses a JSON array into a List<Float>. 139 * 140 * @param identifier The key of the JSON array. 141 * @return A List<Float> containing the elements of the JSON array. 142 */ 143 public List<Float> getFloatList(final String identifier) { 144 final List<Float> result = new ArrayList<>(); 145 if(has(identifier)) { 146 147 final JSONArray array = (JSONArray) object.get(identifier); 148 for(final Object obj : array) { 149 150 result.add(Float.parseFloat(obj.toString())); 151 } 152 } 153 return result; 154 } 155 156 /** 157 * Parses a JSON array into a List<Boolean>. 158 * 159 * @param identifier The key of the JSON array. 160 * @return A List<Boolean> containing the elements of the JSON array. 161 */ 162 public List<Boolean> getBooleanList(final String identifier) { 163 final List<Boolean> result = new ArrayList<>(); 164 if(has(identifier)) { 165 166 final JSONArray array = (JSONArray) object.get(identifier); 167 for(final Object obj : array) { 168 169 result.add(Boolean.parseBoolean(obj.toString())); 170 } 171 } 172 return result; 173 } 174 175 /** 176 * Parses a JSON array into an int array. 177 * 178 * @param identifier The key of the JSON array. 179 * @return An int[] containing the elements of the JSON array. 180 */ 181 public int[] getIntArray(final String identifier) { 182 if(has(identifier)) { 183 184 final JSONArray array = (JSONArray) object.get(identifier); 185 186 final int[] result = new int[array.size()]; 187 for(int i = 0; i < array.size(); i++) { 188 result[i] = Integer.parseInt(array.get(i).toString()); 189 } 190 return result; 191 } 192 return new int[0]; // Return an empty array if the key is not present 193 } 194 195 public JSONObject getObject() { 196 197 return object; 198 } 199 200 public void setObject(final JSONObject object) { 201 202 this.object = object; 203 } 204}