Skip to content

Instantly share code, notes, and snippets.

@dschulten
Last active January 24, 2022 11:31
Show Gist options
  • Save dschulten/ac44bf5422009aad7677d5972c1d851f to your computer and use it in GitHub Desktop.
Save dschulten/ac44bf5422009aad7677d5972c1d851f to your computer and use it in GitHub Desktop.
JsonNodeBuilders
package de.escalon.builders.json;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.JsonNodeFactory;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.rits.cloning.Cloner;
import java.io.Serializable;
/**
* Convenience {@link JsonNode} builder.
*/
public final class JsonNodeBuilders {
/**
* Factory methods for an {@link ObjectNode} builder.
*/
public static ObjectNodeBuilder object() {
return object(JsonNodeFactory.instance);
}
public static ObjectNodeBuilder object(String k1, boolean v1) {
return object().with(k1, v1);
}
public static ObjectNodeBuilder object(String k1, int v1) {
return object().with(k1, v1);
}
public static ObjectNodeBuilder object(String k1, float v1) {
return object().with(k1, v1);
}
public static ObjectNodeBuilder object(String k1, String v1) {
return object().with(k1, v1);
}
public static ObjectNodeBuilder object(String k1, String v1, String k2, String v2) {
return object(k1, v1).with(k2, v2);
}
public static ObjectNodeBuilder object(String k1, String v1, String k2, String v2,
String k3, String v3) {
return object(k1, v1, k2, v2).with(k3, v3);
}
public static ObjectNodeBuilder object(String k1, JsonNodeBuilder<?> builder) {
return object().with(k1, builder);
}
public static ObjectNodeBuilder object(JsonNodeFactory factory) {
return new ObjectNodeBuilder(factory);
}
/**
* Factory methods for an {@link ArrayNode} builder.
*/
public static ArrayNodeBuilder array() {
return array(JsonNodeFactory.instance);
}
public static ArrayNodeBuilder array(boolean... values) {
return array().with(values);
}
public static ArrayNodeBuilder array(int... values) {
return array().with(values);
}
public static ArrayNodeBuilder array(String... values) {
return array().with(values);
}
public static ArrayNodeBuilder array(JsonNodeBuilder<?>... builders) {
return array().with(builders);
}
public static ArrayNodeBuilder array(JsonNodeFactory factory) {
return new ArrayNodeBuilder(factory);
}
public interface JsonNodeBuilder<T extends JsonNode> {
/**
* Construct and return the {@link JsonNode} instance.
*/
T build();
}
private static abstract class AbstractNodeBuilder<T extends JsonNode> implements JsonNodeBuilder<T>, Serializable {
/**
* The source of values.
*/
protected final JsonNodeFactory factory;
/**
* The value under construction.
*/
protected final T node;
public AbstractNodeBuilder(JsonNodeFactory factory, T node) {
this.factory = factory;
this.node = node;
}
/**
* Returns a valid JSON string, so long as {@code POJONode}s not used.
*/
@Override
public String toString() {
return node.toString();
}
}
public final static class ObjectNodeBuilder extends AbstractNodeBuilder<ObjectNode> {
private ObjectNodeBuilder(JsonNodeFactory factory) {
super(factory, factory.objectNode());
}
public ObjectNodeBuilder withNull(String field) {
return with(field, factory.nullNode());
}
public ObjectNodeBuilder with(String field, int value) {
return with(field, factory.numberNode(value));
}
public ObjectNodeBuilder with(String field, float value) {
return with(field, factory.numberNode(value));
}
public ObjectNodeBuilder with(String field, boolean value) {
return with(field, factory.booleanNode(value));
}
public ObjectNodeBuilder with(String field, String value) {
return with(field, factory.textNode(value));
}
public ObjectNodeBuilder with(String field, JsonNode value) {
node.set(field, value);
return this;
}
public ObjectNodeBuilder with(String field, JsonNodeBuilder<?> builder) {
return with(field, builder.build());
}
public ObjectNodeBuilder withPOJO(String field, Object pojo) {
return with(field, factory.pojoNode(pojo));
}
@Override
public ObjectNode build() {
return node;
}
public ObjectNodeBuilder but() {
Cloner cloner=new Cloner();
return cloner.deepClone(this);
}
}
public final static class ArrayNodeBuilder extends AbstractNodeBuilder<ArrayNode> {
private ArrayNodeBuilder(JsonNodeFactory factory) {
super(factory, factory.arrayNode());
}
public ArrayNodeBuilder with(boolean value) {
node.add(value);
return this;
}
public ArrayNodeBuilder with(boolean... values) {
for (boolean value : values)
with(value);
return this;
}
public ArrayNodeBuilder with(int value) {
node.add(value);
return this;
}
public ArrayNodeBuilder with(int... values) {
for (int value : values)
with(value);
return this;
}
public ArrayNodeBuilder with(float value) {
node.add(value);
return this;
}
public ArrayNodeBuilder with(String value) {
node.add(value);
return this;
}
public ArrayNodeBuilder with(String... values) {
for (String value : values)
with(value);
return this;
}
public ArrayNodeBuilder with(Iterable<String> values) {
for (String value : values)
with(value);
return this;
}
public ArrayNodeBuilder with(JsonNode value) {
node.add(value);
return this;
}
public ArrayNodeBuilder with(JsonNode... values) {
for (JsonNode value : values)
with(value);
return this;
}
public ArrayNodeBuilder with(JsonNodeBuilder<?> value) {
return with(value.build());
}
public ArrayNodeBuilder with(JsonNodeBuilder<?>... builders) {
for (JsonNodeBuilder<?> builder : builders)
with(builder);
return this;
}
@Override
public ArrayNode build() {
return node;
}
}
}
package de.escalon.builders.json;
import net.minidev.json.JSONArray;
import net.minidev.json.JSONObject;
import java.io.Serializable;
/**
* Convenience {@link JSONObject} builder, useful to build expected JSON objects for MockMVC or WebTestClient
* expectations.
*/
public final class JSONObjectBuilders {
/**
* Factory methods for an {@link JSONObject} builder.
*/
public static JSONObjectBuilder object(String k1, boolean v1) {
return object().with(k1, v1);
}
public static JSONObjectBuilder object(String k1, int v1) {
return object().with(k1, v1);
}
public static JSONObjectBuilder object(String k1, float v1) {
return object().with(k1, v1);
}
public static JSONObjectBuilder object(String k1, String v1) {
return object().with(k1, v1);
}
public static JSONObjectBuilder object(String k1, String v1, String k2, String v2) {
return object(k1, v1).with(k2, v2);
}
public static JSONObjectBuilder object(String k1, String v1, String k2, String v2,
String k3, String v3) {
return object(k1, v1, k2, v2).with(k3, v3);
}
public static JSONObjectBuilder object(String k1, JSONBuilder<?> builder) {
return object().with(k1, builder);
}
public static JSONObjectBuilder object() {
return new JSONObjectBuilder();
}
/**
* Factory methods for an {@link JSONArray} builder.
*/
public static JSONArrayBuilder array(boolean... values) {
return array().with(values);
}
public static JSONArrayBuilder array(int... values) {
return array().with(values);
}
public static JSONArrayBuilder array(String... values) {
return array().with(values);
}
public static JSONArrayBuilder array(JSONBuilder<?>... builders) {
return array().with(builders);
}
public static JSONArrayBuilder array() {
return new JSONArrayBuilder();
}
public interface JSONBuilder<T> {
/**
* Construct and return the {@link JSONObject} instance.
*/
T build();
}
private static abstract class AbstractJSONBuilder<T> implements JSONBuilder<T>, Serializable {
/**
* The source of values.
*/
// protected final JsonNodeFactory factory;
/**
* The value under construction.
*/
protected final T node;
public AbstractJSONBuilder(T node) {
this.node = node;
}
/**
* Returns a valid JSON string, so long as {@code POJONode}s not used.
*/
@Override
public String toString() {
return node.toString();
}
}
public final static class JSONObjectBuilder extends AbstractJSONBuilder<JSONObject> {
private JSONObjectBuilder() {
super(new JSONObject());
}
public JSONObjectBuilder withNull(String field) {
node.appendField(field, null);
return this;
}
public JSONObjectBuilder with(String field, int value) {
return with(field, value);
}
public JSONObjectBuilder with(String field, float value) {
return with(field, value);
}
public JSONObjectBuilder with(String field, boolean value) {
return with(field, value);
}
public JSONObjectBuilder with(String field, String value) {
return with(field, value);
}
public JSONObjectBuilder with(String field, JSONObject value) {
return with(field, value);
}
public JSONObjectBuilder with(String field, Object value) {
node.appendField(field, value);
return this;
}
public JSONObjectBuilder with(String field, JSONBuilder<?> builder) {
return with(field, builder.build());
}
@Override
public JSONObject build() {
return node;
}
}
public final static class JSONArrayBuilder extends AbstractJSONBuilder<JSONArray> {
private JSONArrayBuilder() {
super(new JSONArray());
}
public JSONArrayBuilder with(boolean value) {
node.add(value);
return this;
}
public JSONArrayBuilder with(boolean... values) {
for (boolean value : values)
with(value);
return this;
}
public JSONArrayBuilder with(int value) {
node.add(value);
return this;
}
public JSONArrayBuilder with(int... values) {
for (int value : values)
with(value);
return this;
}
public JSONArrayBuilder with(float value) {
node.add(value);
return this;
}
public JSONArrayBuilder with(String value) {
node.add(value);
return this;
}
public JSONArrayBuilder with(String... values) {
for (String value : values)
with(value);
return this;
}
public JSONArrayBuilder with(Iterable<String> values) {
for (String value : values)
with(value);
return this;
}
public JSONArrayBuilder with(JSONObject value) {
node.add(value);
return this;
}
public JSONArrayBuilder with(JSONObject... values) {
for (JSONObject value : values)
with(value);
return this;
}
public JSONArrayBuilder with(JSONBuilder<?> value) {
node.add(value.build());
return this;
}
public JSONArrayBuilder with(JSONBuilder<?>... builders) {
for (JSONBuilder<?> builder : builders)
with(builder);
return this;
}
@Override
public JSONArray build() {
return node;
}
}
}
<dependency>
<groupId>uk.com.robust-it</groupId>
<artifactId>cloning</artifactId>
<version>1.9.10</version>
<scope>test</scope>
</dependency>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment