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.fasterxml.jackson.databind.JsonNode;
7   import com.fasterxml.jackson.databind.node.JsonNodeType;
8   import de.larssh.json.dom.JsonDomType;
9   import de.larssh.json.dom.children.JsonDomArrayChildren;
10  import de.larssh.json.dom.children.JsonDomChildren;
11  import de.larssh.json.dom.children.JsonDomObjectChildren;
12  import edu.umd.cs.findbugs.annotations.NonNull;
13  import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
14  
15  /**
16   * Implementation of {@link JsonDomValue} for
17   * <a href="https://github.com/FasterXML/jackson">Jackson</a> and its
18   * {@link JsonNode}.
19   */
20  public class JacksonDomValue implements JsonDomValue<JsonNode> {
21  	/**
22  	 * Wrapped JSON element
23  	 */
24  	private final JsonNode jsonElement;
25  
26  	/**
27  	 * {@inheritDoc}
28  	 */
29  	@NonNull
30  	@Override
31  	public JsonDomChildren<JacksonDomValue> getChildren() {
32  		final JsonNode node = getJsonElement();
33  		if (node.isArray()) {
34  			return new JsonDomArrayChildren<>(node.size(), node, JacksonDomValue::new);
35  		}
36  		if (node.isObject()) {
37  			return new JsonDomObjectChildren<>(node.fields(), JacksonDomValue::new);
38  		}
39  		return Collections::emptySet;
40  	}
41  
42  	/**
43  	 * {@inheritDoc}
44  	 */
45  	@NonNull
46  	@Override
47  	public String getTextValue() {
48  		final JsonNode node = getJsonElement();
49  		return node.isTextual() ? node.asText() : node.toString();
50  	}
51  
52  	/**
53  	 * {@inheritDoc}
54  	 */
55  	@NonNull
56  	@Override
57  	@SuppressFBWarnings(value = "WEM_WEAK_EXCEPTION_MESSAGING", justification = "there is no more information about nodeType")
58  	public JsonDomType getType() {
59  		final JsonNodeType nodeType = getJsonElement().getNodeType();
60  		if (nodeType == JsonNodeType.ARRAY) {
61  			return JsonDomType.ARRAY;
62  		}
63  		if (nodeType == JsonNodeType.BOOLEAN) {
64  			return JsonDomType.BOOLEAN;
65  		}
66  		if (nodeType == JsonNodeType.NULL) {
67  			return JsonDomType.NULL;
68  		}
69  		if (nodeType == JsonNodeType.NUMBER) {
70  			return JsonDomType.NUMBER;
71  		}
72  		if (nodeType == JsonNodeType.OBJECT) {
73  			return JsonDomType.OBJECT;
74  		}
75  		if (nodeType == JsonNodeType.STRING) {
76  			return JsonDomType.STRING;
77  		}
78  		throw new DOMException(DOMException.NOT_SUPPORTED_ERR, "Unknown JSON node type.");
79  	}
80  
81  	/**
82  	 * {@inheritDoc}
83  	 */
84  	@NonNull
85  	@Override
86  	public String toString() {
87  		return getJsonElement().toString();
88  	}
89  
90  	/**
91  	 * Wrapped JSON element
92  	 *
93  	 * @return wrapped JSON element
94  	 */
95  	@java.lang.SuppressWarnings("all")
96  	@edu.umd.cs.findbugs.annotations.SuppressFBWarnings(justification = "generated code")
97  	@lombok.Generated
98  	public JsonNode getJsonElement() {
99  		return this.jsonElement;
100 	}
101 
102 	@java.lang.SuppressWarnings("all")
103 	@edu.umd.cs.findbugs.annotations.SuppressFBWarnings(justification = "generated code")
104 	@lombok.Generated
105 	public JacksonDomValue(final JsonNode jsonElement) {
106 		this.jsonElement = jsonElement;
107 	}
108 
109 	@java.lang.Override
110 	@java.lang.SuppressWarnings("all")
111 	@edu.umd.cs.findbugs.annotations.SuppressFBWarnings(justification = "generated code")
112 	@lombok.Generated
113 	public boolean equals(@edu.umd.cs.findbugs.annotations.Nullable final java.lang.Object o) {
114 		if (o == this) return true;
115 		if (!(o instanceof JacksonDomValue)) return false;
116 		final JacksonDomValue other = (JacksonDomValue) o;
117 		if (!other.canEqual((java.lang.Object) this)) return false;
118 		final java.lang.Object this$jsonElement = this.getJsonElement();
119 		final java.lang.Object other$jsonElement = other.getJsonElement();
120 		if (this$jsonElement == null ? other$jsonElement != null : !this$jsonElement.equals(other$jsonElement)) return false;
121 		return true;
122 	}
123 
124 	@java.lang.SuppressWarnings("all")
125 	@edu.umd.cs.findbugs.annotations.SuppressFBWarnings(justification = "generated code")
126 	@lombok.Generated
127 	protected boolean canEqual(@edu.umd.cs.findbugs.annotations.Nullable final java.lang.Object other) {
128 		return other instanceof JacksonDomValue;
129 	}
130 
131 	@java.lang.Override
132 	@java.lang.SuppressWarnings("all")
133 	@edu.umd.cs.findbugs.annotations.SuppressFBWarnings(justification = "generated code")
134 	@lombok.Generated
135 	public int hashCode() {
136 		final int PRIME = 59;
137 		int result = 1;
138 		final java.lang.Object $jsonElement = this.getJsonElement();
139 		result = result * PRIME + ($jsonElement == null ? 43 : $jsonElement.hashCode());
140 		return result;
141 	}
142 }