View Javadoc
1   // Generated by delombok at Fri Nov 29 09:48:08 UTC 2024
2   package de.larssh.json.dom.values;
3   
4   import java.util.Collections;
5   import org.w3c.dom.DOMException;
6   import com.google.gson.JsonArray;
7   import com.google.gson.JsonElement;
8   import com.google.gson.JsonPrimitive;
9   import de.larssh.json.dom.JsonDomType;
10  import de.larssh.json.dom.children.JsonDomArrayChildren;
11  import de.larssh.json.dom.children.JsonDomChildren;
12  import de.larssh.json.dom.children.JsonDomObjectChildren;
13  import edu.umd.cs.findbugs.annotations.NonNull;
14  import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
15  
16  /**
17   * Implementation of {@link JsonDomValue} for {@link com.google.gson.Gson} and
18   * its {@link JsonElement}.
19   */
20  public class GsonDomValue implements JsonDomValue<JsonElement> {
21  	/**
22  	 * Wrapped JSON element
23  	 */
24  	private final JsonElement jsonElement;
25  
26  	/**
27  	 * {@inheritDoc}
28  	 */
29  	@NonNull
30  	@Override
31  	public JsonDomChildren<GsonDomValue> getChildren() {
32  		final JsonElement element = getJsonElement();
33  		if (element.isJsonArray()) {
34  			final JsonArray array = element.getAsJsonArray();
35  			return new JsonDomArrayChildren<>(array.size(), array, GsonDomValue::new);
36  		}
37  		if (element.isJsonObject()) {
38  			return new JsonDomObjectChildren<>(element.getAsJsonObject().entrySet(), GsonDomValue::new);
39  		}
40  		return Collections::emptySet;
41  	}
42  
43  	/**
44  	 * {@inheritDoc}
45  	 */
46  	@NonNull
47  	@Override
48  	public String getTextValue() {
49  		final JsonElement element = getJsonElement();
50  		return element.isJsonPrimitive() && element.getAsJsonPrimitive().isString() ? element.getAsString() : element.toString();
51  	}
52  
53  	/**
54  	 * {@inheritDoc}
55  	 */
56  	@NonNull
57  	@Override
58  	@SuppressWarnings("PMD.CyclomaticComplexity")
59  	@SuppressFBWarnings(value = "WEM_WEAK_EXCEPTION_MESSAGING", justification = "there is no more information about element and primitive")
60  	public JsonDomType getType() {
61  		final JsonElement element = getJsonElement();
62  		if (element.isJsonArray()) {
63  			return JsonDomType.ARRAY;
64  		}
65  		if (element.isJsonNull()) {
66  			return JsonDomType.NULL;
67  		}
68  		if (element.isJsonObject()) {
69  			return JsonDomType.OBJECT;
70  		}
71  		if (!element.isJsonPrimitive()) {
72  			throw new DOMException(DOMException.NOT_SUPPORTED_ERR, "Unknown JSON data type.");
73  		}
74  		final JsonPrimitive primitive = element.getAsJsonPrimitive();
75  		if (primitive.isBoolean()) {
76  			return JsonDomType.BOOLEAN;
77  		}
78  		if (primitive.isNumber()) {
79  			return JsonDomType.NUMBER;
80  		}
81  		if (primitive.isString()) {
82  			return JsonDomType.STRING;
83  		}
84  		throw new DOMException(DOMException.NOT_SUPPORTED_ERR, "Unknown primitive JSON data type.");
85  	}
86  
87  	/**
88  	 * {@inheritDoc}
89  	 */
90  	@NonNull
91  	@Override
92  	public String toString() {
93  		return getJsonElement().toString();
94  	}
95  
96  	/**
97  	 * Wrapped JSON element
98  	 *
99  	 * @return wrapped JSON element
100 	 */
101 	@java.lang.SuppressWarnings("all")
102 	@edu.umd.cs.findbugs.annotations.SuppressFBWarnings(justification = "generated code")
103 	@lombok.Generated
104 	public JsonElement getJsonElement() {
105 		return this.jsonElement;
106 	}
107 
108 	@java.lang.SuppressWarnings("all")
109 	@edu.umd.cs.findbugs.annotations.SuppressFBWarnings(justification = "generated code")
110 	@lombok.Generated
111 	public GsonDomValue(final JsonElement jsonElement) {
112 		this.jsonElement = jsonElement;
113 	}
114 
115 	@java.lang.Override
116 	@java.lang.SuppressWarnings("all")
117 	@edu.umd.cs.findbugs.annotations.SuppressFBWarnings(justification = "generated code")
118 	@lombok.Generated
119 	public boolean equals(@edu.umd.cs.findbugs.annotations.Nullable final java.lang.Object o) {
120 		if (o == this) return true;
121 		if (!(o instanceof GsonDomValue)) return false;
122 		final GsonDomValue other = (GsonDomValue) o;
123 		if (!other.canEqual((java.lang.Object) this)) return false;
124 		final java.lang.Object this$jsonElement = this.getJsonElement();
125 		final java.lang.Object other$jsonElement = other.getJsonElement();
126 		if (this$jsonElement == null ? other$jsonElement != null : !this$jsonElement.equals(other$jsonElement)) return false;
127 		return true;
128 	}
129 
130 	@java.lang.SuppressWarnings("all")
131 	@edu.umd.cs.findbugs.annotations.SuppressFBWarnings(justification = "generated code")
132 	@lombok.Generated
133 	protected boolean canEqual(@edu.umd.cs.findbugs.annotations.Nullable final java.lang.Object other) {
134 		return other instanceof GsonDomValue;
135 	}
136 
137 	@java.lang.Override
138 	@java.lang.SuppressWarnings("all")
139 	@edu.umd.cs.findbugs.annotations.SuppressFBWarnings(justification = "generated code")
140 	@lombok.Generated
141 	public int hashCode() {
142 		final int PRIME = 59;
143 		int result = 1;
144 		final java.lang.Object $jsonElement = this.getJsonElement();
145 		result = result * PRIME + ($jsonElement == null ? 43 : $jsonElement.hashCode());
146 		return result;
147 	}
148 }